Figen Güngör
Figen Güngör

Reputation: 12559

AutoCompleteTextView doesn't work when I enter first character

My AutoCompleteTextView doesn't work when I enter first character in textbox but starts showing dropdown when I enter second character. What could be the reason?

<AutoCompleteTextView
    android:id="@+id/autocomplete_name"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="7"
    android:background="@drawable/edittextback"
    android:ems="10"
    android:textSize="15sp"
    android:hint="@string/codehint"
    android:textColorHint="@color/hintgrey"
    android:dropDownWidth="fill_parent"
    android:paddingRight="30dp"
    android:paddingLeft="10dp"
    android:singleLine="true"
    android:ellipsize="end"
/>

Upvotes: 17

Views: 12179

Answers (4)

Javid Sattar
Javid Sattar

Reputation: 846

If you want after clicking on MaterialAutoCompleteTextView Open the dialog

you must set the completionThreshold value to 0 And inputType value to none.

you can also increase the same field for the minimum number of characters.

don't forget set inputType to text or number

 <com.google.android.material.textfield.MaterialAutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:completionThreshold="0" <-- This line
                android:ems="10"
                android:inputType="none" />

I hope hep

Upvotes: 0

nobalG
nobalG

Reputation: 4620

You will need to set the completionThreshold property of your autoCompleteView to 1.

<AutoCompleteTextView 
    android:id="@+id/someID" 
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:completionThreshold="1" />

Or for doing it dynamically through code use

mAutoCompleteTextView.setThreshold(1)

Happy Coding :)

Upvotes: 60

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

use i java code

autoComplete.setThreshold(1);

or in xml

android:completionThreshold="1"

Upvotes: 2

koutuk
koutuk

Reputation: 832

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {

     private AutoCompleteTextView autoComplete;
     private MultiAutoCompleteTextView multiAutoComplete;
     private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the defined string-array 
        String[] colors = getResources().getStringArray(R.array.colorList);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,colors);

        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);

        // set adapter for the auto complete fields
        autoComplete.setAdapter(adapter);
        multiAutoComplete.setAdapter(adapter);

        // specify the minimum type of characters before drop-down list is shown
        autoComplete.setThreshold(1);
        multiAutoComplete.setThreshold(2);
        // comma to separate the different colors
        multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        // when the user clicks an item of the drop-down list
        multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                 Toast.makeText(getBaseContext(), "MultiAutoComplete: " +
                            "you add color "+arg0.getItemAtPosition(arg2),
                            Toast.LENGTH_LONG).show();
            }
        });
    }

}

Upvotes: 0

Related Questions