Creating the Activity for Auto Complete


Build your own Android App Dev Empire

Get Instant Access

In this section, you are going to create an Activity that will show off the AutoCompleteTextView. AutoCompleteTextViews can be very powerful tools for your applications. This View is specifically helpful at making the most of the limited space available to the Android main screen.

AutoCompleteTextView, as the name implies, is a modified TextView that will refer a possible value for the completion of a word or phrase typed into it. Such Views are greatly useful in mobile applications when you do not want to devote a large amount of space to a ListView, or you want to speed along the process of entering text into your application.

To begin creating your Activity for the AutoCompleteTextView, you need to add a new .xml file for that layout, a .java file for the code, and an Intent Filter to handle

Create a new .xml file in your AndroidViews project named autocomplete.xml. Keep in mind that the filename must be all lowercase. The file should appear in the layout folder in your Package Explorer. Double-click the file to edit it.

This file is going to control the layout for your AutoCompleteTextView Activity, so you need to have an AutoCompleteTextView in the layout. The XML for adding an AutoCompleteTextView looks like this:

<AutoCompleteTextView android:id="@+id/testAutoComplete"



You have created a few Views now in .xml files, so you should be familiar with the format. There is nothing different or unusual about the AutoCompleteTextView. You are setting the id to testAutoComplete, and the width and height to fill_parent and wrap_content, respectively.

You should add the layouts for two Buttons as well. These Buttons will be used to control the attributes that you will change. Name the Buttons autoCompleteButton and textColorButton, as follows:

<Button android:id="@+id/autoCompleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change Layout"/>

<Button android:id="@+id/textColorButton" android:layout_width="fill_parent"

the calls.

the calls.

android:layout_height="wrap_content" android:text="Change Text Color"/>

With the three View layouts added, your finished autocomplete.xml file should look like this:

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

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"

<AutoCompleteTextView android:id="@+id/testAutoComplete"



<Button android:id="@+id/autoCompleteButton"



android:text="Change Layout"/>

<Button android:id="@+id/textColorButton"



android:text="Change Text Color"/>


Follow the instructions that were introduced in the "Creating a New .java File" section earlier in this chapter to create your autocomplete.xml file.

The first thing you need to do is import the packages for your Views. In this Activity, you are using two Views, the AutoCompleteTextView and the Button. You also need to work with Colors and an ArrayAdapter. Therefore, import the following packages with your Activity:

package android_programmers_guide.AndroidViews;


import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;


While you may not know what they are for yet, just add the packages for Color and ArrayAdapter. I will explain them later in this section.

Was this article helpful?

0 0

Post a comment