Saturday, 23 July 2016

AutoCompleteTextView Example


Screenshots :




activity_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.chantisandroid.exampleapp.MainActivity">
    
    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:hint="enter text here" />
</RelativeLayout>


MainActivity.java :


package com.chantisandroid.exampleapp;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity  {

    String[] STATES = {"Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh",
"Goa", "Gujarat", "Haryana","Himachal Pradesh","Jammu and Kashmir","Jharkhand",
 "Karnataka","Kerala","MadhyaPradesh","Maharashtra","Manipur","Meghalaya","Mizoram"
  ,"Nagaland","Odisha","Punjab","Rajasthan","Sikkim","Tamil Nadu","Telangana",
"Tripura","Uttar Pradesh","Uttarakhand","West Bengal"};

    AutoCompleteTextView autoCompleteTextViewp;

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.select_dialog_item, STATES);

        autoCompleteTextViewp = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        autoCompleteTextViewp.setThreshold(1);
        autoCompleteTextViewp.setAdapter(adapter);
        autoCompleteTextViewp.setTextColor(Color.BLACK);
    }
}


No comments:

Post a Comment

Alarm Manager Example in Android