Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard hide event with BACK key

I've noticed in the Android Market Application, when you click over the search button, it shows the keyboard, but when you click the back button, the search EditText becomes invisible and the keyboard is hidden. The problem is that I can't hide the EditText after the keyboard is hidden after pressing the back key because I can't find a listener for hiding the keyboard event. I found this sample How to capture the "virtual keyboard show/hide" event in Android? but it doesn't work on the soft keyboard.

like image 246
Mohammad Ersan Avatar asked Oct 03 '11 11:10

Mohammad Ersan


People also ask

How do I hide the activity on my keyboard?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.

How to handle the keyboard?

To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.


2 Answers

You need to implement this to capture the BACK button before it is dispatched to the IME:

http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int, android.view.KeyEvent)

like image 60
hackbod Avatar answered Oct 13 '22 07:10

hackbod


I think you should handle this using focus:

 final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    edttext.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!(hasFocus))
            {   
            mgr.hideSoftInputFromWindow(edttext.getWindowToken(), 0);
            }

        }
    });
like image 32
Hanry Avatar answered Oct 13 '22 08:10

Hanry