Today at work, I got this problem: catching event when soft keyboard comes up/down (or show/hide) ?!
At first, I thought it would be easy to resolve, but after a while of googling, I realized that there is no official event or listener in Android API for that, but I did find some workarounds like:
http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android
http://stackoverflow.com/questions/3793093/android-edittext-soft-keyboard-show-hide-event
Well, there are some workarounds that were already posted but they are incomplete (some work on 4.0.4 but not 2.3, some don't work all the time), but at least they are still useful, they gave me an idea to make this "complete" workaround (well, at least it works like a charm in my case for now, I tested on both Android 2.3 and 4.0.4), so I would like to share it here, hope it helps someone out there ;)
Usage: just use following LinearLayout overridden class as the main layout(and of course, use "match_parent" for its layout_width and layout_height) of the activity where you want to catch soft keyboard show/hide events, then setOnSoftKeyboardVisibilityChangeListener for your main layout. Finally, you need this line in your <activity> tag (in manifest.xml) where SoftKeyboardHandledLinearLayout is used:
Take a look at following code to get a better idea of how to implement this hack.
At first, I thought it would be easy to resolve, but after a while of googling, I realized that there is no official event or listener in Android API for that, but I did find some workarounds like:
http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android
http://stackoverflow.com/questions/3793093/android-edittext-soft-keyboard-show-hide-event
Well, there are some workarounds that were already posted but they are incomplete (some work on 4.0.4 but not 2.3, some don't work all the time), but at least they are still useful, they gave me an idea to make this "complete" workaround (well, at least it works like a charm in my case for now, I tested on both Android 2.3 and 4.0.4), so I would like to share it here, hope it helps someone out there ;)
Usage: just use following LinearLayout overridden class as the main layout(and of course, use "match_parent" for its layout_width and layout_height) of the activity where you want to catch soft keyboard show/hide events, then setOnSoftKeyboardVisibilityChangeListener for your main layout. Finally, you need this line in your <activity> tag (in manifest.xml) where SoftKeyboardHandledLinearLayout is used:
android:windowSoftInputMode="adjustResize"
Take a look at following code to get a better idea of how to implement this hack.
Hi, how do u catch it? Pls show me
ReplyDeleteHey, thank you for reminding me about this post, I just updated code and usage, take a look ;)
DeleteHey LeoLink,
ReplyDeleteI tested your solution and in my settings I do not get event in onMeasure.
do you use special setting in the activity manifest?
I want to go with, android:windowSoftInputMode="adjustNothing", and for that settings it's not enough.
Cheers,
Patryk
oh sorry, I forgot to mention you have to have this in your
Deleteandroid:windowSoftInputMode="adjustResize"
Very useful, I'm using with a RelativeLayout instead of LinearLayout, onSoftKeyboardShow is working good but onSoftKeyboardHide is never called, do you have any idea why?
ReplyDeleteThanks
Well, this is kind of tricky, and I'm not even sure that if can work on all devices, but I don't think there is differences between using RelativeLayout and LinearLayout, maybe you should check following things up:
Delete- Did you add android:windowSoftInputMode="adjustResize" to your AndroidManifest.xml
- Are your RelativeLayout's width and height both "match_parent"
- Does "event.getKeyCode()" really return KeyEvent.KEYCODE_BACK when you press back button
That's all I can think of right now, good luck ;)
This comment has been removed by the author.
ReplyDeleteHi, if i have an hybrid app and have only webViewExtended with javascript code?
ReplyDeletemany thanks
Thank you,in my app,work nice
ReplyDeleteonSoftKeyboardShow() not work if user don't press BACK key.
ReplyDeleteVery, very nice!!!!
ReplyDeleteThanks for much!!!))))
onSoftKeyboardShow() not work if user don't press BACK key.
ReplyDeleteIn the event that you are an entrepreneur with an item or administration to advance yet you likewise wish to by and by appreciate the advantages of Facebook.how to figure out someones facebook password
ReplyDelete