Search This Blog

Saturday, June 25, 2016

Auto Complete TextView

 Auto Complete TextView in Android

Auto complete text view is Editable TextView which gives suggestion when we type related character in textview.

Click here for Introduction to Android

Click here for Android Project Directory Structure






as shown bellow:


In android we can use Autocomplete TextView in two way
1) AutoComplete TextView :
   Which will show only strings which we assign to it using ArrayAdapter<String>.
2) Custom AutoComplete Text View :
In custom we can assign strings and also images with strings and so on..



1) AutoComplete TextView:

So lets first see the example of Autocomplete TextView.

So check out my project structure  bellow and create yours:




main.xml


 <?xml version="1.0" encoding="utf-8"?>  

 <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"  
   tools:context="com.ygutte.autocomplete_textview.MainActivity">  

   <AutoCompleteTextView  
     android:id="@+id/actv"  
     android:layout_height="wrap_content"  
     android:layout_width="200dp"  
     android:textSize="12sp"  
     android:layout_centerHorizontal="true">  
     </AutoCompleteTextView>  
 </RelativeLayout>  




MainActivity

 package com.ygutte.autocomplete_textview;
 import android.app.Activity;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.AutoCompleteTextView;

 public class MainActivity extends Activity
 {

    AutoCompleteTextView textViewCity;
    @Override    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textViewCity = (AutoCompleteTextView)findViewById(R.id.actv);
        //String[] Cities = getResources().getStringArray(R.array.Array_city);
        String[] Cities = {"Mumbai", "Pune","Hyderabad","Kolkata","Chennai","Bengaluru"};

        ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,Cities);
        textViewCity.setAdapter(adapter);
        textViewCity.setThreshold(1);

    }
}




Try above example and Happy coding .......









No comments:

Post a Comment