Thursday, 22 January 2015

AutoCompleteTextView Example in Android





strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AutoComplete Example</string>
    <string name="hint">Enter Any Month Name</string>
</resources>

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

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="110dp"
        android:ems="10"
        android:hint="@string/hint" >
<requestFocus />
    </AutoCompleteTextView>
</RelativeLayout>

list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/black" >
    </TextView>

MainActivity:
package com.example.autocompleteexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity implements  OnItemClickListener {
AutoCompleteTextView complete;
       String[] Month={"January","February","March","April","May","June","July","August","September","October","November","December"};
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              complete=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
              ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,Month);
              complete.setAdapter(adapter);
      
              }

       public void onItemClick(AdapterView<?> arg0, View arg1, int positionlong arg3) {
              // TODO Auto-generated method stub
              Toast.makeText(getApplicationContext(), Month[position], Toast.LENGTH_LONG).show();
       }

}


No comments:

Post a Comment