Breaking

Thursday 18 April 2019

Android AutoComplete TextView Example | Android Tutorial


Android AutoComplete TextView Example

Android AutoCompleteTextView finishes the word dependent on 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 AutoComplete TextView Example

In this model, 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 cluster content.

How about we see the straightforward case of autocompletetextview in android.

activity_main.xml



Drag the AutoCompleteTextView and TextView from the pallete, presently the activity_main.xml record will this way:

File: activity_main.xml


<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.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="example.cptech.com.autocompletetextview.MainActivity"> 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="What is your favourite programming language?" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintRight_toRightOf="parent" 
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintVertical_bias="0.032" /> 
 
    <AutoCompleteTextView 
        android:id="@+id/autoCompleteTextView" 
        android:layout_width="200dp" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="92dp" 
        android:layout_marginTop="144dp" 
        android:text="" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent" /> 
 
</android.support.constraint.ConstraintLayout>


Activity Class :


Let's write the code of AutoCompleteTextView.

File :- MainActivity.java


package example.cptech.com.autocompletetextview; 
 
import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
 
public class MainActivity extends AppCompatActivity { 
    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<String> adapter = new ArrayAdapter<String> 
                (this,android.R.layout.select_dialog_item,language); 
        //Getting the instance of AutoCompleteTextView 
        AutoCompleteTextView actv =  (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView); 
        actv.setThreshold(1);//will start working from first character 
        actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView 
        actv.setTextColor(Color.RED); 
    } 
}


OutPut :-

No comments:

Post a Comment