Android Snippet – Android AutoCompleteTextView Suggestions

Android Snippet – Android AutoCompleteTextView Suggestions

In this tutorial, we are going to learn how to create AutoCompleteTextView Suggestions in android application. AutoCompleteTextView is similar to android Editview but with the extra feature of text suggestions why typing in AutoCompleteTextView.

Text suggestion is an important feature in many android application and it also help app users to compete the text they intend to type after entering few characters.

According to Google’s developers guide – AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

Text suggestion can also be used in a ListView. You can read my post on Android ListView with Search Box or Android ListView with EditText Filter

Before we start soiling our hands with code, it is important for us to learn about the different attributes associated with AutoCompleteTextView

[table id=7 /]

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AutoCompleteTextViewSuggestions

Package: com.inducesmile.autocompletetextviewsuggestions

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

What we will do in our activity_main.xml file

1. In our activity_main.xml file, we will add an AutoCompleteTextView

The code for the xml file is shown below

<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">

<AutoCompleteTextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/autoCompleteTextView"

android:layout_marginTop="57dp"

android:layout_alignParentTop="true"

android:hint="@string/auto_complete"

android:layout_centerHorizontal="true" />

</RelativeLayout>

What we will do in our MainActivity.java file

1. We get the instance of the AutoCompleteTextView by using findViewById method of the activity class.

2. We created a String array and populate it with some data.

3. We create an instance of an ArrayAdapter and pass three parameters to its constructor.

4. We finally set the ArrayAdapter to our instance of AutoCompleteTextView.

Copy and paste this code sample to MainActivity.java file as shown below

package inducesmile.com.autocompletetextviewsuggestions;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends ActionBarActivity {

private AutoCompleteTextView autoCompleteTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);

String[]dataStorage = {"Nigeria", "Ghana", "Mali", "South Africa", "Cameroon", "Niger", "Algeria", "Zimbabwe"};

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.select_dialog_item, dataStorage);

autoCompleteTextView.setThreshold(2);

autoCompleteTextView.setAdapter(adapter);

}

@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);

}
}

Save the file and run your project. If everything works for you, an image like this will appear on your test device.

auto complete TextView

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

2 Comments

Add a Comment