Android: AutoCompleteTextView show suggestions when no text entered

AndroidAutocompletetextview

Android Problem Overview


I am using AutoCompleteTextView, when the user clicks on it, I want to show suggestions even if it has no text - but setThreshold(0) works exactly the same as setThreshold(1) - so the user has to enter at least 1 character to show the suggestions.

Android Solutions


Solution 1 - Android

This is documented behavior:

> When threshold is less than or equals 0, a threshold of 1 is > applied.

You can manually show the drop-down via showDropDown(), so perhaps you can arrange to show it when you want. Or, subclass AutoCompleteTextView and override enoughToFilter(), returning true all of time.

Solution 2 - Android

Here is my class InstantAutoComplete. It's something between AutoCompleteTextView and Spinner.

import android.content.Context;  
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class InstantAutoComplete extends AutoCompleteTextView {

	public InstantAutoComplete(Context context) {
		super(context);
	}

	public InstantAutoComplete(Context arg0, AttributeSet arg1) {
		super(arg0, arg1);
	}

	public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
		super(arg0, arg1, arg2);
	}

	@Override
	public boolean enoughToFilter() {
		return true;
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
		if (focused && getAdapter() != null) {
			performFiltering(getText(), 0);
		}
	}

}

Use it in your xml like this:

<your.namespace.InstantAutoComplete ... />

Solution 3 - Android

Easiest way:

Just use setOnTouchListener and showDropDown()

AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event){
      text.showDropDown();
      return false;
   }
});

Solution 4 - Android

Destil's code works just great when there is only one InstantAutoComplete object. It didn't work with two though - no idea why. But when I put showDropDown() (just like CommonsWare advised) into onFocusChanged() like this:

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused) {
        performFiltering(getText(), 0);
        showDropDown();
    }
}

it solved the problem.

It is just the two answers properly combined, but I hope it may save somebody some time.

Solution 5 - Android

The adapter does not perform filtering initially.
When the filtering is not performed, the dropdown list is empty.
so you might have to get the filtering going initially.

To do so, you can invoke filter() after you finish adding the entries:

adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);

Solution 6 - Android

You can use onFocusChangeListener;

TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus) {
				TCKimlikNo.showDropDown();

			}

		}
	});

Solution 7 - Android

Destil's answer above almost works, but has one subtle bug. When the user first gives focus to the field it works, however if they leave and then return to the field it will not show the drop down because the value of mPopupCanBeUpdated will still be false from when it was hidden. The fix is to change the onFocusChanged method to:

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused) {
        if (getText().toString().length() == 0) {
    		// We want to trigger the drop down, replace the text.
    		setText("");
        }
    }
}

Solution 8 - Android

Just call this method on touch or click event of autoCompleteTextView or where you want.

autoCompleteTextView.showDropDown()

Solution 9 - Android

To make CustomAutoCompleteTextView.

  1. override setThreshold,enoughToFilter,onFocusChanged method

    public class CustomAutoCompleteTextView extends AutoCompleteTextView {

     private int myThreshold; 
     
     public CustomAutoCompleteTextView  (Context context) { 
     	super(context); 
     } 
    
     public CustomAutoCompleteTextView  (Context context, AttributeSet attrs, int defStyle) { 
     	super(context, attrs, defStyle); 
     } 
    
     public CustomAutoCompleteTextView  (Context context, AttributeSet attrs) { 
     	super(context, attrs); 
     } 
      //set threshold 0.
     public void setThreshold(int threshold) { 
     	if (threshold < 0) { 
     		threshold = 0; 
     	} 
     	myThreshold = threshold; 
     } 
     //if threshold   is 0 than return true
     public boolean enoughToFilter() { 
     	 return true;
     	} 
     //invoke on focus 
     protected void onFocusChanged(boolean focused, int direction,
     		Rect previouslyFocusedRect) {
     				//skip space and backspace 
     	super.performFiltering("", 67);
     	// TODO Auto-generated method stub
     	super.onFocusChanged(focused, direction, previouslyFocusedRect);
     	
     }
    
     protected void performFiltering(CharSequence text, int keyCode) {
     	// TODO Auto-generated method stub
     	super.performFiltering(text, keyCode);
     }
    
     public int getThreshold() { 
     	return myThreshold; 
     } 
    

    }

Solution 10 - Android

try it

    searchAutoComplete.setThreshold(0);
    searchAutoComplete.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//cut last probel
                    if (charSequence.length() > 1) {
                        if (charSequence.charAt(charSequence.length() - 1) == ' ') {
                            searchAutoComplete.setText(charSequence.subSequence(0, charSequence.length() - 1));
                            searchAutoComplete.setSelection(charSequence.length() - 1);
                        }
                    }
                   }
    
    
                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
    
    
    //when clicked in autocomplete text view
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
              case R.id.header_search_etv:
                    if (searchAutoComplete.getText().toString().length() == 0) {
                        searchAutoComplete.setText(" ");
                    }
             break;
            }
        }):

Solution 11 - Android

on FocusChangeListener, check

if (hasFocus) {
            tvAutoComplete.setText(" ")

in your filter, just trim this value:

filter { it.contains(constraint.trim(), true) }

and it will show all suggestion when you focus on this view.

Solution 12 - Android

This worked for me, pseudo code:

    public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            performFiltering(getText(), 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.showDropDown();
        return super.onTouchEvent(event);
    }
}

Solution 13 - Android

Just paste this to your onCreate Method in Java

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            getResources().getStringArray(R.array.Loc_names));

    textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
    textView1.setAdapter(arrayAdapter);
    
    textView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View arg0) {
            textView1.setMaxLines(5);
            textView1.showDropDown();

        }
    });

And this to your Xml file...

<AutoCompleteTextView
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:hint="@string/select_location"
            android:id="@+id/acT1"
            android:textAlignment="center"/>

And create an Array in string.xml under Values...

<string-array name="Loc_names">

        <item>Pakistan</item>
        <item>Germany</item>
        <item>Russia/NCR</item>
        <item>China</item>
        <item>India</item>
        <item>Sweden</item>
        <item>Australia</item>
    </string-array>

And you are good to go.

Solution 14 - Android

Seven years later, guys, the problem stays the same. Here's a class with a function which forces that stupid pop-up to show itself in any conditions. All you need to do is to set an adapter to your AutoCompleteTextView, add some data into it, and call showDropdownNow() function anytime.

Credits to @David Vávra. It's based on his code.

import android.content.Context
import android.util.AttributeSet
import android.widget.AutoCompleteTextView

class InstantAutoCompleteTextView : AutoCompleteTextView {

    constructor(context: Context) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun enoughToFilter(): Boolean {
        return true
    }

    fun showDropdownNow() {
        if (adapter != null) {
            // Remember a current text
            val savedText = text

            // Set empty text and perform filtering. As the result we restore all items inside of
            // a filter's internal item collection.
            setText(null, true)

            // Set back the saved text and DO NOT perform filtering. As the result of these steps
            // we have a text shown in UI, and what is more important we have items not filtered
            setText(savedText, false)

            // Move cursor to the end of a text
            setSelection(text.length)

            // Now we can show a dropdown with full list of options not filtered by displayed text
            performFiltering(null, 0)
        }
    }
}

Solution 15 - Android

For me, It is :

 autoCompleteText.setThreshold(0);

do the trick.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfhuchoView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidDavid VávraView Answer on Stackoverflow
Solution 3 - Androiduser1913469View Answer on Stackoverflow
Solution 4 - AndroidalexView Answer on Stackoverflow
Solution 5 - Androiddavid m leeView Answer on Stackoverflow
Solution 6 - AndroidGöksel GürenView Answer on Stackoverflow
Solution 7 - AndroidColin StewartView Answer on Stackoverflow
Solution 8 - AndroidDalvinder SinghView Answer on Stackoverflow
Solution 9 - Androidsanjeev vishnoiView Answer on Stackoverflow
Solution 10 - AndroidRafayel PogosyanView Answer on Stackoverflow
Solution 11 - AndroidbeokhView Answer on Stackoverflow
Solution 12 - AndroidNasif Md. TanjimView Answer on Stackoverflow
Solution 13 - AndroidLalit FauzdarView Answer on Stackoverflow
Solution 14 - AndroidmykolajView Answer on Stackoverflow
Solution 15 - AndroidMingyong GuView Answer on Stackoverflow