AutocompleteTextView provides auto-completion suggestions while a user is typing text. Users can choose from a list of suggestions displayed in a dropdown menu to replace the content of the edit box with one of these items.
Listed below are the contents of the modified main activity file
package com.example.techstackguru;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autocomplete;
String[] array = { "Blue", "Black", "Brown", "Green", "Red"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autocomplete = findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item, array);
autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
}
}
In order to use you need to create an AutoCompleteTextView Field in your XML.
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:ems="10" />
</androidx.appcompat.widget.LinearLayoutCompat>