Suggestions not shown in AutoCompleteTextView

10,117

Solution 1

Do you miss autoCtextView.setThreshold(1); ?

(to start working from first character)

for example demo:

String[] strList={"a","aaa","aabb","b","bbc","cbb","c","cdd","caa","d","ddc","dda","e","eea","ebc","aec"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list
           ArrayAdapter<String> adapter = new ArrayAdapter<String>  
            (this,android.R.layout.select_dialog_item,strList);  

        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView autoCtextView= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           autoCtextView.setThreshold(1);         //to start working from first character  
           autoCtextView.setAdapter(adapter);//set the adapter data to the AutoCompleteTextView  




}  

Solution 2

After declare autocompleteTextView than fill the first adapter.

like Ref here

public class CountriesActivity extends Activity {
 protected void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.countries);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, COUNTRIES);
     AutoCompleteTextView textView = (AutoCompleteTextView)
             findViewById(R.id.countries_list);
     textView.setAdapter(adapter);
 }

 private static final String[] COUNTRIES = new String[] {
     "Belgium", "France", "Italy", "Germany", "Spain"
 };

}

Solution 3

In my fragment layout, if I don't use the ems attribute (field size in number of "m"s), the suggestions will not be shown. When it is present, the suggestions are shown.

<AutoCompleteTextView
        android:id="@+id/enter_activities_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:completionThreshold="1"/>

Solution 4

Do this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, android.R.layout.simple_list_item_1, namesLsist);
autoCtextView.setAdapter(adapter);

Before autoCtextView.addTextChangedListener(new TextWatcher() {...

Share:
10,117
user965071
Author by

user965071

Updated on June 14, 2022

Comments

  • user965071
    user965071 almost 2 years

    I have a AutoCompleteTextView in my layout. When a user enter "@" character in that i have to show them some suggestions. It normally names i get it from internet.

    I am getting the names and i create an ArrayAdapter as shown below.

    autoCtextView.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                String lsatChar = s.toString().substring(s.length()-1,s.length()); 
                if(lsatChar.equals("@")) {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, 
                             android.R.layout.simple_list_item_1, namesLsist);
                    autoCtextView.setAdapter(adapter);
                }
    
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
    
            }
        });
    

    But the suggestions are not shown. Am i doing anything wrong ? Please ask if need clarification on question

  • Zohra Khan
    Zohra Khan over 10 years
    @user965071 If you find something better than this answer, Please post it.
  • Alberto Cappellina
    Alberto Cappellina over 4 years
    that's my case. you saved me a lot of time, mate