Friday, 31 July 2015

Android AutoCompleteTextView

Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.

Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.


Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class.

Sample Code:

File: 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"
    android:background="@drawable/bg">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="@string/what_is_your_favourite_programming_language_"
        android:textColor="#FB3434"
        android:textStyle="bold"/>
 
    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:text="">

        <requestFocus />
    </AutoCompleteTextView>
</RelativeLayout>

File: MainActivity.java

package com.sneha.autocompletetextview;

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

public class MainActivity extends Activity
{
String[] language ={"Arabic","Albanian","Afrikaans","Basque","Bengali","Catalan","Chinese",
"Danish","Dutch","English","Estonian","French","Gujarati","Greek","German","Hindi",
"Hausa","Italic","Irish","Japanese","Kannada","Kazakh","Lao","Latin","Malay","Marathi",
"Nepali","Polish","Punjabi","Russian","Spanish","Swedish","Tamil","Telugu","Thai",
"Urdu"};

@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,language);

AutoCompleteTextView actv = (AutoCompleteTextView) findViewById (R.id.autoCompleteTextView1);

actv.setThreshold(1);
actv.setAdapter(adapter);
actv.setTextColor(Color.RED);        
}

@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;
}
}

Output: 




























Download Full Source Code: AutoCompleteTextView

Enjoy Coding...!!!

No comments:

Post a Comment