Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

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.

Android AutoCompleteTextView Example

In this example, we are displaying the programming languages in the autocompletetextview. All the programming languages are stored in string array. We are using the ArrayAdapter class to display the array content.

Let’s see the simple example of autocompletetextview in android.

activity_main.xml

Drag the AutoCompleteTextView and TextView from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <TextView
  7.         android:id=“@+id/textView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginTop=“15dp”
  13.         android:text=“@string/what_is_your_favourite_programming_language_” />
  14.     <AutoCompleteTextView
  15.         android:id=“@+id/autoCompleteTextView1”
  16.         android:layout_width=“wrap_content”
  17.         android:layout_height=“wrap_content”
  18.         android:layout_alignParentLeft=“true”
  19.         android:layout_below=“@+id/textView1”
  20.         android:layout_marginLeft=“36dp”
  21.         android:layout_marginTop=“17dp”
  22.         android:ems=“10”
  23.         android:text=“”>
  24.         <requestFocus />
  25.     </AutoCompleteTextView>
  26. </RelativeLayout>

Activity class

Let’s write the code of AutoCompleteTextView.

File: MainActivity.java
  1. package com.example.autocompletetextview;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.view.Menu;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.AutoCompleteTextView;
  8. public class MainActivity extends Activity {
  9.     String[] language ={“C”,“C++”,“Java”,“.NET”,“iPhone”,“Android”,“ASP.NET”,“PHP”};
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.         //Creating the instance of ArrayAdapter containing list of language names
  15.            ArrayAdapter<String> adapter = new ArrayAdapter<String>
  16.             (this,android.R.layout.select_dialog_item,language);
  17.         //Getting the instance of AutoCompleteTextView
  18.            AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
  19.            actv.setThreshold(1);//will start working from first character
  20.            actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
  21.            actv.setTextColor(Color.RED);
  22.     }
  23.     @Override
  24.     public boolean onCreateOptionsMenu(Menu menu) {
  25.         // Inflate the menu; this adds items to the action bar if it is present.
  26.         getMenuInflater().inflate(R.menu.activity_main, menu);
  27.         return true;
  28.     }
  29. }

download this example

Output:

autocompleteoutput1

autocompleteoutput2

Comments

comments