Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> AutoComplete TextBox in Android




AutoComplete TextBox in Android - MindStick

AutoComplete TextBox in Android

In this article I am going to explain how to create an AutoComplete textbox by using AutoCompleteTextView widget in an Android application. In this article I am going to create AutoCompleteTextView widget that provides suggestions for student names.

         Start a new project named AutoCompleteDemo.

         Create a XML file named list_item.xml and insert the following code in the xml.

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"   

                android:layout_width="fill_parent"   

                android:layout_height="fill_parent"   

                android:padding="10dp"   

                android:textSize="16sp"   

                android:textColor="#000">

</TextView>

This file simple define a TextView that will be used for each item that appears in the list of suggestions.

         Open the res/layout/main.xml and add an AutoCompleteTextView widget inside the linear layout.

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

       <TextView android:layout_width="fill_parent"

                 android:layout_height="wrap_content"

                 android:text="Student Names"/>

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

                              android:layout_width="fill_parent"

                              android:layout_height="wrap_content"

                              android:layout_marginLeft="5dp" />

</LinearLayout>

Open res/values/strings.xml and the following resources:

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

<resources>

    <string name="hello">Hello World, AutoCompleteActivity!</string>

    <string name="app_name">AutoCompleteDemo</string>

    <string-array name="studentname_array">

       <item>Rohit</item>

       <item>Rahul</item>

       <item>Amit</item>

       <item>Arun</item>

       <item>Rati</item>

       <item>Aman</item>

       <item>Farhan</item>

       <item>Farhad</item>

       <item>Ritika</item>

    </string-array>

</resources>

         Open the Activity (java) file and apply the following code:

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

 

public class AutoCompleteActivity extends Activity {

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        AutoCompleteTextView txtview =(AutoCompleteTextView)

                                             findViewById(R.id.autocompletetextview);

        String [] names=getResources().getStringArray(R.array.studentname_array);

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, 
                                                           R.layout.
list_item,names);

        txtview.setAdapter(adapter);

    }

}

After the content view is set to the main.xml layout, the AutoCompleteTextView() widget is captured from the layout with findViewById(int). Then we get all the student names in an array from the resource file. A new array adapter is then initialized to bind the list_item.xml layout to each list in the names string array. Then setAdpater() is called to associate the ArrayAdapter with the AutoCompleteTextView widget so that the string array will populate the list of suggestions.

         Run the application.
The output look something like below:

AutoComplete TextBox in Android

When the users press some keyword, it will display the related student names automatically from the resource file.



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 10/21/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 2720
Advertisement
MindStick SurveyManager
Advertise with Us
  
Dudelabs
Copyright © 2009 - 2013MindStick. All Rights Reserved.