Android AutoComplete

In this Android tip, I am going to show you how to implement AutoComplete using the AutoCompleteTextView widget. The AutoCompleteTextView allows you to enter few beginning characters of a word or phrase and then it will display a list of possible values from which one value can be selected to complete the word or phrase. The list of possible values is displayed when you enter beginning part of the word or phrase and it disappears after an item of the list is selected or when you click any location on the screen. This is useful when you want to speed along the process of typing text in your application, or you want to save space of your screen interface.



Now to have an example application of using the AutoCompleteTextView to implement the AutoComplete feature, create a new Android project. Then add an AutoCompleteTextView to the activity_main.xml file as shown below.

<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" >

   <AutoCompleteTextView
     android:id="@+id/autocompleteview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
    
  />

</RelativeLayout>


After that, open the MainActivity.java file. In the onCreate() method of the MainActivity class, you write code to create an array to store the names of days of a week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday), create an object of ArrayAdapter that wraps the array, and call set the adapter to the AutoCompleteTextView using its setAdapter() method.

      package com.example.autocomplete;

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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create a collection of items
String[] items=new String[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday","Sunvideo"};
//create an ArrayAdapter object containing items for the AutoCompleteTextView
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
//reference to the AutoCompleteTextView
AutoCompleteTextView auto_view=(AutoCompleteTextView)findViewById(R.id.autocompleteview);
//set adapter to the AutoCompleteTextView
auto_view.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-AutoComplete

Posted by: Dara | post date: 08-24-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.