16

I have an AutoCompleteTextView which as usual provides suggestions after a user types 3 letters. I want once once I touch the suggestion list to hide the soft keyboard. What I have done below with the table layout hides the keyboard only when clicking anywhere but the suggestion list.

XML

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <AutoCompleteTextView
        android:id="@+id/auto_insert_meds"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="15"
        android:inputType="textVisiblePassword|textMultiLine"
        android:scrollHorizontally="false"
        android:text=""
        android:textSize="16sp" />
</TableRow>

Java

TableLayout tbl = (TableLayout) findViewById(R.id.main_table);
tbl.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        in.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return true;
    }
});

XML for custom list

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/medlist_linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TextView
        android:id="@+id/med_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollHorizontally="false"
        android:padding="3dp"
        android:textColor="@android:color/white" />

</LinearLayout>
Nipun
  • 624
  • 1
  • 14
  • 21
ChrisGeo
  • 3,645
  • 11
  • 51
  • 85

12 Answers12

26

Use OnItemClickListener. Hope this works :)

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.getWindowToken(), 0);

  }

});

UPDATED

Use this

in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);

instead of -

in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
Raj
  • 2,797
  • 2
  • 9
  • 25
17

As chakri Reddy Suggested:

It's working for me I just replaced

this:

 in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);

with:

 in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
AskNilesh
  • 58,437
  • 15
  • 99
  • 129
Shailendra Madda
  • 16,925
  • 14
  • 71
  • 115
9

1) Change AutoCompleteTextView dropdown height to MATCH_PARENT

setDropDownHeight(LinearLayout.LayoutParams.MATCH_PARENT);

2) Dismiss soft keyboard by overriding getView() of AutoCompleteTextView adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = super.getView(position, convertView, parent);
    v.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                InputMethodManager imm = (InputMethodManager) getActivity()
                        .getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        typeTextView.getWindowToken(), 0);
            }

            return false;
        }
    });

    return v;
}
  • Just use... imm.hideSoftInputFromWindow(typeTextView.getApplicationWindowToken(), 0); In place of.. imm.hideSoftInputFromWindow(typeTextView.getWindowToken(), 0); It Will work... :) – chakri Reddy Feb 03 '16 at 12:47
5

Found this chunk of code in a similar thread. Hope it helps:

    private void hideKeyboard() {   
        // Check if no view has focus:
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
}

Link to the original post. Note that it is not the accepted answer.

Community
  • 1
  • 1
Carlos Borau
  • 1,334
  • 1
  • 22
  • 29
4

If you need to hide keyboard even if tapped / clicked (in a dropdown scenario with DropDown menu (TextInputLayout + MaterialAutoCompleteTextView) to prevent keyboard appearing when selecting from a predefined drowpdown)

(kotlin)

autoCompleteTextView.inputType = InputType.TYPE_NULL

RG

Roar Grønmo
  • 977
  • 7
  • 20
1

In case this helps anyone, I had a similar situation.

My AutoCompleteTextView was displayed on a DialogFragment with no Title Bar because I used getWindow().requestFeature(Window.FEATURE_NO_TITLE); in onCreateDialog()

I added the following line after FEATURE_NO_TITLE and the results list displayed over the keyboard:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialogo=super.onCreateDialog(savedInstanceState);
        dialogo.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialogo.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        return dialogo;
    }
Volker E.
  • 5,443
  • 11
  • 43
  • 62
Alex
  • 19
  • 1
1
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);

    }
  }
});
slfan
  • 8,209
  • 115
  • 61
  • 73
Ashwin H
  • 549
  • 6
  • 19
1

I worked all day on this and managed to develop a custom View that extends (Material)AutoCompleteTextView and uses reflection to set an OnTouchListener on the popup window to hide the keyboard when it's touched, so that you can keep typing in the TextView until you are ready to scroll through the suggestions.

Hopefully someone finds it useful.

https://gist.github.com/MachFour/c6b5252292dd3bfe18d6b1141f137d32

machfour
  • 352
  • 1
  • 7
1
autoCompleteTextView.setInputType(InputType.TYPE_NULL);
UzumakiL
  • 313
  • 2
  • 9
0

Try this guys..It will work..!!!

 AutoCompleteTextView auto_text = (AutoCompleteTextView)findViewById(R.id.auto_insert_meds);
 auto_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);
} });
chakri Reddy
  • 2,900
  • 1
  • 13
  • 12
0

I used the TextWatcher already to detect when the user has typed in at least 3 letters (which triggers a download API) as follows:

    searchAutoComplete.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // when the user has clicked on an item in the list, it will trigger onTextChanged.
            // To avoid querying the server and showing the dropdown again, use searchAutoComplete.isPerformingCompletion()
            if (!searchAutoComplete.isPerformingCompletion()){
                if (s != null && s.length() >= 3) {
                    downloadSearchResults(s.toString());
                } else {
                    searchAutoComplete.dismissDropDown();// hide dropdown after user has deleted characters and there's less than 3 visible
                }
            } else {
                // user has clicked on a list item so hide the soft keyboard
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (in != null) in.hideSoftInputFromWindow(searchAutoComplete.getApplicationWindowToken(), 0);
            }
        }

        @Override
        public void afterTextChanged(Editable s) { }
    });
Someone Somewhere
  • 22,369
  • 11
  • 111
  • 155
0

better solution is observing focus. hide keyboard in OnItemClickListener hide keyboard only if choose the item, but focus from previous element(EditText for example) lost and carriage lost but keyboard shows. here code in Kotlin

yourAutoCompleteTextView.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus) {
        val inputMethodManager = v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(v.applicationWindowToken, 0)
   }
}