Android

Android Auto Complete

Android Auto Complete

You may be familiar with the auto-complete text option on your android device. Ever wondered how it suggests various kinds of texts for the completion of whatever text we are writing? So, you can implement this Auto Complete feature in your android application with the help of AutoCompleteTextView class. Let’s understand it with an example: 

First, you need to create an application under a package for e.g. com.firstapp.ashulakhwan.greatlearning, and after that, some modifications are needed in some files such as MainActivity.java and activity_main.xml. And after that, you can simply run your application to test its working. 
Now, let’s see the contents of the files that we are going to use in our application development:

MainActivity.java file will have the following content:

package com.firstapp.ashulakhwan.greatlearning;

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.View;
import android.view.MenuItem;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation;

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


public class MainActivity extends Activity {
   AutoCompleteTextView auto1;
   MultiAutoCompleteTextView auto2;
   String[] languages={"Android ", "Python", "C", "SQL", "JAVA", "Web services"};
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      auto1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
      auto2=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
      
      ArrayAdapter adapter = new 
         ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
      
      auto1.setAdapter(adapter);
      auto1.setThreshold(1);
      
      auto2.setAdapter(adapter);
      auto2.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
   }
}

The other file activity_main.xml will look like this:

<xml version="1.0" encoding="utf-8"?>
<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="28dp"
      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="Great Learning"
      android:id="@+id/textView2"
      android:textColor="#ff61d0"
      android:textSize="42dp"
      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/greatlogo"
      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="12"
      android:layout_below="@+id/imageView"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView"
      android:layout_marginTop="75dp"
      android:hint="Auto complete text 2">
      <requestFocus />
   </AutoCompleteTextView>
   
   <MultiAutoCompleteTextView
      android:id="@+id/multiAutoCompleteTextView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="12"
      android:layout_below="@+id/autoCompleteTextView1"
      android:layout_alignLeft="@+id/autoCompleteTextView1"
      android:layout_alignStart="@+id/autoCompleteTextView1"
      android:hint="Auto Complete Options" />
      
</RelativeLayout>

At last, we are going to modify the AndroidManifest.xml file that has the following content:

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

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.firstapp.ashulakhwan.greatlearning.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>

Now, I assume that you are done with all these modifications and you can now run your application. You need to run the application in an Android Emulator or else you can run it on an android device for testing. 

Top course recommendations for you

    Docker Swarm
    1 hrs
    Beginner
    1.8K+ Learners
    4.5  (126)
    Factorial Program in C
    2 hrs
    Beginner
    5.3K+ Learners
    4.46  (556)
    Jenkins Tutorial
    1 hrs
    Beginner
    8.7K+ Learners
    4.53  (710)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    4.1K+ Learners
    4.46  (325)
    Python Data Structures
    1 hrs
    Beginner
    31.8K+ Learners
    4.5  (2546)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.8K+ Learners
    4.39  (69)
    Priority Queue in C++
    1 hrs
    Beginner
    2.2K+ Learners
    4.38  (110)
    Introduction to MATLAB
    2 hrs
    Beginner
    22.5K+ Learners
    4.41  (1798)
    Packages in Python
    1 hrs
    Beginner
    7.4K+ Learners
    4.33  (568)
    Palindrome in Python
    2 hrs
    Beginner
    2.9K+ Learners
    4.65  (92)