In this article, we will learn how to implement the Android AutoCompleteTextView. This text view can be editable just like an EditText. The only difference is it comes with a drop-down list. And that list is visible only when we click on that AutoCompleteTextView. Anyway, let’s get started!!

This is a complete step by step guide. So first and foremost we need to add this below snippet in our layout file. Also, remember to assign an ID so that we can play with this AutoCompleteTextView in Java.

<AutoCompleteTextView
        android:id="@+id/auto_text"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        />

Here is how we should initialise an AutoCompleteTextView in Java.

AutoCompleteTextView auto_text = findViewById(R.id.auto_text);

Once we have initialised the AutoCompleteTextView. Our next step should be to prepare a list that we are gonna show on the drop-down list. As a sample, copy this below list.

String[] platformList = {
            "Youtube",
            "Facebook",
            "Telegram",
            "Instagram",
            "Twitter",
            "Twitch"};

Now we can create an ArrayAdapter with the above list and set that adapter into the AutoCompleteTextView. As shown below.

ArrayAdapter adapter = new ArrayAdapter(this,                // Context
         android.R.layout.simple_dropdown_item_1line,        // Default layout of Android Resource
         android.R.id.text1,                             // Default ID 
         platformList);                                             // Here is our List

auto_text.setAdapter(adapter);                                           // Finally set the adapter

Now if we build our project you can see an empty AutoCompleteTextView. And if you click on that TextView or Simply type something in it. You will notice some suggestions come just below that TextView.

Example of the Android AutoCompleteTextView

Here is the complete code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AutoCompleteTextView auto_text = findViewById(R.id.auto_text);

        String[] platformList = {
                "Youtube",
                "Facebook",
                "Telegram",
                "Instagram",
                "Twitter",
                "Twitch"};

        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_dropdown_item_1line,
                android.R.id.text1,
                platformList);
        auto_text.setAdapter(adapter);
}

Why OnClickListener?

Now let’s take a situation, let’s say we need to show the list when a user simply Taps on that AutoCompleteTextView. That means we need to set an OnClickListener or OnTouchListener. Right! At the same time, we need to call showDropDown() and clearFocus() as well. As shown below,

auto_text.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                
                auto_text.showDropDown();
                paramView.clearFocus();
                return false;
            }
});

Also, we can use the OnItemClickListener interface, in order to get the clicked item from the drop-down list.

auto_text.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
               String selectedItem = String.valueOf(adapterView.getItem(position));
                Toast.makeText(MainActivity.this, "Item: " + selectedItem, Toast.LENGTH_SHORT).show();
            }
});

So here in the above code snippet, onItemClick method providing four different types of variables a) AdapterView: This is representing the ArrayAdapter, b) View: This is representing the TextView, c) Int: This is for the item’s position and d) Long: This is also giving us the item’s position in long format.

I hope you got all the points of Android AutoCompleteTextView. Anyway if you liked this article then please Subscribe to our Youtube channel for keeping this talk continue. You can also find us on Facebook and Instagram. Thanks for your support.

Translate »