Friday, February 22, 2013

AutoCompleteTextView Tutorial


here is the layout xml file:
?
1
2
3
4
5
                        android:id="@+id/AndroidBooks"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >
                    
Nothing special. It just consists of one element.
Now in the main activity, I create an array of android books:
?
1
2
3
4
5
6
7
8
9
10
11
12
String[] androidBooks =
           {
           "Hello, Android - Ed Burnette",
           "Professional Android 2 App Dev - Reto Meier",
           "Unlocking Android - Frank Ableson",
           "Android App Development - Blake Meike",
           "Pro Android 2 - Dave MacLean",
           "Beginning Android 2 - Mark Murphy",
           "Android Programming Tutorials - Mark Murphy",
           "Android Wireless App Development - Lauren Darcey",
           "Pro Android Games - Vladimir Silva",
           };
Then in the onCreate(..) method, I create an ArrayAdapter that I can pass to thisAutoCompleteTextView as the data Source.

?
1
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,androidBooks);

Then, I get a handle to the AutocompleteTextView, and set the arrayAdapter to it along with the Threshold. The Threshold defines the number of charaters a user should type before the suggestions start showing up.

?
1
2
3
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.acTVCountry);
           acTextView.setThreshold(3);
           acTextView.setAdapter(adapter);
?
1
 
That is it. Now execute and see it work. 

This is the way it would look:


1 comment:

  1. Hi,

    Recently I came across some great articles on your site.
    The other day, I was discussing (http://paragkavar.blogspot.in/2013/02/autocompletetextview-tutorial.html)with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
    Would it be ok to submit the article? It is free of charge, of course!

    Let me know what you think
    Contact me at anelieivanova@gmail.com

    Regards
    Anelie Ivanova

    ReplyDelete