Android MultiAutoCompleteTextview Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about how to use android MultiAutoCompleteTextView using kotlin in any android application. We will also learn about different attributes of android MultiAutoCompleteTextview that can be used to customise this widget.

Output

Tutorialwing Android MultiAutoCompleteTextView Output Android MultiAutoCompleteTextView using Kotlin

Tutorialwing Android MultiAutoCompleteTextView Output

Getting Started

MultiAutoCompleteTextView is an editable textView, 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.

Different Attributes of Android MultiAutoCompleteTextview Widget

Some of the popular attributes of MultiAutoCompleteTextView inherited from AutoCompleteTextView are –

Sr. XML Attributes Description
1 android:completionHint Sets hint shown in the dropdown menu
2 android:completionHintView Sets hint view shown in the dropdown menu
3 android:completionThreshold Sets number of character that user must type before suggestion is displayed in drop down menu
4 android:dropDownSelector Sets selector in dropdown list

Some of the popular attributes of MultiAutoCompleteTextView inherited from TextView are –

Sr. XML Attributes Description
1 android:height Sets height of the view
2 android:width Sets width of the view
3 android:textStyle Sets style of the text. For example, bold, italic or bolditalic etc.
4 android:textSize Sets Size of the text

Some of the popular attributes of MultiAutoCompleteTextView inherited from View are –

Sr. XML Attributes Description
1 android:alpha Sets alpha of the view
2 android:background Sets background of the view
3 android:clickable Sets whether view is clickable or not
4 android:focusable Sets whether view can take focus or not



Example of Android MultiAutoCompleteTextview Using Kotlin

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as MultiAutoCompleteTextView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">MultiAutoCompleteTextView</string>

	<string-array name="countries_array">
		<item>India</item>
		<item>Albania</item>
		<item>Algeria</item>
		<item>American Samoa</item>
		<item>Andorra</item>
		<item>Angola</item>
		<item>Anguilla</item>
		<item>Antarctica</item>
	</string-array>
</resources>

3. Use MultiAutoCompleteTextview Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

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

	<MultiAutoCompleteTextView
		android:id="@+id/multiAutoCompleteTextView"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:hint="Please enter something..."
		android:layout_margin="20dp"/>

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Submit"/>

</LinearLayout>

In activity_main.xml file, we have defined MultiAutoCompleteTextView and Button widgets. Attributes android:hint=”” is used to set hint in the MultiAutoCompleteTextView.

4. Access MultiAutoCompleteTextview using Kotlin file

Open src/main/java/com.tutorialwing.multiautocompletetextview/MainActivity.kt file and add below code into it.

package com.tutorialwing.multiautocompletetextview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.MultiAutoCompleteTextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val multiAutoCompleteTextView = findViewById<MultiAutoCompleteTextView>(R.id.multiAutoCompleteTextView)
        // Get the string array
        val countries = resources.getStringArray(R.array.countries_array)
        // Create the adapter and set it to the MultiAutoCompleteTextView
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, countries)
        multiAutoCompleteTextView.setAdapter(adapter)

        multiAutoCompleteTextView.threshold = 1
        multiAutoCompleteTextView.setTokenizer(MultiAutoCompleteTextView.CommaTokenizer())

        val button = findViewById<Button>(R.id.button)
        button?.setOnClickListener {
            val text = "Entered text:" + " " + multiAutoCompleteTextView.text
            Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
        }
    }
}

We have accessed MultiAutoCompleteTextView using kotlin file i.e. in MainActivity.kt file. Then, we have defined an adapter using array of strings i.e. countries_array, defined in strings.xml file.Then, we have set this adapter in multiAutoCompleteTextView. This adapter is responsible to display completion suggestions when user starts typing in the View.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.multiautocompletetextview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android MultiAutoCompleteTextView using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners