0

In my app, I have an edit text which, when pressed, sets an int value to "1". Obviously, when you press the edit text, a keyboard pops up. The keyboard is dismissed by pressing back, and I have used the following code...

@Override
public void onBackPressed() {

    if (editingText == 1) {
        editingText = 0;
        setUnit();
    }

    Log.d("Back pressed", "Back was pressed :)");

    super.onBackPressed();
}  

... to change the value back. setUnit() then changes the value of a TextViewH depending on the contents of the EditText. However, as I have seen by using the logs, this method is only called when the user backs out of the entire activity, and not when the user backs out of the keyboard.

Is there any way to detect "back" presses when the keyboard is displayed? Or, alternatively, is it possible to change a string's value to match the value of the edit text in real-time? All help appreciated.

  • Did you try overriding onKeyEvent in your activity?You can get the back key event in it – Ajit Pratap Singh Sep 01 '14 at 15:32
  • Have a look at this: http://stackoverflow.com/questions/5113437/get-back-key-event-on-edittext – Francesco verheye Sep 01 '14 at 15:37
  • If Francesco verheye's answer is what you mean, Ajit, unfortunately it didn't work. Not sure if this method's deprecated or something, but I put a log in it, and when I clicked back out of the EditText, no log appeared, and the value didn't change. Any other ideas? – Isaac 'Criterion' Akalanne Sep 01 '14 at 16:06

0 Answers0