Pages

5 Feb 2014

A hack to catch soft keyboard show/hide event on Android

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:

android:windowSoftInputMode="adjustResize"

Take a look at following code to get a better idea of how to implement this hack.

13 comments:

  1. Hi, how do u catch it? Pls show me

    ReplyDelete
    Replies
    1. Hey, thank you for reminding me about this post, I just updated code and usage, take a look ;)

      Delete
  2. Hey LeoLink,

    I 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

    ReplyDelete
    Replies
    1. oh sorry, I forgot to mention you have to have this in your

      android:windowSoftInputMode="adjustResize"

      Delete
  3. 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?
    Thanks

    ReplyDelete
    Replies
    1. 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:
      - 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 ;)

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi, if i have an hybrid app and have only webViewExtended with javascript code?

    many thanks

    ReplyDelete
  6. Thank you,in my app,work nice

    ReplyDelete
  7. onSoftKeyboardShow() not work if user don't press BACK key.

    ReplyDelete
  8. Very, very nice!!!!
    Thanks for much!!!))))

    ReplyDelete
  9. onSoftKeyboardShow() not work if user don't press BACK key.

    ReplyDelete
  10. In 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

Note: only a member of this blog may post a comment.