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?
You can try it:
This will work without any need to change your activity's
android:windowSoftInputMode
step 1: extend EditText class and override these two:
step 2: create these two in your activity:
*** remember in order to make
clearFocus
work, you have to make parent or first child in the parent hierarchy focusable.As Vikram pointed out in the comments, detecting whether the softkeyboard is shown or has disappeared is only possible with some ugly hacks.
Maybe it is enough to set a focus listener on the edittext:
"Jaap van Hengstum"'s answer is working for me, but there is no need to set "android:windowSoftInputMode" as he just said!
I've made it smaller(it now just detects what I want, actually an event on showing and hiding of keyboard):
and just don't forget to add this
Unfortunately I do not have a sufficiently high reputation to comment on Jaap van Hengstum's answer. But I read a few comments of people, having the problem that
contentViewTop
is always0
and thatonShowKeyboard(...)
is always called.I had the same issue and figured out the problem I had. I used an
AppCompatActivity
instead of a 'normal'Activity
. In this caseWindow.ID_ANDROID_CONTENT
refers to anContentFrameLayout
and not to theFrameLayout
with the right top-value. In my case it was fine to use the 'normal'Activity
, if you have to use another activity-type (I just tested theAppCompatActivity
, maybe it's also an issue with other acitivy-types like theFragmentActivity
), you have to access theFrameLayout
, which is an ancestor of theContentFrameLayout
.Use this class,
In
Android Manifest
,android:windowSoftInputMode="adjustResize"
is necessary.P.S - Completely taken from here.