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.

Recent Articles

OnePlus 7T vs iPhone 11 | Specs Comparison

The OnePlus 7T and iPhone 11 are the two best phones if you looking for a price of less than $1000. Both the phone...

Elon Musk unveils Tesla’s Cybertruck

Are you into gaming or love watching sci-fi movies?  What about those back to the future movies which gave us a glimpse of the...

7 Best Hiking Apps you Must Try in 2020

Having the best hiking apps always gives you an advantage if you are going for adventurous trips. These apps can track your workout and activities...

[UPDATED] 9 Best Android Launcher Apps of 2019

Android launcher is best to customize the overall experience of Android for a user in terms of look and act. In apps store, there are...

Stadia Pro members will get two more games in December

If you are a member of Google Stadia Pro then there is good news for you, you will be getting two more games for free...

Related Stories

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox