Hide keyboard when choosing text in Autocomplete

Asked

Viewed 160 times

1

I’m using elements of the type Autocomplete in the project, so that when the user type part of the text they search, it presents the suggested options for the same.

For this, I am using the following excerpt:

private void arrayAutoComplete() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_expandable_list_item_1, produto);
    AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.editProduto);
    textView.setAdapter(adapter);
}

private static final String[] produto = new String[]{
        "Skol beats sensations", "Brahma", "Skol litrão", "Heinekein", "Skol 350 ml"
};

I want to know, how do I hide the smartphone keyboard when the user selects one of the suggested texts.

1 answer

1

To perform an action when selecting an item, the Listener AdapterView.OnItemClickListener. So you can try something like this:

textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.