AutocompleteTextview in Android – Complete Tutorial

AutocompleteTextview in Android – Implementation and Overview

AutocompleteTextview in short, helps the user complete the text when entered into a EditText. If you have read my TextView or ListView, you can guess how we are going to do this.

We will need to declare AutocompleteTextview with a simple AutocompleteTextview XML tag.

YOU ARE YOUR BOSS
I agree to have my personal information transfered to MailChimp ( more information )
Join over 5.000 visitors who are receiving our free content and learn how to program with Android. The growth of Android is estimated to reach a whooping 83% by 2020. Hop on the train and start making money!
We hate spam. Your email address will not be sold or shared with anyone else.
<AutoCompleteTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Here"/>

AutocompleteTextview Properties

 

android:dropDownHeight – Specifies the height of the dropdown.

android:dropDownAnchor – View to anchor the auto-complete dropdown to.

android:completionHint – The hint displayed in the drop down menu. Make sure you have this set, inorder to provide better understanding that the view is actually a drop down view

android:completionThreshold – Tthe number of characters that the user must type before completion suggestions are displayed in a drop down menu. Threshold is important when you are serving data from the database

Think of lots of data you have to show in the drop down. The threshold can help you categorise the data a bit faster. Play around with this property to balance the UI experience and the speed with which the data is rendered.

android:popupBackground – This sets the background. Can be color or a image. Be careful when you set this property, sometimes simple is better!!

android:dropDownSelector – This is the selector in a drop down list.

AutocompleteTextview Implementation

Implement the XML document like below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <AutoCompleteTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text Here"
        android:id="@+id/autocomplete_textview"
        android:completionThreshold="3"
        android:completionHint="Complete Text"/>
</LinearLayout>

AutocompleteActivity.java

package wolfsoft.ozzon;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutocompleteActivity extends AppCompatActivity {
    AutoCompleteTextView autoCompleteTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocomplete_textview);
        String[] stringOfArray = { "autocomplete,text1", "Beta complete","Lines of code",
                "Lint based framework", "automatic"};
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_textview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.select_dialog_item, stringOfArray);
        autoCompleteTextView.setAdapter(adapter);
    }
}

The final screen looks like this:

AutocompleteTextview
AutocompleteTextview

You can take a look here to learn about firebase and pulling data from there.

It is very useful when you have lot of data that you want to create a autocompletetextview. Learn to play around with the data controllers and also various adapters to learn how you can interpret the data.

Check here for additional tips for material design inspiration.

“Learn and Be Curious”

 

Also Read  Taking the first step towards programming in Android

Leave a Comment

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