I have an Activity
where there are 5 EditText
s. When the user clicks on the first EditText
, the soft keyboard opens to enter some value in it. I want to set some other View
's visibility to Gone
when the soft keyboard opens and also when the user clicks on the first EditText
and also when the soft keyboard closes from the same EditText
on the back button press. Then I want to set some other View
's visibility to visible.
Is there any listener or callback or any hack for when the soft keyboard opens from a click on the first EditText
in Android?
If you can, try to extend EditText and override 'onKeyPreIme' method.
How can you extend it:
I think, that recalculating of screen height is not 100% successfully as mentioned before. To be clear, overriding of 'onKeyPreIme' is not called on 'hide soft keyboard programatically' methods, BUT if you are doing it anywhere, you should do 'onKeyboardHidden' logic there and do not create a comprehensive solutions.
I've modified the Jaap's accepted answer a bit. But in my case, there are few assumptions such as
android:windowSoftInputMode=adjustResize
and the keyboard does not show up at the beginning when the app starts. And also, I assume that the screen in regard matches the parent's height.contentHeight > 0
this check provides me to know if the regarding screen is hidden or shown to apply keyboard event listening for this specific screen. Also I pass the layout view of the regarding screen inattachKeyboardListeners(<your layout view here>)
in my main activity'sonCreate()
method. Every time when the height of the regarding screen changes, I save it toprevContentHeight
variable to check later whether the keyboard is shown or hidden.For me, so far it's been worked pretty well. I hope that it works for others too.