Hide keyboard when choosing text in AutoComplete

1

I'm using elements of type AutoComplete in the project, so when the user types part of the text they are looking for, it presents the suggested options for it.

For this, I'm using the following snippet:

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 to hide the smartphone's keyboard when the user selects one of the suggested texts.

    
asked by anonymous 20.04.2015 / 19:48

1 answer

1

To perform an action when selecting an item, the listener is used 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);
    }
});
    
20.04.2015 / 20:02