So, here we came with our Short tutorial on How to Implement AutoCompleteTextView in an Android Application. In this tutorial, we will discuss a simple View which is present in android knows as AutoCompleteTextView.
This view is somewhat similar to Edit text but in this, while entering some Input this gives you Suggestion below it to choose from that and save the time of typing.
So, Let's start,
1. First of all, Create a New Project in Android Studio and Complete All the Process of making a project.
2. Now Open your activity_main.xml and add the below code into it which contains AutoComplete TextView
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.targetandroid.info.autocomptv">
<DigitalClock
android:id="@+id/clock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/secondary_text_light"
android:textStyle="bold"
android:textSize="20dp"/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/actv_city"
android:hint="Enter city"
android:gravity="center"
android:layout_below="@+id/clock"
/>
</RelativeLayout>
3. Now Direct Come Towards our MainActivity.java and add below code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import java.util.ArrayList;
import java.util.List;
public class autocomptv extends AppCompatActivity {
//creating Variables
AutoCompleteTextView actv_city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomptv);
actv_city=(AutoCompleteTextView) findViewById(R.id.actv_city);
//An Array list to add the suggestions
List<String> city=new ArrayList<>();
city.add("Nagpur");
city.add("Raipur");
city.add("Kolhapur");
city.add("Solapur");
city.add("Bilaspur");
city.add("Kanpur");
city.add("Nasik");
ArrayAdapter city_adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,city);
//set the adapter into autocomplete textview
actv_city.setAdapter(city_adapter);
//set here that after how many words the suggestion should appear
//I passed here 1 means after entering the first word the suggestion starts appearing
actv_city.setThreshold(1);
}
}
4. Now Run your Project and See the Output.
For More Awesome Tutorial and to stay updated with our content please subscribe to my blog below is the option to subscribe and share this article.
ThankYou

0 comments:
Post a Comment