How to Use AutoCompleteTextView in Android (Java)

0

How to Use AutoCompleteTextView in Android (Java)

AutoCompleteTextView is a powerful UI widget in Android that provides suggestions automatically as the user types. It's useful in forms, search bars, and when you want to improve the user experience by helping users complete text input quickly. In this blog post, we’ll walk through how to use AutoCompleteTextView in Android using Java.

What is AutoCompleteTextView?

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

Benefits of Using AutoCompleteTextView

  • Improves user input speed
  • Enhances user experience with suggestion lists
  • Reduces typing errors
  • Useful for search bars, forms, etc.

Step-by-Step Implementation

1. XML Layout File

Create a layout file activity_main.xml with a single AutoCompleteTextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/auto_com"
        android:layout_margin="16dp"
        android:hint="Enter fruit name"
        android:padding="12dp"
        android:background="@android:drawable/editbox_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
  

2. Java Activity File

Write your Java logic in MainActivity.java to bind the data with AutoCompleteTextView using ArrayAdapter.

package com.earntrek.autocompletetextapp;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView auto_com;

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

        auto_com = findViewById(R.id.auto_com);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");
        arrayList.add("Mango");
        arrayList.add("Grapes");
        arrayList.add("Papaya");
        arrayList.add("Pineapple");
        arrayList.add("Strawberry");
        arrayList.add("Watermelon");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
        auto_com.setAdapter(adapter);
        auto_com.setThreshold(1); // Start showing suggestions after 1 character
    }
}
  

Explanation

  • AutoCompleteTextView is linked with the ID auto_com.
  • We create an ArrayList of fruit names.
  • ArrayAdapter is used to bind this list to the AutoCompleteTextView.
  • setThreshold(1) means suggestions will appear after the user types one character.

Use Cases

  • Fruit selection dropdown
  • City or country picker
  • Search suggestion bar
  • Form input with frequent options

Final Thoughts

AutoCompleteTextView is a handy feature in Android apps that improves usability and enhances user experience. Whether you're building a food app, travel planner, or shopping list, this feature makes input smarter and faster. Keep exploring more advanced features like using custom adapters or integrating APIs for dynamic suggestions.

Watch the Full Tutorial

Post a Comment

0Comments
Post a Comment (0)