AutoCompleteTextView

Course- Android >

Android AutoCompleteTextView finishes the word in view of the saved words, so no compelling reason to compose every one of the characters of the word. 

Android AutoCompleteTextView is an editable content field, it shows a rundown of proposals in a drop down menu from which client can choose just a single recommendation or esteem. 

Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class. 

Android AutoCompleteTextView Example 

In this illustration, we are showing the programming dialects in the autocompletetextview. All the programming dialects are put away in string cluster. We are utilizing the ArrayAdapter class to show the exhibit content. 

We should see the straightforward case of autocompletetextview in android.

activity_main.xml

Drag the AutoCompleteTextView and TextView from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  

    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginTop="15dp"  
        android:text="@string/what_is_your_favourite_programming_language_" />  

    <AutoCompleteTextView  
        android:id="@+id/autoCompleteTextView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_below="@+id/textView1"  
        android:layout_marginLeft="36dp"  
        android:layout_marginTop="17dp"  
        android:ems="10"  
        android:text="">  

        <requestFocus />  
    AutoCompleteTextView>  

RelativeLayout>  

Activity class

Let's write the code of AutoCompleteTextView.

File: MainActivity.java

package com.example.autocompletetextview;  

import android.os.Bundle;  
import android.app.Activity;  
import android.graphics.Color;  
import android.view.Menu;  
import android.widget.ArrayAdapter;  
import android.widget.AutoCompleteTextView;  

public class MainActivity extends Activity {  
    String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list of language names  
           ArrayAdapter adapter = new ArrayAdapter  
            (this,android.R.layout.select_dialog_item,language);  
        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           actv.setThreshold(1);//will start working from first character  
           actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
           actv.setTextColor(Color.RED);  
 
    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
}