ChrisGeo
ChrisGeo

Reputation: 3907

Android: AutoCompleteTextView hide soft keyboard

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>

Upvotes: 16

Views: 37069

Answers (13)

Alexandre Bianchi
Alexandre Bianchi

Reputation: 334

Just set XML with

android:inputType="none" 

and use the code below to configure the component programatically:

  autoCompleteTextView.setOnFocusChangeListener { _, hasFocus ->
    if (hasFocus) {
        val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(autoCompleteTextView.windowToken, 0)
    }
  }

Upvotes: 0

Afanasyeu Alexei
Afanasyeu Alexei

Reputation: 147

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)
   }
}

Upvotes: 0

UzumakiL
UzumakiL

Reputation: 403

autoCompleteTextView.setInputType(InputType.TYPE_NULL);

Upvotes: 1

Roar Gr&#248;nmo
Roar Gr&#248;nmo

Reputation: 3976

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

Upvotes: 8

machfour
machfour

Reputation: 2709

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

Upvotes: 1

chakri Reddy
chakri Reddy

Reputation: 2920

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);
} });

Upvotes: 0

Shailendra Madda
Shailendra Madda

Reputation: 21551

As chakri Reddy Suggested:

It's working for me I just replaced

this:

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

with:

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

Upvotes: 17

user948620
user948620

Reputation:

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);

Upvotes: 28

Someone Somewhere
Someone Somewhere

Reputation: 23787

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) { }
    });

Upvotes: 0

Ashwin H
Ashwin H

Reputation: 723

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);

    }
  }
});

Upvotes: 1

Carlos Borau
Carlos Borau

Reputation: 1473

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.

Upvotes: 6

Alex
Alex

Reputation: 19

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;
    }

Upvotes: 1

Erkki Nokso-Koivisto
Erkki Nokso-Koivisto

Reputation: 1275

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;
}

Upvotes: 9

Related Questions