Easy Tutorial
❮ Verilog Intro Inner Lambda Final ❯

2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)

Category Android Basic Tutorial

Introduction to This Section:

>

This section continues to explore the widgets of the Adapter class, this time introducing the AutoCompleteTextView (auto-complete text box). I believe you have noticed that widgets related to Adapter can all define their own item styles, right? Or the layout of each item~ play as you wish~ Well, without further ado, let's start the content of this section~ By the way, here is the official API: AutoCompleteTextView


1. Related Attributes:

>

In addition, there is actually a MultiAutoCompleteTextView (auto-complete text box with multiple hints) that has similar functions and attributes to this AutoCompleteTextView. What is the specific difference between them? We will experience it in the following code~ Also, both of these are full-word matching, for example, "little pig": if you type "small" -> it will suggest "little pig", but if you type "pig pig" -> it will not suggest "little pig"!


2. Code Example:

Running Effect Picture :

Implementation Code :

Here we will not customize the layout, but directly use ArrayAdapter to implement it!

Layout file: activity_main.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/atv_content"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:completionHint="Please enter search content"
        android:completionThreshold="1"
        android:dropDownHorizontalOffset="5dp" />

    <MultiAutoCompleteTextView
        android:id="@+id/matv_content"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:completionThreshold="1"
        android:dropDownHorizontalOffset="5dp"
        android:text="" />

</LinearLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView atv_content;
    private MultiAutoCompleteTextView matv_content;

    private static final String[] data = new String[]{
            "Little Pig", "Little Dog", "Little Chicken", "Little Cat", "Little Meow Meow"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        atv_content = (AutoCompleteTextView) findViewById(R.id.atv_content);
        matv_content = (MultiAutoCompleteTextView) findViewById(R.id.matv_content);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.
                this, android.R.layout.simple_dropdown_item_1line, data);
        atv_content.setAdapter(adapter);

        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, data);
        matv_content.setAdapter(adapter2);
        matv_content.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}

Partial Code Analysis :

>

❮ Verilog Intro Inner Lambda Final ❯