0% found this document useful (0 votes)
28 views5 pages

Android - AutoCompleteTextView Control

The document discusses the AutoCompleteTextView control in Android. It describes what the control is, provides examples of key attributes, and includes a full code example to create an Android app with an AutoCompleteTextView.

Uploaded by

html backup
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views5 pages

Android - AutoCompleteTextView Control

The document discusses the AutoCompleteTextView control in Android. It describes what the control is, provides examples of key attributes, and includes a full code example to create an Android app with an AutoCompleteTextView.

Uploaded by

html backup
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Android - AutoCompleteTextView Control

A AutoCompleteTextView is a view that is similar to EditText, except that it shows a list of completion
suggestions automatically while the user is typing.

The list of suggestions is displayed in drop down menu. The user can choose an item from there to replace the
content of edit box with.

AutoCompleteTextView Attributes
Following are the important attributes related to AutoCompleteTextView control. You can check Android official
documentation for complete list of attributes and related methods which you can use to change these attributes
are run time.

/
Sr.No Attribute & Description

android:completionHint
1
This defines the hint displayed in the drop down menu.

android:completionHintView
2
This defines the hint view displayed in the drop down menu.

android:completionThreshold
3 This defines the number of characters that the user must type before completion suggestions are
displayed in a drop down menu.

android:dropDownAnchor
4
This is the View to anchor the auto-complete dropdown to.

android:dropDownHeight
5
This specifies the basic height of the dropdown.

android:dropDownHorizontalOffset
6
The amount of pixels by which the drop down should be offset horizontally.

android:dropDownSelector
7
This is the selector in a drop down list.

android:dropDownVerticalOffset
8
The amount of pixels by which the drop down should be offset vertically.

android:dropDownWidth
9
This specifies the basic width of the dropdown.

android:popupBackground
10
This sets the background.

Example

This example will take you through simple steps to show how to create your own Android application using Linear
Layout and AutoCompleteTextView.

/
Step Description

1 You will use Android Studio IDE to create an Android application and name it as GUIDemo3 under a package
com.example.guidemo3 as explained in the Hello World Example chapter.

2 Modify src/MainActivity.java file to add a click event.

3 Modify the default content of res/layout/activity_main.xml file to include Android UI control.

4 Define necessary constants in res/values/strings.xml file

5 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.guidemo3/MainActivity.java. This file
can include each of the fundamental lifecycle methods.

package com.example.guidemo3;

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

public class MainActivity extends Activity {


AutoCompleteTextView autocomplete;

String[] arr = { "Paries,France", "PA,United States","Parana,Brazil",


"Padua,Italy", "Pasadena,CA,United States"};

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

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

ArrayAdapter<String> adapter = new ArrayAdapter<String>


(this,android.R.layout.select_dialog_item, arr);

autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
}
}

Following will be the content of res/layout/activity_main.xml file −

<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
/
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="@string/example_autocompletetextview" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="54dp"
android:ems="10" />

</RelativeLayout>

Following will be the content of res/values/strings.xml to define these new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">GUIDemo3</string>
<string name="example_autocompletetextview">Example showing AutoCompleteTextView<
/string>
</resources>

Following is the default content of AndroidManifest.xml −

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guidemo3" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.example.guidemo3.MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
/
</application>
</manifest>

Let's try to run your GUIDemo3 application. I assume you had created your AVD while doing environment setup.

To run the app from Android Studio, open one of your project's activity files and click Run icon from the toolbar.
Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it
will display following Emulator window −
The following screen will appear after "pa" will be typed in AutoCompleteTextView −

Exercise

I will recommend to try above example with different attributes of AutoCompleteTextView in Layout XML file as
well at programming time to have different look and feel of the AutoCompleteTextView. Try to make it editable,
change to font color, font family, width, textSize etc and see the result. You can also try above example with
multiple AutoCompleteTextView controls in one activity.

You might also like