Use MultiAutoCompleteTextView in Android


Create new project in Android Studio with steps as below:




Open res\values\strings.xml file and add new string as below:

<resources>
    <string name="app_name">Learn Android with Real Apps</string>
    <string name="search_category">Search Category...</string>
</resources>

Open res\layout\activity_main.xml file and create layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextViewCategory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>




Add code to MainActivity.java file in android.demo.learnandroidwithrealapps package as below:

package android.demo.learnandroidwithrealapps;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MultiAutoCompleteTextView multiAutoCompleteTextViewCategory;

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

    private void initView() {
        multiAutoCompleteTextViewCategory = findViewById(R.id.multiAutoCompleteTextViewCategory);
    }

    private void loadData() {
        List<String> categories = new ArrayList<String>();
        categories.add("Mobile");
        categories.add("Computer");
        categories.add("Tivi");
        categories.add("Laptop");
        categories.add("Desktop");
        multiAutoCompleteTextViewCategory.setThreshold(1);
        multiAutoCompleteTextViewCategory.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        multiAutoCompleteTextViewCategory.setHint(getText(R.string.search_category));
        ArrayAdapter<String> categoryArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, categories);
        multiAutoCompleteTextViewCategory.setAdapter(categoryArrayAdapter);
        multiAutoCompleteTextViewCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                multiAutoCompleteTextViewCategory_onItemClick(adapterView, view, i, l);
            }
        });
    }

    private void multiAutoCompleteTextViewCategory_onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String category = adapterView.getItemAtPosition(i).toString();
        Toast.makeText(getApplicationContext(), category, Toast.LENGTH_SHORT).show();
    }

}