Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keyboard when down arrow is pressed

Tags:

android

The picture shows a part of my app, an AutoCompleteTextView with an attached adapter. When the user enters something into that view, autocomplete suggestions are shown.

The problem I have is: when the suggestions are shown and the device's down arrow is pressed, only the suggestions from the AutoCompleteTextView are closed, the keyboard stays open and needs a second tap on the down arrow to disappear.

I do want the suggestions and the keyboard to disappear on the first tap on the down arrow.

I tried overriding onBackPressed but it is not called when the down arrow is tapped, presumably because it's not considered 'back'.

How could I do this?

EDIT: I know how to programmatically hide the keyboard, I guess my problem is to detect the 'down arrow' tap.

enter image description here

like image 645
fweigl Avatar asked May 13 '15 07:05

fweigl


1 Answers

Try to override onKeyPreIme() method in your AutoCompleteTextView as follows:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
        super.onKeyPreIme(keyCode, event);
        hideKeyboard()
        return true;
    }
    return super.onKeyPreIme(keyCode, event);
}
like image 101
Oleg Osipenko Avatar answered Oct 08 '22 15:10

Oleg Osipenko