Handle soft input keyboard backpress like a pro in android development
Welcome Welcome Welcome! Cheers to another post about the evergreen android development world
Recently, this very problem came in my hand where I need to call an event when the user pressed the clear text button on the keyboard, After brainstorming many posts and articles, I came up with a very interesting yet very easy approach to handle such cases, So without further a due,
Let’s Get Started ~~~
Now take a very close look at the very existing interface in Edittext which is our very own TextChangeListener
on beforeTextChanged , When the user presses the key, we get the last text(s), For example, if the last text is “Shi” and now user pressed a key ‘v’ then we get ‘Shi’ in the (s) parameter, the start will be the length of the string, in this case, will be 3 before the new character pressed and the count will always be 0, See some logs sample
Our Logger ~
Log.d("beforeTextChanged","::S "+ s)
Log.d("beforeTextChanged","::Start "+ start)
Log.d("beforeTextChanged","::Count "+ count)
When the user pressed ‘S’
beforeTextChanged: ::S
beforeTextChanged: ::Start 0
beforeTextChanged: ::Count 0
Above the previous text was empty so we receive an empty string “ ”, be known that S is the part of our logger and start 0 when we started the empty string and count will always be 0 here
When the user pressed ‘h’
beforeTextChanged: ::S S
beforeTextChanged: ::Start 1
beforeTextChanged: ::Count 0
Above the previous text was not empty so we receive “S ”, and start 1 as when we started the string length of one and count will always be 0 here
When the user pressed ‘i’
beforeTextChanged: ::S Sh
beforeTextChanged: ::Start 2
beforeTextChanged: ::Count 0
Above the previous text was empty so we receive “Sh”, and start 2 as when we started the string length of two and count will always be 0 here
But what happens when the back button is pressed, See the logs ~
beforeTextChanged: ::S Shi
beforeTextChanged: ::Start 0
beforeTextChanged: ::Count 3
Above the previous text was “Shi”, and start 0 as when we have cleared the string and count will be 3, Now this count we receive in onTextChangedMethod as before
In onTextChanged , When the user presses the key, we get the current text(s), For example, if the last text is “Shi” and now the user pressed a key ‘v’ then we get ‘Shiv’ in the (s) parameter, Now here we receive start, count and before
the start will be the length of the string same as above and count will be one , as we have inputted one character in the keyboard and before will always be 0 until the user presses the clear text button
See some logs sample ~
Our logger ~
Log.d("onTextChanged","::S "+ s)
Log.d("onTextChanged","::Start"+ start)
Log.d("onTextChanged","::Before"+ before)
Log.d("onTextChanged",":: Count"+ count)
Now when the user presses the back button ~
onTextChanged: ::S Sh
onTextChanged: ::Start 0
onTextChanged: ::Before 3
onTextChanged: :: Count 2
Above the current text which is “Sh”, and start 0 as when we have cleared the string and count will be 2 (current length of the string) and in before we receive the previous length of string which was 3
So we can clearly put our logic code here ~
if (before > count) { //onTextChanged//Your logic when user the clear text button}
So simple! isn’t it. Thanks For Reading
Stay tuned for more :)