Sunday 2 February 2014

Auto Complete TextView :

AutoCompleteTextView is View/control that gives multiple suggestions when user types text in AutoCompleteTextView, it generates the matches related to entered text from its data source to help users to select the text that user wants to enter.This functionality is really help full And you can use this control for to help your app users to avoid Typing complete text with this handy control, which is basically the combination of Edittext and Spinner.

On Launch

AutoCompleteTextView_onLaunch

On Entering Company Name 

AutoCompleteTextView_onEnteringTextAutoCompleteTextView_onEnteringText2AutoCompleteTextView_onEnteringText3

Create new Android Project
Project Name: AutoCompleteTextView //tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Auto Complete Text View
Package Name: com.shaikhhamadali.blogspot.autocompletetextview
Create layout file: layout_auto_complete_text


 1. create layout:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <AutoCompleteTextView
        android:id="@+id/tVAutoComplete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1" 
        android:hint="Enter Compnay Name:"/>

</LinearLayout>

-Strings file:

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

    <string name="app_name">AutoCompleteTextView</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <!-- Array of cell phone companies names -->
 <string-array name="CellphoneCompanies">
     <item>Samsung</item>
     <item>Sony</item>
     <item>SonyEricson</item>
     <item>Qmobile</item>
     <item>Motorola</item>
     <item>Nexus</item>
     <item>LG</item>
     <item>Ainol</item>
     <item>Iphone</item>
     <item>Ainol</item>
     <item>HTC</item>
 </string-array>
</resources>


2.code of activity:


package com.shaikhhamadali.blogspot.autocompletetextview;

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

public class AutoCompleteText extends Activity{
 //declare controls
 AutoCompleteTextView tVAutoComplete;
 //variables
 // Array of cell phone companies names 
 String CellphoneCompanies[]={
   "LG", "Nexus", "Sony", "SonyEricson",
   "Qmobile", "Ainol", "Samsung",
   "Motorola", "HTC", "Iphone", 
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_auto_complete_text);
  //Initialize controls
  tVAutoComplete = (AutoCompleteTextView)findViewById(R.id.tVAutoComplete);
  //Set Adapter to AutoCompleteTextView 
  //you can use it using string array declared in strings.xml file
  /*uncomment me if you want to access Array from strings.xml file
  tVAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, R.array.CellphoneCompanies));*/
  //or using simply array declared in your activity
  tVAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, CellphoneCompanies));
 }
}

3. note that:

  • AutoCompleteTextView is a very handy control you almost see every where in contacts app when you enter name or number of contact, in most of the social apps where you enter you Id or Email it suggest your previous entries etc.
  • Read more about AutoCompleteTextView.
  • completionThreshold this Defines the number of characters that the user must type before  completion suggestions are displayed in a drop down menu. 

      4. conclusion:

      • Some information about how to use AutoCompleteTextView in Android.
      • know the 2 different ways of describing Array Strings in android.

      5. About the post:

      • AutoCompleteTextView  is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
      • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
      • Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
      • Hope you enjoy it!

      6. Source Code:

              you can download the source code From: GoogleDrive, Github