Android MultiAutoCompleteTextview

Multi AutoComplete Textview

An editable text view, extending AutoCompleteTextView, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.

MultiAutoCompleteTextView can hold multiple string words value at single time. These all values are separated by comma(,).

Get GITHUB code from Here.

 

Creating New Project

  1. In Android Studio, go to File New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

  2. Open activity_main.xml in which I have add  one MultiAutoCompleteTextview with following basic properties.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:padding="18dp"
    tools:context="com.example.lenovo.multiautocompletetextview.MainActivity">

    <MultiAutoCompleteTextView
        android:id="@+id/mac_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:textSize="25sp" />

</android.support.constraint.ConstraintLayout>

3.Now open  MainActivity.java and and add the below code. The following code snippet shows
how to create a text view which suggests various countries names while the user is typing:

MainActivity.java

package com.example.lenovo.multiautocompletetextview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    String[] country = {"Australia", "Albania", "Belgium", "Bhutan", "Canada", "China", "India"};

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


        //We can use any of the built_in android layout simple_list_item_1 and select_dialog_item
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, country);

        MultiAutoCompleteTextView mac_tv = (MultiAutoCompleteTextView) findViewById(R.id.mac_tv);

        //Start character for search
        mac_tv.setThreshold(1);

        //Setting adapter
        mac_tv.setAdapter(adapter);

        // set tokenizer that distinguish the various substrings by comma
        mac_tv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        //Setting textcolor after selection
        mac_tv.setTextColor(Color.BLUE);
    }
}


Now when you run the app it will look like this:

 

Leave a Reply