In an application with shopping cart, I give the option to change the amount of items via an EditText which allows only numerical input.
Everything works fine, except when the user changes the field and then presses the back key to hide the soft keyboard. In this case, the field shows the changed value, but I don't know how I can detect this change and react on it. Waiting for a switch to another activity is not an option.
When the user confirms with "done" button, I can handle this with a "OnEditorActionListener". But what about the back key?
update:
As it turned out, neither onKeyDown / onBackPressed nor OnKeyListener on the edit field do trigger when closing the soft keyboard with back key.
I had the same problem in an application which i wrote some time ago. It is discontiuned now but that's not your question xD.
In fact there is no option to track such a operation, but i've found a great (more or less) solution to add such a functionality. It's quite simple in theory but it's a "hack" too i think.
So what you will need is a custom linear layout which contains your application, or a special region. After that you have to add it a listener. And this will work only in portrait mode.
So here to the code: (i'm sorry but i can't remember the source)
The custom layout:
LinearLayoutThatDetactsSoftwarekeyboard.java (that's the original name of the layout xD)
And how you add the listener:
The LinearLayout adds a Listener which will be called each time the layoutheight changes by at least 128 pixels. It's a trick and it won't work with keyboards that are smaller than 128 pixels (but i think each keyboard has such a height) If the LayoutHeight has Changed you will get notified if it's showing now or not.
I hope my answer was useful. Perhaps you find the real source here on StackOverFlow again. I won't steal someones genius so. Credits goes to the unknown person ;)
With the new information of the code being in a fragment, I would suggest adding a key listener to the text box. This code hasn't been tested, but should work.
Excuse me if this is not relevant but I once had this problem and forgot that I had added an OnKeyListener that caught the on back pressed for my dialogFragment. I rewrote the listener as follows. The Top condition was added to solve the problem.
You can override the onBackPressed in your activity (pseudo-code - be warned):
This is the modified version of @mikepenz's answer, since it didn't work on Android 8 with windowSoftInputMode="adjustPan" mode.
The language is kotlin. Use this view as root view and set the keyboardListener: