There are a lot of posts about finding a show/hide soft keyboard event. I find myself in a situation where I need to change an icon based on the soft key status, in a fragment.
I tried to implement onMeasure, but I can't override that in my fragment. Is there a (relative) painless way of getting a clean show/hide soft keyboard event in my fragment or should I abondon ship?
After some struggle I was able to abstract a method that I can call whenever I need to show or hide the keyboard without using the SHOW_FORCED that I saw in many answers where the result would be the keyboard open even on a new activity without text input.
I used this code inside onGlobalLayout to check if the keyboard is open or not and then in my method I decide if I want open or close.
Here is the code:
To check if it's open or not:
To perform the action that I need (here is where I call the above method):
To use, I just call like this:
Sadly but true - android do not have native on software keyboard show event.
One the way handle fact that keyboard is hidden is to check entered symbols and back button press (for example textEdit will receive even back button) - but it is not flexible enough solution.
Another the possible solutions is: Override onMeasure in activity and then notify observers (pattern Observer) - for example fragments. Fragment should subscribe and unsubscribe himself onPause onResume events. Something like that for activity code:
I'm not sure but as I remember it works only for LinearLayout and of course activity should have
adjustResize
flag set (programmatically or in manifeset)Another (I thing better approach) is to as here with globalTree observer
SoftKeyboard open and close listener in an activity in Android?