Friday, April 3, 2015

How to use AutoCompleteTextView in Android

1. Android AutoCompleteTextView Example

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:padding="16dp"
    tools:context=".MainActivity"
    >
    <AutoCompleteTextView
        android:id="@+id/actv"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidexamples;


// Import ActionBar class when minSdkVersion=11 (Android 3.0) or higher
import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

//additional imported classes
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

    //Initialize a String Array with values
    private static final String[] Colors = new String[]{
            "AliceBlue","AntiqueWhite","Aqua","AquaMarine","Azure",
            "Beige","Bisque","Black","Blue","BlueViolet",
            "CadetBlue","Coral","Crimson","Cyan"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialize an ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_dropdown_item_1line,Colors);
        //Get reference of XML Layout's AutoCompleteTextView widget
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.actv);

        //Apply ArrayAdapter to AutoCompleteTextView
        acTextView.setAdapter(adapter);

        //Define the AutoComplete Threshold
        acTextView.setThreshold(1);
        //Define the AutoComplete DropDown width
        acTextView.setDropDownWidth(250);
    }

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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





2. AutoCompleteTextView with application resource (String Array XML file)

res/values/colors_array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="known_colors_array">
        <item>Lavender</item>
        <item>LavenderBlush</item>
        <item>LawnGreen</item>
        <item>LemonChiffon</item>
        <item>LightBlue</item>
        <item>LightCoral</item>
        <item>LightCyan</item>
        <item>Magenta</item>
        <item>Maroon</item>
        <item>MintCream</item>
        <item>MistyRose</item>
        <item>Moccasin</item>
        <item>Olive</item>
        <item>Orange</item>
        <item>Orchid</item>
    </string-array>
</resources>
MainActivity.java (Partial Code)

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 //Get a Resources instance for application's package
 Resources res = getResources();
 //Initialize and populate a String Array from application resource
 //String Array - XML resource that provide an array of string
 String[] KnownColors = res.getStringArray(R.array.known_colors_array);

 //Initialize an ArrayAdapter from a String Array
 ArrayAdapter<String> adapter = new ArrayAdapter<String>
   (this,android.R.layout.simple_dropdown_item_1line,KnownColors);

 //Get reference of XML Layout's AutoCompleteTextView widget
 AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.actv);

 //Apply ArrayAdapter to AutoCompleteTextView
 acTextView.setAdapter(adapter);

 //Define the AutoComplete Threshold
 acTextView.setThreshold(1);
 //Define the AutoComplete DropDown width
 acTextView.setDropDownWidth(300);
}
Additional Imported Classes

import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.content.res.Resources;


More android examples