Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView: Remove soft keyboard on back press instead of suggestions

Tags:

When using AutoCompleteTextView, the dropdown suggestion list appears with the software keyboard still visible. This makes sense, as it is often a lot more efficient to type ensuing characters to narrow the list.

But if the user wants to navigate the suggestion list, it becomes extremely tedious with the software keyboard still up (this is even more of a problem when the device is in landscape orientation). Navigating the list is a lot easier without the keyboard hogging the screen space. Unfortunately, the default behaviour removes the list first when you press the back key (even though in the software versions of the back key it is showing the image that says 'pressing this will hide the keyboard').

Here's a barebones example that demonstrates what I'm talking about:

public class Main2 extends Activity {     private static final String[] items = {             "One",             "Two",             "Three",             "Four",             "Five"     };      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          AutoCompleteTextView actv = new AutoCompleteTextView(this);         actv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));         actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));         actv.setThreshold(1);          LinearLayout ll = new LinearLayout(this);         ll.setOrientation(LinearLayout.VERTICAL);         ll.addView(actv);          setContentView(ll);     } } 

Besides the fact that this is unintuitive (the back key hint is suggesting that the back press will be sent to the keyboard), it makes navigating AutoCompleteTextView suggestions extremely tiresome.

What is the least intrusive way (e.g. catching the back in on onBackPressed() in every activity and routing it accordingly would definitely not be ideal) to make the first back press hide the keyboard, and the second remove the suggestion list?

like image 927
btalb Avatar asked Nov 23 '14 07:11

btalb


2 Answers

You can achieve that by override-ing onKeyPreIme in your custom AutoCompleteTextView.

public class CustomAutoCompleteTextView extends AutoCompleteTextView {      public CustomAutoCompleteTextView(Context context) {         super(context);     }      public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {         super(context, attrs);     }      public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);     }      @Override     public boolean onKeyPreIme(int keyCode, KeyEvent event) {         if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {             InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                          if (inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),                 InputMethodManager.HIDE_NOT_ALWAYS)) {                     return true;             }         }          return super.onKeyPreIme(keyCode, event);     }  } 
like image 112
Mostafa Gazar Avatar answered Oct 22 '22 12:10

Mostafa Gazar


set DismissClickListener like this

 autoCompleteTextView.setOnDismissListener(new AutoCompleteTextView.OnDismissListener() {             @Override             public void onDismiss() {                 InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                 in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);             }         }); 
like image 36
Krishna Meena Avatar answered Oct 22 '22 11:10

Krishna Meena