Wednesday, 12 October 2016

AutoCompleteTextView

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  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:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<AutoCompleteTextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/autoCompleteTextView"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="20dp"
  android:hint="enter letter" />
<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/button"
 android:layout_gravity="center_horizontal"
 android:text="show" />
</LinearLayout>

MainActivity.java :-


package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
Button show;
String name;
static final String[] fruits={"Apple","Orange","Watermelon","Bannana","Strawberry","Peaches"};
@Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
  show=(Button)findViewById(R.id.button);
  ArrayAdapter<String>arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,fruits);
  autoCompleteTextView.setThreshold(0);
  autoCompleteTextView.setAdapter(arrayAdapter);
  autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  name= (String) parent.getItemAtPosition(position);
  }
 });
 show.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
  Toast.makeText(MainActivity.this,"Select Fruits :"+name,Toast.LENGTH_SHORT).show();
  }
  });
  }
}

Output :-

AutoCompleteTextView
AutoCompleteTextView fig :1



No comments:

Post a Comment