Basic Concepts of AutoCompleteTextView and MultiAutoCompleteTextView control

In this tutorial basic concepts of AutoCompleteTextView and MultiAutoCompleteTextView control of Android is shown.

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<AutoCompleteTextView
android:id=”@+id/actv1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”58dp”
android:ems=”10″
android:hint=”Enter colors” >

<requestFocus />
</AutoCompleteTextView>

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_below=”@+id/textView1″
android:layout_marginTop=”26dp”
android:text=”TextView” />

<MultiAutoCompleteTextView
android:id=”@+id/mactv1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/actv1″
android:layout_below=”@+id/actv1″
android:layout_marginTop=”20dp”
android:ems=”10″
android:hint=”Enter colors”
android:completionThreshold=”1″ />

<Button
android:id=”@+id/b1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:onClick=”onSubmit”
android:text=”Click Here” />

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/b1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”36dp”
android:text=”TextView” />

</RelativeLayout>

 

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;

public class MainActivity extends Activity{

AutoCompleteTextView actv;
MultiAutoCompleteTextView mactv;
TextView tv1,tv2;
final String color[]={“Red”,”Green”,”Yellow”,”Blue”,”Green1″,”Green2″,”Green3″,”Red1″,”Red2″};
String t1,t2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv=(AutoCompleteTextView)findViewById(R.id.actv1);
mactv=(MultiAutoCompleteTextView)findViewById(R.id.mactv1);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);

ArrayAdapter <String> ad1=new ArrayAdapter <String> (this,android.R.layout.simple_dropdown_item_1line,color);
actv.setAdapter(ad1);
mactv.setAdapter(ad1);
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

}

public void onSubmit(View v)
{
t1=actv.getText().toString();
t2=mactv.getText().toString();

tv1.setText(t1);
tv2.setText(t2);
}

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

}

https://youtu.be/Ke52_oL_bjg

Add a Comment

Your email address will not be published. Required fields are marked *