If you want to get suggestions, when you type in an editable text field, you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop-down menu from which the user can choose an item to replace the content of the edit box with.
First, we have to create AutoCompleTextView Field in MainActivity.xml
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_below="@+id/imageView"
android:hint="Auto Complete Demo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
After that, you have to define a reference of this textview in MainActivity.java
AutoCompleteTextView textView;
textView=(AutoCompleteTextView)findViewById(R.id.autoComplete);
After that, you need to specify the list of suggestions item to be displayed. You
can specify the list items as a string array in java or in string.xml
String[] language=getResources().getStringArray(R.array.list_of_language);
The array adapter class is responsible for displaying the data as list in the suggestion box of the text field. The setAdapter method is used to set the adapter of the autoCompleteTextView.
Here is the Code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Auto Complete Demo"
android:textSize="30sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Is Simples"
android:textSize="30sp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:textColor="#fc1214"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/android"
android:scaleType="centerCrop"
android:contentDescription="@null"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_below="@+id/imageView"
android:hint="Auto Complete Demo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<MultiAutoCompleteTextView
android:id="@+id/mutiAutoComplete"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_below="@+id/autocomplete"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:hint="Multi Auto Complete"/>
</RelativeLayout>
Here is the Code of MainActivity.java
package com.mahesh.androidissimples.autocompletedemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
String[] languages={"Android","java","IOS","Swift","Asp","Web Application","JDBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text= (AutoCompleteTextView) findViewById(R.id.autocomplete);
text1= (MultiAutoCompleteTextView) findViewById(R.id.mutiAutoComplete);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
text1.setAdapter(adapter);
text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
No comments:
Post a Comment