1

i have an activity with an edit box, when the user touches inside the edit box, the soft keyboard comes up. if the user presses the hardware "back" button, the soft keyboard goes away. I want to detect this situation. I have looked around and the best response i've seen so far is this one:

http://groups.google.com/group/android-developers/browse_thread/thread/9d1681a01f05e782

my question is -- how can you detect if your application window has been resized?

I added this text to my activities in my android manifest file: android:windowSoftInputMode="adjustResize"

but I'm not quite sure how to detect the change.

any help greatly appreciated.

Heath Hunnicutt
  • 18,667
  • 3
  • 39
  • 62
user645402
  • 737
  • 1
  • 16
  • 34
  • You might take a look at http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android – Scott W Sep 06 '12 at 17:43

2 Answers2

1

Android does not provide an API for checking if the keyboard is visible or not. You can, however, key off the height of your top level layout to determine this information.

First, you have to set your activity's android:windowSoftInputMode attribute to "adjustResize".

Then, create a new class that extends your desired layout type (eg LinearLayout). In that class, you can override a few different methods that will be called as the height of your layout changes (due to the keyboard being shown or hidden). When these calls are triggered, you can compare the height of your layout to the height of the screen. If there's a substantial difference between the two (ie more than just the size of the notification bar), the keyboard is visible.

Finally, make sure that you use your new class as the top level layout in your layout xml (eg in place of LinearLayout).

If you would like a more thorough explanation, I've written one up: http://www.cannedcoding.com/2011/08/soft-keyboard.html

Brian H
  • 11
  • 1
0

Creating an Input Method Service (See http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html) to listen for the back button might work. When it is pressed, you can pass that on to your activity which then does what you want it to do along with removing the soft keyboard.

Travis
  • 3
  • 3