Friday, 6 May 2016

AutoComplete TextView

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

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="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <AutoCompleteTextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:completionThreshold="3"
        android:id="@+id/auto"/>

</RelativeLayout>

ActivityMain.Java

package com.example.autocompletetextview;

import org.w3c.dom.Text;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
String[] Values={"Android","Java","CoreJava","Raj","Android","Java","CoreJava","Raj"};

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

AutoCompleteTextView auto=(AutoCompleteTextView) findViewById(R.id.auto);

ArrayAdapter adp=new ArrayAdapter<>(getApplicationContext(), 
android.R.layout.simple_list_item_1,Values);
auto.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView tv=(TextView)view;
Toast.makeText(getApplicationContext(), "Selected is:"+tv.getText().toString(), 5000).show();


}

});


}
}


No comments:

Post a Comment

Ads Inside Post