Saturday, September 8, 2012

Autocomplete Text in TextView In Android

Description:
This example shows how to crating rating bar in android.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it AutocompleteText.
2.) Write following code into your main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:padding="5dp">
        <TextView android:layout_width="wrap_content"

                android:layout_height="wrap_content" android:text="Items" />
        <AutoCompleteTextView android:id="@+id/autocomplete_country"
                android:layout_width="fill_parent"android:layout_height="wrap_content"
                android:layout_marginLeft="5dp" />
</LinearLayout>
3.) Create and write following code into your res/layout/list_item.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>
4.) Build and run your code and check the output given below in the doc.
Steps:
1.) Create a project named AutocompleteText and set the information as stated in the image.
Build Target: Android 3.0
Application Name: AutocompleteText
Package Name: com.org. AutocompleteText
Activity Name: AutocompleteText
Min SDK Version: 11
2.) Open AutocompleteText.java file and write following code there:
package com.org.AutocompleteText;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutocompleteTextActivity extends Activity {
    /** Called when the activity is first created. */
        static final String[] ITEMS = new String[] { "Tea",
                "Coffee""Milk""Expresso""Lemon Tea""Cappuciano",
                "Hot Water""Tomato Soup""Ginger Tea""ColdDrink"};
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_country);  
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, ITEMS);  
        textView.setAdapter(adapter);
        }
}
3.) Compile and build the project.
4.) Run on simulator and start typing some letters. It will show a list as soon as you start entering some letters.
Output

No comments:

Post a Comment