Getting Array From String.xml File in MultiAutoCompleteTextView in Android

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:
  1. multiAutoCompleteTextView=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);  
  2. ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,getResources().getStringArray(R.array.ItemAraay));  
  3. multiAutoCompleteTextView.setAdapter(adapter);  
  4. multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 
Step 1
 
Create a project like this:
 
Clipboard02.jpg
 
Step 2
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:tools="http://schemas.android.com/tools"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.    android:paddingRight="@dimen/activity_horizontal_margin"  
  7.    android:paddingTop="@dimen/activity_vertical_margin"  
  8.    android:paddingBottom="@dimen/activity_vertical_margin"  
  9.    tools:context=".MainActivity">  
  10.    <TextView  
  11.       android:layout_height="wrap_content"  
  12.       android:layout_width="wrap_content"  
  13.       android:text="@string/app_name"  
  14.       android:textStyle="bold"  
  15.       android:layout_centerHorizontal="true"  
  16.       android:textSize="20dp"/>  
  17.    <MultiAutoCompleteTextView  
  18.       android:layout_width="fill_parent"  
  19.       android:layout_height="wrap_content"  
  20.       android:id="@+id/multiAutoCompleteTextView"  
  21.       android:completionThreshold="1"  
  22.       android:layout_marginTop="50dp"  
  23.       android:hint="enter alphabet"  
  24.       />  
  25. </RelativeLayout> 
Step 3
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    <array name="ItemAraay">  
  4.       <item>Apple</item>  
  5.       <item>Ant</item>  
  6.       <item>Ass</item>  
  7.       <item>Bear</item>  
  8.       <item>Bat</item>  
  9.       <item>Ball</item>  
  10.       <item>Cat</item>  
  11.       <item>Crow</item>  
  12.       <item>cow</item>  
  13.       <item>Dog</item>  
  14.       <item>Dolphin</item>  
  15.       <item>Drum</item>  
  16.    </array>  
  17. </resources> 
Step 4
 
Create a Java file and write this:
  1. package com.multicompletetextview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.MultiAutoCompleteTextView;  
  8.   
  9. public class MainActivity extends Activity   
  10. {  
  11.  MultiAutoCompleteTextView multiAutoCompleteTextView;  
  12.   
  13.  @Override  
  14.  protected void onCreate(Bundle savedInstanceState)   
  15.  {  
  16.   super.onCreate(savedInstanceState);  
  17.   setContentView(R.layout.activity_main);  
  18.   multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);  
  19.   ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.ItemAraay));  
  20.   multiAutoCompleteTextView.setAdapter(adapter);  
  21.   multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());  
  22.   
  23.  }  
  24.  @Override  
  25.  public boolean onCreateOptionsMenu(Menu menu)   
  26.  {  
  27.   // Inflate the menu; this adds items to the action bar if it is present.  
  28.   getMenuInflater().inflate(R.menu.main, menu);  
  29.   return true;  
  30.  }  
  31.   
Step 5
 
Image
 
Clipboard06.jpg