Hello Readers! In this post, we are going to learn about android MultiAutoCompleteTextView. We will also go through different attributes of android MultiAutocompleteTextView.
Output
Getting Started
MultiAutoCompleteTextview can be defined as below –
Android MultiAutoCompleteTextview is an extension of the AutoCompleteTextview that can show suggestions as a list in the dropdown while the user is typing in it. As the name suggests, we can select multiple suggestions from the dropdown. The selected suggestion will be appended to the text already entered in the MultiAutoCompleteTextview.
But, in the AutoCompleteTextView, scenario was little bit different. In AutoCompleteTextview, the selected suggestion replaces the whole text in the view.
Attributes of Android MultiAutoCompleteTextview Widget
Some of the popular attributes of MultiAutoCompleteTextview are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:completionHint | Specifies hint to show in the dropdown menu |
2 | android:dropDownHorizontalOffset | Sets amount of pixel by which dropdown is to be offset horizontally |
3 | android:dropDownSelector | Specifies selector in dropdown list |
4 | android:dropDownWidth | Specifies width of the dropdown |
Some of the popular attributes of MultiAutoCompleteTextView inherited from TextView are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:height | Height of the view |
2 | android:width | Width of the view |
3 | android:textStyle | Style of the text. For example, bold, italic or bolditalic etc. |
4 | android:textSize | Size of the text |
Some of the popular attributes of MultiAutoCompleteTextview inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Alpha of the view |
2 | android:background | Background of the view |
3 | android:clickable | Defines whether view is clickable or not |
4 | android:focusable | Defines whether view can take focus or not |
Example of Android MultiAutoCompleteTextview Widget
At first, we will create an android application. Then, we will use MultiAutoCompleteTextview widget into this application.
1. Creating New Project
Follow the steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as MultiAutoCompleteTextview. Then, 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. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use MultiAutoCompleteTextview widget in it.
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>
Note that we have defined an array in strings.xml file. This array will be used to show suggestions as list in the dropdown of the widget.
3. Use MultiAutoCompleteTextView Widget in xml file
Open res/layout/activity_main.xml file. Then, 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 used MultiAutoCompleteTextView and Button widgets. Now, we will access this widget in java file to perform some action on it.
4. Access MultiAutoCompleteTextView Widget in java file
Open src/main/java/com.tutorialwing.multiautocompletetextview/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.multiautocompletetextview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.MultiAutoCompleteTextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final MultiAutoCompleteTextView multiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView); // Get the string array String[] countries = getResources().getStringArray(R.array.countries_array); // Create the adapter and set it to the MultiAutoCompleteTextView ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countries); multiAutoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView.setThreshold(1); multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); Button button = findViewById(R.id.button); if (button != null) { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = "Entered text:" + " " + multiAutoCompleteTextView.getText(); Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); } }); } } }
In MainActivity.java file, we have accessed MultiAutoCompleteTextView. Then, we have set an adapter that contains list of countries. Data in the adapter is shown as a list in the dropdown of multiAutocompleteTextview while user is typing in the view. Also, we have provided MultiAutoCompleteTextView.Tokenizer to distinguish between different text entered by the user. You must provide MultiAutoCompleteTextView.Tokenizer in this view. setThresold method is responsible to set character count after which suggestions will be shown to the user while user is typing in the view.
At last, we set click listener on the button that displays entered text in the MultiAutoCompleteTextview widget.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/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 you run the application, you will get output as shown above.
That’s end of tutorial on Android MultiAutoCompleteTextview widget.
You must be logged in to post a comment.