Autocomplete Textview in Android

AutoCompleteTextView is used to show list of suggestion and select one of them . Basically it looks like as EditText before type . It's a sub class of EditText. Lets see how we can easily implement the AutoCompleteTextView in android .

Step 1: Create Project 
          a) Open Android Studio
          b) Go to File >New> New Project  > Project Name  > Next > Next > Next > Finish 

Step 2 : Layout Design 
open activity_main.xml file and add the bellow code 

<?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"
    android:background="#ff33b5e5">

    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Language..."
        android:layout_marginTop="20dp"
       />
</RelativeLayout>


Step 3 : Code
open MainActivity class and add the bellow code 

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

/**
 * Created by anupam on 30/9/16.
 */
public class MainActivity extends Activity {
  
    Context context;
    AutoCompleteTextView auto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
      
        String[] Language = { "C", "C++","C#","Java","Objective C",
                "Swift","Php","Python","ASP","ASP.Net","VB.Net"};

        auto=(AutoCompleteTextView)findViewById(R.id.auto);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.select_dialog_item,Language);
        auto.setThreshold(1);//search will start from first character
        auto.setAdapter(adapter);//set the adapter
    }
}


Find Us : 
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial

Comments

Popular posts from this blog

Custom Calendar in android

Disable/Hide Year from DatePickerDialog in android

Coordinator Layout in Android