Search code examples
androidandroid-adapterandroid-softkeyboardautocompletetextview

How to hide keyboard after the user is choose "something" from AutoCompleteTextView?


I have a AutoCompleteTextView, when the user is typing he get the keyboard. after, the user choose from AutoCompleteTextView what is need, but the keyboard is stay and not hide.

This is my Layout.xml code:

 <AutoCompleteTextView
            android:id="@+id/autocompleteTextView"
            android:layout_width="300dp"
            android:layout_marginLeft="33dp"
            android:layout_height="match_parent"
            android:hint="  Choose what you need"
            android:ems="3"
            android:maxLength="30"
            android:backgroundTint="#FFFFFF"/>

this is my java code:

 mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
 mAutocompleteView.setAdapter(mAdapter);

what I should do if I want to hide the keyboard after choose something from Autocomplete?

thanks for help.


Solution

  • Try this this will sure work for you

        AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);
    
        text.setOnItemClickListener(new OnItemClickListener() {
    
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
    
      }
    
    });
    

    or, you can use this :

     AutoCompleteTextView autoText= (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);
    
    autoText.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    
        }
      }
    });