NOTE:

NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.
Thanks

Search This Blog

x

Monday 10 May 2010

Auto Complete Text View | Android Beginner Dev Tutorial

This is a very simple tutorial on using Auto Complete Text View provided by Android SDK.


It consists of a single text box which can show suggestions based on a list of data that I provide as a source for this field. Just like Google Suggest, this also shows the nearest matches to the string that is being input by the end user.

So, here is the layout xml file:

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

Nothing special. It just consists of one element.

Now in the main activity, I create an array of android books:

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 this AutoCompleteTextView as the data Source.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(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.

AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks);
acTextView.setThreshold(3);
acTextView.setAdapter(adapter);

That is it. Now execute and see it work. 
This is the way it would look:

The example code can be downloaded here.

31 comments:

  1. hi,very nice application,but when i type * in autotext all the arraay elements should appear in screen as same as Api demos
    (Contacts with Hint:
    Demonstates an autocomplete text box that understands the * wildcard.) in android.can u please help me

    ReplyDelete
  2. Hi, very nice example. I was wondering how to go about grabbing the index of the String[] that auto complete uses?

    Thanks,
    Andrew

    ReplyDelete
  3. Hi,

    how can i print the statement("any print statement") when the array is not match to the items.

    ReplyDelete
  4. that's rite @watson, i can't handle it too.. :(
    how 'bout you now?

    ReplyDelete
  5. Naveen Sabesan2 April 2011 at 18:16

    thanku so much for :) it really helped :)

    ReplyDelete
  6. hi,
    provide me the build automation tools for android apps.

    ReplyDelete
  7. Great post. I really appereciate your work done in developing this blog.
    Thankfully for this unique article, I will definitely add this website to my own rss feeds, a buddy basically told me concerning this a while back.I just stumbled across your site. Pretty interesting topic, it really makes me think.

    ReplyDelete
  8. The post is pretty interesting. I really never thought I could have a good read by this time until I found out this site. I am grateful for the information given. Thank you for being so generous enough to have shared your knowledge with us.

    ReplyDelete
  9. Hi Geetha, Krishna here, HOw to fill a textview with large string?

    ReplyDelete
  10. hi, very helpful article, but i am facing problem in dynamically updating the auto complete adapter, and even unable to handle click event on it.
    so please can you tell me appropriate way to implement it into my code of logic.

    Thanks.

    ReplyDelete
  11. Hi Great tutorial ,but as you took an strings collection and made an addapter for that but i want to use an adapter which will collect to the phone contacts or say sqlite database how to do this can you explain please.........thanks in advance

    ReplyDelete
  12. Thanks for the afflatus I was fatigued by appointment but i learn that action is about alive to the fullest and able every moment.

    ReplyDelete
  13. The website is amazing, I have a look at out it continually and always has interesting information. Considered and very well-organized.Best Hosting Service

    ReplyDelete
  14. As when i make a visit to this website, I was really curious to read this post. This post is really awesome.

    ReplyDelete
  15. I truly liked your incredible content. Please keep up the good work. Thank you very much !!!!

    Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

    ReplyDelete
  16. I must show my thanks to the writer just for rescuing me from such a crisis. As a result of looking throughout the world-wide-web and seeing thoughts which were not productive, I assumed my entire life was well over. Being alive devoid of the solutions to the difficulties you’ve resolved as a result of your entire site is a serious case, as well as the kind which could have in a negative way affected my entire career if I had not discovered the website. Your personal natural talent and kindness in dealing with almost everything was helpful. I’m not sure what I would have done if I hadn’t come upon such a point like this. I am able to at this point relish my future. Thanks so much for your high quality and effective help. I will not hesitate to propose your web site to any individual who needs to have counseling on this problem.

    ReplyDelete
  17. Ecommerce is a really good way to boost your business in many terms it will help in taking your business in the next level.
    Ecommerce Software

    ReplyDelete
  18. Thank you mam for writing this valuable tutorial.

    ReplyDelete
  19. This is a great post; it was very informative. I look forward in reading more of your work. Also, I made sure to bookmark your website so I can come back later. I enjoyed every moment of reading it. I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. Great stuff as usual...
    can i lose weight |
    vitamind 2

    ReplyDelete
  20. Sai Geetha Can you tell me or show me how to write the android code to auto populate forms? you can e-mail me the answer at appdesignsbyjoe@gmail.com
    Look forward to hearing back from you

    ReplyDelete
  21. This is a great post; it was very informative. I look forward in reading more of your work.

    ReplyDelete
  22. I think this auto suggestion box will surely help all of us. Goo and thank for this innovative idea.

    ReplyDelete
  23. I could tell how great you are in your field of interest. You could relate in each detail very well. Thank you for spending a time on sharing such informative writings to us.

    ReplyDelete
  24. Hi,I have a small problem in AutoCompleteText view.The suggestion direction changed by default based on the EditText position(like DropDown or BottomUp).But i want to fix the suggestion direction only BottomUp is this possible.

    ReplyDelete
  25. hello mem i want to that when i press dot (.) in a AutoCompleteTextView it automatically add that string which required.. like when we type in any browser www. it automatically search of website name after that when we type dot(.) it also added .com,.in automatically what i do for it.plzz reply..thanxx in advance..

    ReplyDelete
  26. good support for beginners ... really thank u sis....

    ReplyDelete
  27. Simple and Great ...... Thanks

    ReplyDelete
  28. In android,How to Insert Auto complete text view within creating popup. Help me.

    ReplyDelete