Android example for Autocomplete Textview

In android edittext is one of the most important widget which is used to take inputs from user. If we want to get input regarding a specific topic ,it will be comfortable for user if we can provide the suggestion while he is typing in the edittext.

In android if you want to show suggestion while typing in an edit field you need to use a widget name AutocompleteTextView instead of EditText. It will show suggestions automatically  in a drop down menu when the user is typing.When the user clicks on a particular item in the drop down ,the edit box content will get replaced with the clicked item text. In this tutorial we will learn how to implement an AutocompleteTextView in android.

Steps

  1. Create new project in android studio
  2. Place the autocomplete widget in the layout file.
  3. Setting array data and adapter to autocomplete widget
  4. Setting threshold to autocomplete widget

Create New project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

Place the autocomplete widget in the layout file

Following is the xml code for creating the autocomplete textview . You just need to place it in the layout file you are using.

<AutoCompleteTextView
    android:id="@+id/at_countries"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:hint="Enter your country"/>

Setting array data and adapter to autocomplete widget

Here is the code for your MainActivity .java (activity class which is using autocomplete textview).Here i am defining array of items to display suggestion list and is populating  locally.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    AutoCompleteTextView at_countries;
    //array of countries
    String[] countries = {"Mexico","India","USA","Japan","china",
            "Malasiya","Germany", "Italy", "Canada", "UAE", "Brazil", "Pakistan", "Australia", "Zimbawe","West Indies"  };
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //Set adapter to AutoCompleteTextView
        at_countries.setAdapter(adapter);
    }

    private void init() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        at_countries = (AutoCompleteTextView) findViewById(R.id.at_countries);
        at_countries.setThreshold(1);

        //Create adapter and pass country array to it
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countries);

    }

}

Setting threshold to autocomplete textview

The above code will start displaying suggestions while just typing one letter. If you want to start displaying suggestions only after typing two or three letters you need to set threshold as 2 or 3 respectively.

for start showing suggestions on completing 2 letters you need to add the following code.

at_countries.setThreshold(2);

output

android autocomplete textview
android autocomplete textview

 If you want to show places suggestion you can use googles places suggestion widget.Refer Places autocomplete to learn how to implement places autocomplete in android.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *