godotengine / godotengine/godot

Android: Virtual keyboard height is not updated correctly when the keyboard resizes

Open
#116,606 0 comments 0 reactions 0 assignees View on GitHub
bug platform:android topic:platforms
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Reproducible in Godot v4.6.stable.official [89cea1439]

### System information

Android 12

### Issue description

When a virtual keyboard is displayed, `DisplayServer.virtual_keyboard_get_height()` correctly returns the initial size of the keyboard. However, if the keyboard size changes while it is already open (e.g. when the emoji menu is toggled), the method continues to return the old height value.

The updated height is only reflected after the keyboard is closed and reopened.

Image
Image

**Investigation**
I traced the bug to the JNI function `Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight` in `platform/android/java_godot_lib_jni.cpp`. This led me to the Kotlin implementation in `platform/android/java/lib/src/main/java/org/godotengine/godot/Godot.kt`, specifically the call to `GodotLib.setVirtualKeyboardHeight(finalHeight)`.

**My fix**
I am not an Android/Kotlin developer, but I consulted an AI tool for a potential solution. It suggested adding the following:

```
val isImeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())

if (isImeVisible) {
val imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime())
val keyboardHeight = imeInsets.bottom - imeInsets.top
GodotLib.setVirtualKeyboardHeight(keyboardHeight)
}
else {
GodotLib.setVirtualKeyboardHeight(0)
}
```
In the `ViewCompat.setOnApplyWindowInsetsListener(rootView)`

This worked for me, but I don't trust AI generated slop and belive there is a better solution.

### Steps to reproduce

1. Create a scene with a `LineEdit` control and a `Label` control.
2. Write a script to assigned the value of `DisplayServer.virtual_keyboard_get_height()` to the `Label` text (e.g., inside `_process` or via a timer).
3. Export and install the project on an **Android** device.
4. Tap the `LineEdit` to open the virtual keyboard and note the height shown on the label.
5. Change the keyboard size while it is still open (e.g., by opening the emoji menu).
6. **Observation**: Notice that the `Label` text does not update to reflect the new height.
7. Close the keyboard and reopen it. Observe that the height now reflects the emoji menu size rather than the standard keyboard size.

### Minimal reproduction project (MRP)

[Virtual keyboard bug repo](https://github.com/Abbion/GodotVirtualKeyboardBug)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.