19

I've followed the various questions and answers here to setup my Android activity to override the onConfigurationChanged() in order to execute logic when the soft keyboard opens and closes. Here's the relevant excerpts from my code. I've boiled it down to the simplest scenario:

AndroidManifest.xml

...
<activity 
    android:name=".SearchActivity" 
    android:label="@string/app_name" 
    android:configChanges="keyboard|keyboardHidden|orientation"
/>
...

SearchActivity.java

...
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Toast.makeText(this, "onConfigurationChanged()", Toast.LENGTH_SHORT).show();
}
...

The code above will display the Toast when I change orientation, but does nothing when the soft keyboard opens or closes. I have tested opening the soft keyboard via EditText focusing and by manually opening it with a long press on the menu button. Neither fire the onConfigurationChanged() call.

So the code in place appears to work since orientation change fires, but I get nothing for the soft keyboard. Any ideas? If the answer is "onConfigurationChanged() doesn't catch soft keyboard events", what is an appropriate solution for detecting and handling this event?

Just in case it's relevant, I am testing on a Droid X running Gingerbread.

Tony Lukasavage
  • 1,937
  • 1
  • 14
  • 26

1 Answers1

16

No, onConfigurationChange() doesn't catch soft keyboard events: it's not a configuration change. The orientation change causes a new set of resources to be used (such as layout-land vs layout-port), which is the definition of a configuration change.

So how to do it? Well, There's no event fired when the keyboard is shown, but you can detect when the keyboard causes your layout to be adjusted.

See How to check visibility of software keyboard in Android? for the code.

Community
  • 1
  • 1
Shawn Lauzon
  • 6,234
  • 4
  • 35
  • 46
  • 35
    This sounds like a bug to me. `keyboardHidden` is clearly a part of `Configuration` object and even used in the example here http://developer.android.com/guide/topics/resources/runtime-changes.html From the docs: `A flag indicating whether any keyboard is available. Unlike hardKeyboardHidden, this also takes into account a soft keyboard, so if the hard keyboard is hidden but there is soft keyboard available, it will be set to NO. Value is one of: KEYBOARDHIDDEN_NO, KEYBOARDHIDDEN_YES.` – Ilia G Aug 14 '12 at 23:25