Introduction
This article explains how to use an array from string.xml in MultiAutoCompleteTextView.
The Android Text Control allows you to configure, style, and manipulate the controls in a variety of ways; we have many useful attributes we can use within the application to change the style, position and so on.
The four types of Text Controls in Android are:
- TextView
-
EditText
- AutoCompleteTextView
-
MultiCompleteTextView
MultiCompleteTextView: In this, when we enter a character regarding a predefined array, this will automatically show the list of items of the array. So in this application, we will show a list of animals that we will got from the string.xml file. In MultiCompleteTextView we will show this by writing this code:
- multiAutoCompleteTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
- ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,getResources().getStringArray(R.array.ItemAraay));
- multiAutoCompleteTextView.setAdapter(adapter);
- multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Step 1
Create a project like this:
Step 2
Create an XML file and write this:
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="@string/app_name"
- android:textStyle="bold"
- android:layout_centerHorizontal="true"
- android:textSize="20dp"/>
- <MultiAutoCompleteTextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/multiAutoCompleteTextView"
- android:completionThreshold="1"
- android:layout_marginTop="50dp"
- android:hint="enter alphabet"
- />
- </RelativeLayout>
Step 3
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <array name="ItemAraay">
- <item>Apple</item>
- <item>Ant</item>
- <item>Ass</item>
- <item>Bear</item>
- <item>Bat</item>
- <item>Ball</item>
- <item>Cat</item>
- <item>Crow</item>
- <item>cow</item>
- <item>Dog</item>
- <item>Dolphin</item>
- <item>Drum</item>
- </array>
- </resources>
Step 4
Create a Java file and write this:
- package com.multicompletetextview;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.ArrayAdapter;
- import android.widget.MultiAutoCompleteTextView;
-
- public class MainActivity extends Activity
- {
- MultiAutoCompleteTextView multiAutoCompleteTextView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
- ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.ItemAraay));
- multiAutoCompleteTextView.setAdapter(adapter);
- multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }
Step 5
Image