activity_main.xmlMainActivity.java
<?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" android:padding="16dp" tools:context="com.codinginflow.multiautocompletetextviewexample.MainActivity"> <MultiAutoCompleteTextView android:id="@+id/mactv" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1" android:hint="Select countries" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="showInput" android:text="Show input" /> </LinearLayout>
package com.codinginflow.multiautocompletetextviewexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.MultiAutoCompleteTextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String[] COUNTRIES = new String[]{ "Afghanistan", "Albania", "Algeria", "Andorra", "Angola" }; private MultiAutoCompleteTextView editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.mactv); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COUNTRIES); editText.setAdapter(adapter); editText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } public void showInput(View v) { String input = editText.getText().toString().trim(); String[] singleInputs = input.split("\\s*,\\s*"); String toastText = ""; for (int i = 0; i < singleInputs.length; i++) { toastText += "Item " + i + ": " + singleInputs[i] + "\n"; } Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show(); } }