Advertisement
Featured

Android AutoCompleteTextView Example

Aditya SinghUpdated
Android AutoCompleteTextView Example

Android AutoCompleteTextView gives you suggestions based on a character you have typed.

It makes easy for users to search the word as they don't have to type complete words. AutoCompleteTextView will show a drop-down menu and we can select the word which we want.

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

In this example, we have taken all the laptop company name and stored in a string array. We have used ArrayAdapter to display the array content.

MultiAutoCompleteTextView is a subclass of AutoCompleteTextView. AutoCompleteTextView Class is a subclass of EditText.

Android AutoCompleteTextView Example in Android

Advertisement

Activity_main.xml

You can Drag AutoCompleteTextView and TextView from the palette which on the left-hand side in Android Studio.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9BCAF8"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="180dp"
        android:layout_marginEnd="8dp"
        android:lineSpacingExtra="8sp"
        android:text="LAPTOP"
        android:textAlignment="textStart"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="36sp"
        android:textStyle="bold"
        android:typeface="serif"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <AutoCompleteTextView
        android:id="@+id/acx"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="8dp"
        android:hint="SELECT LAPTOP"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:ignore="MissingConstraints" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

package androidhire.com.autocomplete;

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

import java.lang.reflect.Array;

public class MainActivity extends AppCompatActivity {

    String[] laptops = new String[]{"Dell","Lenovo","ACER","APPLE","HP","MSI","Toshiba","ASUS","Samsung","Sony","VAIO","Xiaomi","iball",};
    AutoCompleteTextView autoCompleteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoCompleteTextView = findViewById(R.id.acx);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, laptops);
        autoCompleteTextView.setAdapter(adapter);
    }
}

If are facing any problem in Android AutoCompleteTextView Example then you can comment below.

Related reading

Frequently Asked Questions

What is AutoCompleteTextView in Android?

AutoCompleteTextView is an editable text field that shows completion suggestions in a drop-down menu as the user types. Picking an item from the list replaces the contents of the edit box, so users do not have to type a full word.

How do I supply suggestions to an AutoCompleteTextView?

Put the suggestions in a string array and attach them with an ArrayAdapter, then call setAdapter() on the AutoCompleteTextView. The adapter handles matching what the user types against the list.

What is the difference between AutoCompleteTextView and MultiAutoCompleteTextView?

MultiAutoCompleteTextView is a subclass of AutoCompleteTextView that allows more than one completion in the same field, separated by a token such as a comma. Use it for things like tag or recipient entry.

Is AutoCompleteTextView a type of EditText?

Yes. AutoCompleteTextView extends EditText, so it accepts the usual EditText attributes for hints, input type and styling in addition to its own suggestion behaviour.

How many characters before suggestions appear?

By default the drop-down appears after two characters. Change it with the android:completionThreshold attribute in XML, or setThreshold() in code, and set it to 1 if you want suggestions from the first keystroke.

Android

Related Articles

Advertisement