Android AutoCompleteTextview With Custom Layout

May 25,2020

Coding Demos


Hi and welcome to another tutorial from CodingDemos :)

Today you will learn how to use Android AutoCompleteTextview that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down list from which the user can choose an item to replace the content of the edit box.

You will also learn how to create a custom Android AutoComplete by changing the text size, text style, and add a line that separates the items.

You will create a custom layout that contains Android textview and view. This textview will show the name of the item while the view is used to show a horizontal line that will separate the items inside AutoCompleteTextview drop down.

Here are the steps:
1- Open up Android Studio.

2- Add Android AutoCompleteTextview inside the activity_main.xml file.

3- Open up MainActivity.java file and reference the view AutoCompleteTextview based on its ID.

4- Create an ArrayAdapter that will hold the items of AutoCompleteTextview. This adapter will link to the items that were initialized inside the Strings.xml file.

5- Build and run the app to see the result.

6- Now that you have the basic AutoCompleteTextview showing successfully inside the emulator, next you need to customize the appearance by creating a custom layout called layout_item_autocomplete.xml file.

7- Inside layout_item_autocomplete.xml file, you need to add a textview that will be used to show the name of the item, while you add the other view that will be used as a horizontal line that separates the items. Now change the text size and style of this textview.

8- Next you need to change the ArrayAdapter resource file with the custom file that you've just created together with the textview ID.

9- Build and run the app to see the output.

10- You can change the background color of the AutoCompleteTextview drop down using android:popupBackground.

11- You can set a hint for the AutoCompleteTextview using android:completionHint.

12- You can also change the color of the AutoCompleteTextview selected item using android:dropDownSelector.

13- Build and run the app to see the final result :)

Links:
Android developer documentation: https://developer.android.com/reference/android/widget/AutoCompleteTextView
Website: https://www.codingdemos.com
FaceBook: https://www.facebook.com/Codingdemos

android developmentandroid studioandroid studio tutorialandroid tutorialjavaandroid autocompletetextviewandroid autocompletetextview tutorialandroid autocompletetextview dropdownautocompletetextview in android studioautocompletetextview androidautocompletetextview exampleandroid edittext dropdownandroid edittext drop down listandroid studio drop down in edittextandroid edittext autocompletecustom autocompletetextview androidandroid