How can I catch the event when the user, after he entered the text on an EditText, select Back and the keyboard disappear?
Asked
Active
Viewed 653 times
1
-
Can you make it a little clear, whether you want to handle the back pressed event or edittext on focus after the soft keyboard being hidden. – Andro Selva Aug 05 '11 at 12:54
-
Sounds like you don't really care about the back button but about if the soft keyboard is visible or not, in that case I suggest you check out http://stackoverflow.com/questions/2150078/android-is-software-keyboard-shown – Zharf Sep 01 '11 at 12:51
3 Answers
4
Override onBackPressed at your Activity class:
@Override
public void onBackPressed() {
//If keyboard is on, do what you want
super.onBackPressed();
}

Maxim
- 2,996
- 17
- 19
-
-
ok, In this case override onKeyDown(keyCode, event) and procceed if keyCode == KeyEvent.KEYCODE_BACK – Maxim Aug 05 '11 at 13:03
-
like I sad before, after the user has finished entering text in Edittext he press back and keyboard disappear but the method is not called...it is called second time when button back is pressed – Gabrielle Aug 05 '11 at 14:20
-
Sorry for my prompt answer. It seems that when SoftKey is opened it catches keyBack event before Activity can handle it. And thanks for the interesting question. – Maxim Aug 05 '11 at 14:39
-
1
I had the same problem. Vineet Shukla's answer was not working, until I made sure that the delegate was a EditText.OnKeyListener. Prior to that it was a View.OnKeyListener and I was not seeing KeyEvent.KEYCODE_BACK == keyCode, for was I even seeing onKey ever called. I hope this is helpful to someone having a similar problem, although this post is a year old. Cheers.

HeadNinja
- 69
- 1
- 9
1
If you want to catch the back key when user has finished entering text in EditText and he presses back key then you should use:
EditText edit = (EditText)findViewById(R.id.yourId);
edit.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
System.out.println("******back key caught in edit.setOnKeyListener");
}
return false;
}
});

Vineet Shukla
- 23,865
- 10
- 55
- 63
-
It is not working...this I'm trying to do but in DDMS doesn't appear nothing – Gabrielle Aug 05 '11 at 13:07
-
I have tested it and it is working. You should debug and see if you are getting a call there.... – Vineet Shukla Aug 05 '11 at 13:09