Hamzah Malik
Hamzah Malik

Reputation: 2570

How can i find the height of soft-keyboard in a LibGDX application

I'm developing an Android game with LibGDX. And I need to find the height of the soft-keyboard the user is using, as I want to display some objects right above the keyboard.

I have read this thread: how can i get soft keyboard height on android?

But the comment suggests that it doesn't work, and it also seems to be for using with Android SDK. I'm not sure. Does anyone know a way that will definitely work?

Upvotes: 10

Views: 1240

Answers (2)

Subrata Mondal
Subrata Mondal

Reputation: 852

Hopefully someone will find this answer helpful:

There is a workout to detect the exact height of the soft-keyboard which involve the Launcher Activity to send screen dimension to the game when a screen resize event occurs.

First, set a layout listener on the ViewTreeObserver of the rootView of your LauncherActivity:

public class AndroidLauncher extends AndroidApplication {
//...
    public void setListenerToRootView() {
        final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
    }

    private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect visibleDisplayFrame = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleDisplayFrame);
            game.screenResize(visibleDisplayFrame.width(), visibleDisplayFrame.height());
        }
    };
//...
}

If you try to get the height of the root view, it will not work as most of the games are fullscreen.

Don't forget to add and remove the listener on appropriate occurrences:

@Override
protected void onCreate (Bundle savedInstanceState) {
    //...
    setListenerToRootView();
}

@Override
protected void onDestroy () {
    super.onDestroy();
    removeListenerToRootView();
}

public void removeListenerToRootView() {
    final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);
}

Next, declare the screenResize method inside the Game Class which will receive the dimensions and send it to the current screen:

public class YourGame extends Game {
//...
public ScreenBase currentScreen;
//...
    public void screenResize(float width, float height) {
        if(currentScreen != null)
            currentScreen.onScreenResize(width, height);
    }
//...
}

Every screen that involves a change must implement the onScreenResize method. Introduce an Abstract Base Class of screen that has an abstract method onScreenResize. The currentScreen variable must be set in the constructor:

public abstract class ScreenBase implements Screen {
//...

public ScreenBase(YourGame game) {
//...
    this.game = game;
    this.game.currentScreen = this;
//....
}

public abstract void onScreenResize(float width, float height);

Implement these in whichever screen you want:

public class LoginScreen extends ScreenBase {  
//...
    @Override
    public void onScreenResize(final float width, final float height) {
        if(Gdx.graphics.getHeight() > height) {
            Gdx.app.log("LoginScreen", "Height of keyboard: " + (Gdx.graphics.getHeight() - height));
        }
    }
}

Upvotes: 3

JohnyTex
JohnyTex

Reputation: 3481

If your problem is that your textfields are obscured then I suggest using

void Gdx.input.getTextInput(Input.TextInputListener listener, java.lang.String title, java.lang.String text)

instead because that will generate a native modal text input dialog that moves up and down with the keyboard. I have tried to get the height of the keyboard as well but so far I haven't managed.

See answers for this thread as well: How do libgdx detect keyboard presence

Upvotes: 3

Related Questions