Q: Howto capture the 'virtual keyboard show/hide' event in Android?

D: 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?

Test Case #17


File ID: #4365637-0-cc


public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation = = Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    }
    else if (newConfig.orientation = = Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
    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();
    }
}

  1. This only works if you have registered the activity to listen to the configChanges you want in the AndroidManifest.
  2. Does this work in a fragment as well? I have tried this solution but onConfigurationChanged() does not seem to be called at all.
  3. ConfigurationChange does not work for SOFT keyboards.

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: