Android AutoCompleteTextView Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android autocompleteTextView widget in any android application. We will also go through different attributes of autocompleteTextView widget that can be used to customise it.


Tutorialwing Android Autocompletetextview Output Autocompletetextview widget tutorial with example

Tutorialwing Android Autocompletetextview Output

Getting Started

AutocompleteTextView widget can be defined as below –

AutocompleteTextView is an editable textview that shows completion suggestions while the user is typing in it. The suggestions are displayed as list in the dropdown menu. When user selects any suggestion, it replaces the entered text in autoCompleteTextView.

Attributes of Android AutocompleteTextview Widget

Some of the popular attributes of android AutoCompleteTextView widget are –

Sr. XML Attributes Description
1 android:completionHint It defines the hint shown in the dropdown menu
2 android:completionHintView It defines the hint view shown in the dropdown menu
3 android:completionThreshold It defines the number of character that user must type before suggestion is displayed in drop down menu
4 android:dropDownAnchor View to anchor the auto-complete dropdown to
5 android:dropDownHeight Specifies the basic height of the dropdown
6 android:dropDownHorizontalOffset It specifies the amount of pixel by which dropdown is to be offset horizontally
7 android:dropDownSelector Selector in dropdown list
8 android:dropDownWidth Specifies the basic width of the dropdown

Some of the popular attributes of android AutoCompleteTextView inherited from TextView are –

Sr. XML Attributes Description
1 android:height It specifies height of the view
2 android:width It specifies width of the view
3 android:textStyle It specifies style of the text. For example, bold, italic or bolditalic etc.
4 android:textSize It specifies size of the text

Some of the popular attributes of autoCompleteTextView inherited from View are –

Sr. XML Attributes Description
1 android:alpha Sets alpha of the view
2 android:background Sets Background of the view
3 android:clickable Specifies whether view is clickable or not
4 android:focusable Control whether view can take focus or not

Example of Android AutCompleteTextView Widget

At first, we will create android application. Then, we will use autocompleteTextView widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as AutoCompleteTextview. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use autoCompleteTextview widget in the application.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">AutoCompleteTextView</string>
	<string name="hint">Please type something...</string>
	<string name="submit">Submit</string>
	<string name="entered_text">Entered text:</string>

	<string-array name="countries_array">
		<item>American Samoa</item>


Note that we have also defined an array to show suggestions in the autoCompleteTextview.

3. Use AutoCompleteTextview Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

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




In activity_main.xml file, we have used autoCompleteTextview and button widgets. Now, we will access these widgets in java file and perform some action on it.

4. Access AutoCompleteTextview Widget in java file

Open src/main/java/com.tutorialwing.autocompletetextview/ file. Then, add below code into it.

package com.tutorialwing.autocompletetextview;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {

		final AutoCompleteTextView textView = findViewById(;
		// Get the string array
		String[] countries = getResources().getStringArray(R.array.countries_array);
		// Create the adapter and set it to the AutoCompleteTextView
		ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countries);

		Button button = findViewById(;
		if (button != null) {
			button.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					String text = getString(R.string.entered_text) + " " + textView.getText();
					Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();

In file, we have accessed autoCompleteTextview. Then, we set an adapter in it. Adapter contains the list of suggestions to show while user is typing in the view. The selected suggestion from the dropdown list replaces text entered by the user in the autoCompleteTextview.

Finally, we have set a click listener to the button that displays text entered by the user in the autoCompleteTextview.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.


Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.autocompletetextview"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of tutorial on Android AutoCompleteTextview widget.

Leave a Reply