Android: AutoCompleteTextView show suggestions when no text entered

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.


Asked by: Cherry904 | Posted: 20-01-2022






Answer 1

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.

Answered by: Emma526 | Posted: 21-02-2022



Answer 2

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 ... />

Answered by: Lucas250 | Posted: 21-02-2022



Answer 3

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

Answered by: Ada558 | Posted: 21-02-2022



Answer 4

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.

Answered by: Walter893 | Posted: 21-02-2022



Answer 5

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

Answered by: Hailey191 | Posted: 21-02-2022



Answer 6

You can use onFocusChangeListener;

TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {

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

            }

        }
    });

Answered by: Catherine711 | Posted: 21-02-2022



Answer 7

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

Answered by: Kimberly243 | Posted: 21-02-2022



Answer 8

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

autoCompleteTextView.showDropDown()

Answered by: Thomas755 | Posted: 21-02-2022



Answer 9

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

Answered by: Elise751 | Posted: 21-02-2022



Answer 10

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

Answered by: Justin683 | Posted: 21-02-2022



Answer 11

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.

Answered by: Owen431 | Posted: 21-02-2022



Answer 12

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

Answered by: Aida184 | Posted: 21-02-2022



Answer 13

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.

Answered by: Walter249 | Posted: 21-02-2022



Answer 14

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

Answered by: Oliver397 | Posted: 21-02-2022



Answer 15

For me, It is :

 autoCompleteText.setThreshold(0);

do the trick.

Answered by: Adrian505 | Posted: 21-02-2022



Similar questions

android - Fetch AutoCompleteTextView suggestions from service in separate thread

For my AutoCompleteTextView I need to fetch the data from a webservice. As it can take a little time I do not want UI thread to be not responsive, so I need somehow to fetch the data in a separate thread. For example, while fetching data from SQLite DB, it is very easy done with CursorAdapter method - runQueryOnBackgroundThread. I was looking around to other adapters like ArrayA...


android - AutoCompleteTextView suggestions can be customize?

I am using AutoCompleteTextView in my application for search option. In my case I want to show some suggestions along with image as drawable left/right to the TextView. How to filter some suggestion and add drawable resource while displaying the suggestios.Because this drawable I have to add for some options only. Can we give our own layout for the suggestions display? Suggest me. Regards Murthy


Android: AutoCompleteTextView with default suggestions

How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView. I want to show suggestions for common input values to save the user from typing. Any suggestions?


android - AutoCompleteTextView hide auto complete suggestions

i have an AutoCompleteTextView widget on the view. if it has input focus and I'm trying to change the text in there with: txtField1.setText("test"); I'm getting suggestions list appears on the screen. How do I hide it? is it possible to update the text not showing suggestions?


java - Android AutoCompleteTextView, how to link suggestions to xml?

I am trying to use the suggestions of my AutoCompleteTextView can be click to view my xml layouts. I have done the xml part. AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" android:layout_gravity="center" android:text="Enter keyword to search outlet" So here are my suggestions :


android - Show suggestions of AutoCompleteTextView

Is it possible to have the AutoCompleteTextView show the suggestions above the keyboard just like when using the default dictionary? (If yes, how:)) Or can I use my own database to show suggestion when using EditText? Hope you understand what I mean:) Thank you Bastaixen


android - AutoCompleteTextView doesn't show dictionary suggestions

I have a custom AutoCompleteTextView where the user can enter text and whenever the user writes @ I show a dropdown with suggestions of custom usernames. Unfortunately, I also need to show the dictionary word suggestions above the keyboard and, for some reason, AutoCompleteTextView doesn't show dictionary suggestions, although it inherits from EditText where it does show. So, ...


Can i show the location name suggestions for AutoCompleteTextView from service in android?

I am using AutoCompleteTextView to show the suggestions for my search area. It is displaying city names which are coming form the server. I stored them in array and gave that array to adapter. But some time the services is giving thousands of city name. To load these city names my application will take 4-5 minutes. So i want an alternative to do that. Now my theme is getting city names from some existed api's like Google a...


android - AutocompleteTextView with async suggestions doesn't show dropdown

I add TextChangedListener to AutocompleteTextView. In TextChangedListener's afterTextChanged() I invoke AsyncTask which loads data from web (loading all the data when activity starts is not an option because lists can be pretty large, so it becomes just waste of traffic). AsyncTask's onPostExecute() looks like that (I use Array...


android - Constant Suggestions for AutoCompleteTextView

I am using autocompletetextview in my app and putting 4 items in my strings.xml file. When I type a letter, the dropdowm list is populated according to the letter I type. What I need it to do, however, is whenever I type a letter all the values from strings.xml must be shown in the dropdown list on autocompletetextview. Is this possible?


android - AutoCompleteTextView not working properly in Dialog?

I am trying to put Autocomplete text view in dialog, but I am getting error on adaptor. I have created a new class extended by dialog and written following code: AutoCompleteTextView textViewCountry = (AutoCompleteTextView) findViewById(com.example.FindItNear.R.id.autocomplete_radius); ArrayAdapter adapter = new ArrayAdapter&lt;String&gt;(this,com.example.FindItNear.R.layout.list_item, RADIU...


autocomplete - Android: Make AutoCompleteTextView dropdown wrap to 2 lines or more

I am writing an Android app that uses an AutoCompleteTextView just like the API example. The problem is that my text is too long and needs to wrap when the dropdown occurs. I have a custom list_item.xml, but only one line of text displays. The list_item.xml layout wraps to 2 lines if used with a spinner, but not with the AutoCompleteTextView. main.java public class main extends Activity { @Ov...


android - AutoCompleteTextView with custom list: how to set up OnItemClickListener

I am working on an app which uses tags. Accessing those should be as simple as possible. Working with an AutoCompleteTextView seems appropriate to me. What I want: existing tags should be displayed in a selectable list with a CheckBox on each item's side existing tags should be displayed UPON FOCUS of AutoCompleteTextView (i.e. not after typing a letter) What I've done so far is storin...


android - AutoCompleteTextView onItemClick item position or id using HashMap

I am new to Android development and I ran into a problem which I find difficult to solve. I am trying to figure out how to use an AutoCompleteTextView widget properly. I want to create a AutoCompleteTextView, using XML data from a web service. I managed to get it to work, but I am defenitely not pleased with the output. I would like to put a HashMap with id => name pairs into...


android - How to limit the number of dropdown items visible at a time on screen in AutoCompleteTextView?

I have an AutoCompleteTextView and want to limit the number of dropdown items visible at a time on the screen. Currently it fills up the screen but can I limit it to say 2 items with a scroll bar for displaying more.


android - AutocompleteTextView: on "NEXT" highlight next TextView, on "DONE", make keyboard disappear

I have two AutocompleTextViews and I want to switch to the next if the user presses "NEXT", and make the virtual keyboard disappear when he hits "DONE" at the second AutocompleTextView. So far, the buttons "NEXT"/"DONE" do nothing at all.... Unfortunately I found no resources addressing this problem. Any suggestions? thx EDIT: Just want to add that this was asked when Android was on vers...


android - Selection list cut off in AutoCompleteTextView in Dialog

I have a dialog window that covers 1/3 of the entire screen height and is displayed on top of my activity. The dialog holds two AutoCompleteTextView fields. The problem I'm facing is that when the user starts typing something into the AutoCompleteTextView, the list with all suggestions only shows up to the bottom end of the dialog, but doesn't go beyond that even the list is longer. It looks like it's cut off. (scr...


How to populate an AutoCompleteTextView with contacts names in android 2.1

I have an AutoCompleteTextView and i want it to autocomplete contact names on input. The problem is that Contacts.People has been deprecated. The new documentation says to use ContactsContract but i can't find any proper tutorial. Can anyone give me a sample code or point me to an appropriate tutorial. Thanks.


android autocompletetextview hint results hidden under keyboard

I have 3 autocompletetextview's in which i set its adapter to be an ArrayAdapter&lt;String&gt; with a very simple textview layout. The autocompletextview hint results are showing, but are under the onscreen keyboard(i can see part of it). how can i make the results show above the autocompletetextview rather than below? airline = (...


android - AutoCompleteTextView doesn't show dropdown

I extend AutoCompleteTextView and override preformFiltering function in order to get results from database. I'm getting the results but then nothing is shown. And getView in custom adapter is never called. The strange thing that If I preload items (inside init() function) I can see them... May by anyone can point me to the right solution? Thanks. public class CityAutoCompleteTextView extends AutoCompleteT...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top