AutoCompleteTextView

Hi Guys!
An AutoComplete TextView 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 drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.

FOr detail about the Auto Complete Text View please visit the android developer site AutoCompleteTextView.

Now lets start the coding about the auto text complete view . Here we are getting the item data in array from xml resource.And these array data stored in the adapter.

res/value/myvalues.xml

<resources>
     <array name="month">
       January
       February
       March
       April
       May
       June
       July
       August
       September
       October
       November
       December
</array>
</resources>

activity_main.xml

<Linearlayout android:id="@+id/LinearLayout1" 
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin" 
  tools:context=".MainActivity" 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools">

    <Textview 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/hello_world"/>

    <Autocompletetextview 
     android:completionthreshold="1" 
     android:id="@+id/input"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"/>

</Linearlayout>

MainActivity.java

package com.sunil.autocompletetext;

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

public class MainActivity extends Activity implements TextWatcher {
 
AutoCompleteTextView autoCompleteTextView;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.input);
      String[] month = getResources().getStringArray(R.array.month);
      autoCompleteTextView.addTextChangedListener(this);
      autoCompleteTextView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, month));
  }

@Override
public void afterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub
  
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  int arg3) {
 // TODO Auto-generated method stub
  
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
 // TODO Auto-generated method stub
  
}
}

 You can download the source code Auto Complete Text View

Cheers Guys!

2 comments:

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *