Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onConfigurationChanged() not firing for keyboardHidden

Tags:

java

android

xml

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.

like image 899
Tony Lukasavage Avatar asked Jun 08 '11 13:06

Tony Lukasavage


1 Answers

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.

like image 51
Shawn Lauzon Avatar answered Nov 18 '22 23:11

Shawn Lauzon