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

Activity_main.xml

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

AutoCompleteTextView Example

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here