Android AutoComplete

Android AutoComplete

Android AutoComplete is used If you wish to get ideas , when you enter an editable words field , you are able to do this via Android AutoComplete TextView. It provides ideas automatically when the user is typing. The set of ideas is displayed in a drop down menu that the user can pick an item to switch this content of the edit box with.

To be able to use Android AutoComplete TextView you have to first create an Android Auto Complete TextView Field in the xml. Its syntax is listed below.

<AutoCompleteTextView
   android:id="@+id/autoCompleteTextView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="65dp"
   android:ems="10" >

 

From then on, you need to get a reference of the textview in java. Its syntax is listed below.

private AutoCompleteTextView actv;
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 

 

The the following point you must do is to designate the set of recommendations what to be displayed. You could designate the list items as a string array in java or in strings.xml. Its syntax is listed below.

String[] countries = getResources().getStringArray(R.array.list_of_countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);

 

The array adapter class is in charge of displaying the info as list in the advice box of the written text field. The setAdapter method is employed to create the adapter of the android AutoComplete TextView. From these methods apart, the other ways of Android Auto Complete here are shown.

Methods & Descriptions

getAdapter()

This method comes back a filterable list adapter used for car completion

getCompletionHint()

This method results optional hint text message displayed in the bottom of the the matching list

getDropDownAnchor()

This method profits returns the identification for the view that the auto-complete drop down list is anchored to.

getListSelection()

The positioning is delivered by this technique of the dropdown view selection, when there is one

isPopupShowing()

This method reveals if the popup menu is showing

setText(CharSequence wording, boolean filtration)

This method models word except that it can disable filtering

showDropDown()

This method exhibits the drop down on display.

Android AutoComplete Example

The below android autocomplete example shows the utilization of Android AutoComplete TextView course. It crates a simple application which allows you to enter and it shows ideas on your device.

To test out this android autocomplete example , you will need to perform this on a genuine device or within an emulator.

Steps & Descriptions

  1. You use Android Studio to make an Android software under a bundle package deal com.example.sappal.myapplication. While creating this job, be sure to Aim for SDK and Compile With at the latest version of Android os SDK to work with higher degrees of APIs.
  2. Modify src/MainActivity.java  to include android AutoComplete TextView code
  3. Modify design XML document res/structure/activity_main.xml add any GUI element if required.
  4. Run the application form and choose a jogging android os device and set up the application onto it and check the results.

This is actually the content of src/MainActivity.java

package com.example.sappal.myapplication;

import android.app.Activity;
import android.content.Context;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

import android.os.Bundle;
import android.os.Environment;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
import java.io.IOException;


public class MainActivity extends Activity {
   AutoCompleteTextView text;
   MultiAutoCompleteTextView text1;
   String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
      text1=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
      
      ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
      
      text.setAdapter(adapter);
      text.setThreshold(1);
      
      text1.setAdapter(adapter);
      text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

 

Here is the content of activity_main.xml

<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_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Android Auto Complete"
      android:id="@+id/textView"
      android:textSize="30dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/logo"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2" />
      
   <AutoCompleteTextView
      android:id="@+id/autoCompleteTextView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:layout_below="@+id/imageView"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView"
      android:layout_marginTop="72dp"
      android:hint="AutoComplete TextView">
      <requestFocus />
   </AutoCompleteTextView>
   
   <MultiAutoCompleteTextView
      android:id="@+id/multiAutoCompleteTextView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:layout_below="@+id/autoCompleteTextView1"
      android:layout_alignLeft="@+id/autoCompleteTextView1"
      android:layout_alignStart="@+id/autoCompleteTextView1"
      android:hint="Multi Auto Complete " />
      
</RelativeLayout>

Here is the content of Strings.xml

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

 

Here is the content of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.autocomplete"
   android:versionCode="1"
   android:versionName="1.0" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

Let’s make an effort to run the android autocomplete textview application. I suppose you have linked your AVD while doing android environment setup. To perform the software from Android Studio, available one of your project’s activity data files and click Run Eclipse Run Icon from the toolbar. Android os studio room will set up this program in your AVD as well as your AVD will screen pursuing display screen.

android auto complete

Now just enter the written text view to see ideas of the Dialects. As i simply type one notice that happen to be asa, and it shows me advice of vocabulary.

android auto complete

The multi android AutoComplete TextView shoes ideas for not only a expressed word but for whole text. As after writing word first , after i start writing the next word , it displays me the suggestions. This is shown in the picture below

android auto complete