Monday 10 November 2014

AutoComplete TextView Example

Your  MainActivity.java like this;

package com.example.autocomplete;

import java.util.ArrayList;

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

public class MainActivity extends Activity {

AutoCompleteTextView countyList;
ArrayList<String>countryNames;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        countyList=(AutoCompleteTextView)findViewById(R.id.countyList);
        countryNames=new ArrayList<String>();
        countryNames.add("India");
        countryNames.add("United States of America");
        countryNames.add("Pakistan");
        countryNames.add("France");
        countryNames.add("Indonesia");
        countryNames.add("United states of Kingdom");      
        countryNames.add("South Africa");
        countryNames.add("Germany");
        countryNames.add("Japan");
        countryNames.add("China");
       
        ArrayAdapter<String> autoAdptr=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,countryNames);
        countyList.setAdapter(autoAdptr);
    }

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

Your activity_main like this;

<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=".MainActivity" >

    <AutoCompleteTextView
        android:id="@+id/countyList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:ems="10"
        android:text="AutoCompleteTextView" >

        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>

App like this;




No comments:

Post a Comment