What is AutoCompleteTextView

In this section we are going to discuss the AutoCompletTextView widgets.


AutoCompleteTextView:.

AutoCompleteTextView is a widgets that shows the list of suggestions in drop down menu automatically while the user type the character in it. These suggestion we store into array . Array provide all the info to the ArrayAdapter . ArrayAdapter provide the info to AutoCompletTextView .

Smiley face

First of all we need to create the android project give the name to your project like AutoCompleteTextViewDemo.

Once you have done the project creation.Then you drag and drop the AutoCompleteTextView and then open the activity_main.xml.It will display code look like this.

<RelativeLayout 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="com.example.niitpc.myapplication.MainActivity">

       <AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="71dp"
android:layout_marginStart="71dp"
android:layout_marginTop="54dp" />
</RelativeLayout>

After that you will open the MainActivity.java file and write the code like this.

It is used to perform some action after clicking like if you want to add two number than you will use the Button widgets.

       package com.example.niitpc.myapplication;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView autoCompleteTextView;
    String[] country={"India","Iran","Iraq","America" ,"Afrika"};

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

        autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        ArrayAdapter adapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,country);
        autoCompleteTextView.setThreshold(1);
        autoCompleteTextView.setAdapter(adapter);


    }

}

       

In the above code bellow the MainActivity class i have declared the AutoCompleteTextView object as autoCompleteTextView.

In next line i have declared the array and their data type is String and store the country name.

Below the setContentView() i have to initialized the autoCompleteTextView object.

And then i have to created the object of ArrayAdapter as adapter.Here we need to pass the three argument first is context like this second is layout and third is array(country).

In next line we call the setThresold(1) with the object of autoCompleteTextView object.It deciede how many character you enter in AutoCompleteTextView to display the suggestion .In my case it is 1 . I have passed the argument

In the next line we call the setAdapter(adapter) with the object of autoCompleteTextView object. It will provide all the info of array to autoCompleteTextView object .


autocompletetextview

What is autocompletetextview for video click here

Smiley face                     Smiley face