Sander Versluys Sander Versluys - 1 year ago 85
Android Question

How to capture the "virtual keyboard show/hide" event in Android?

I would like to alter the layout based on whether the virtual keyboard is shown or not. I've searched the API and various blogs but can't seem to find anything useful.

Is it possible?


Answer Source

You've got to handle configuration changes yourself.


// from the link above
public void onConfigurationChanged(Configuration newConfig) {

    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();

Then just change the visibility of some views, update a field, and change your layout file.


This solution will not work for soft keyboards and onConfigurationChanged will not be called for soft keyboards.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download