I would like to alter the layout based on whether the virtual keyboard is shown or not. I've searched the API and various blogs but can't seem to find anything useful.
Is it possible?
Thanks!
I would like to alter the layout based on whether the virtual keyboard is shown or not. I've searched the API and various blogs but can't seem to find anything useful.
Is it possible?
Thanks!
Based on the Code from Nebojsa Tomcic I've developed the following RelativeLayout-Subclass:
This works quite fine... Mark, that this solution will just work when Soft Input Mode of your Activity is set to "WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE"
Like @amalBit's answer, register a listener to global layout and calculate the difference of dectorView's visible bottom and its proposed bottom, if the difference is bigger than some value(guessed IME's height), we think IME is up:
the height threshold 100 is the guessed minimum height of IME.
This works for both adjustPan and adjustResize.
I solve this by overriding onKeyPreIme(int keyCode, KeyEvent event) in my custom EditText.
I have sort of a hack to do this. Although there doesn't seem to be a way to detect when the soft keyboard has shown or hidden, you can in fact detect when it is about to be shown or hidden by setting an
OnFocusChangeListener
on theEditText
that you're listening to.NOTE: One thing to be aware of with this hack is that this callback is fired immediately when the
EditText
gains or loses focus. This will actually fire right before the soft keyboard shows or hides. The best way I've found to do something after the keyboard shows or hides is to use aHandler
and delay something ~ 400ms, like so:Hide|Show events for keyboard can be listened through simple hack in OnGlobalLayoutListener :
Here activityRootView is your Activity's root view.
If you want to handle show/hide of IMM (virtual) keyboard window from your Activity, you'll need to subclass your layout and override onMesure method(so that you can determine the measured width and the measured height of your layout). After that set subclassed layout as main view for your Activity by setContentView(). Now you'll be able to handle IMM show/hide window events. If this sounds complicated, it's not that really. Here's the code:
main.xml
Now inside your Activity declare subclass for your layout (main.xml)
You can see from the code that we inflate layout for our Activity in subclass constructor
And now just set content view of subclassed layout for our Activity.