Autocomplete TextView And MultiAutoComplete TextView In Android

AutoComplete TextView-

AutoComplete text view is same like EditText, Which is used to provide autosuggestion text data (in a drop down manner )  according to input character given (type) by the user. And from the autosuggestion (drop down menu) user can select only one suggestion or value. For example suppose on google user want to search India then user type first or second character  of india then in drop down list user will get many words which started with I or In from which user can select India.

AutoComplete TextView is the subclass of EditTextaclass.

    

 Example-

.xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity4">
<TextView
android:id="@+id/txtac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="62dp"
android:layout_marginEnd="46dp"
android:layout_marginBottom="8dp"
android:text="which is your favorite city"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/act"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></TextView>
<AutoCompleteTextView
android:id="@+id/act"
android:layout_width="362dp"
android:layout_height="71dp"
android:layout_marginStart="21dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="532dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtac"></AutoCompleteTextView>
</androidx.constraintlayout.widget.ConstraintLayout>


.java  file code


package com.example.fragementa;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity4 extends AppCompatActivity {
AutoCompleteTextView acc;
String []city={"india","america","uk","indonesia","pakistan","afganistan","irak","srilanka","argentina"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
acc = findViewById(R.id.act);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.select_dialog_item,city);
acc.setTextColor(Color.BLUE);
acc.setThreshold(1);
acc.setAdapter(aa);
}
}

Use of Threshold()-

In Threshold we set the character size. In search bar start to search related words.

Suppose if we want to search words from first character then we will set the value of Threshold is one.

acc.setThreshold(1);

Suppose if we want to search words from first two character then we will set the value of Threshold is two.

acc.setThreshold(2);

OUTPUT-



MultiAutoComplete TextView-

The MultiAutocomplete TextView is the subclass of AutoComplete class.

Whenever we have to search more than one word then we will use MultiAutocomplete TextView.

Example-

Code for MultiAutoComplete TextView-

.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity4">

<TextView
android:id="@+id/txtac"
android:layout_width="391dp"
android:layout_height="57dp"
android:layout_marginStart="2dp"
android:layout_marginTop="62dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="8dp"
android:text="which is your favorite country"
android:textColor="#000"
android:textSize="27dp"
app:layout_constraintBottom_toTopOf="@+id/act"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></TextView>

<MultiAutoCompleteTextView
android:id="@+id/act"
android:textSize="30dp"
android:layout_width="371dp"
android:layout_height="83dp"
android:layout_marginStart="21dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="532dp"
android:textColor="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtac"></MultiAutoCompleteTextView>
</androidx.constraintlayout.widget.ConstraintLayout>

.java file 

package com.example.fragementa;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity4 extends AppCompatActivity {
MultiAutoCompleteTextView acc;
String []city={"india","america","uk","indonesia","pakistan","afganistan","irak","srilanka","argentina"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
acc = findViewById(R.id.act);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.select_dialog_item,city);
acc.setTextColor(Color.BLUE);
acc.setThreshold(1);
acc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
acc.setAdapter(aa);
}
}
Output-














Comments