In order to implement Autocomplete in Android EditText we need to use an AutoCompleteTextView View in the layout.xml file to represent an EditText.
This is a custom view that can be used to suggest text automatically while the user is typing.
The suggestions are displayed in the dropdown menu.
Lets see an Example :
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.code2care.example.autocompleteedittextexample.AutoCompleteEditText" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="@string/title" />
<AutoCompleteTextView
android:id="@+id/autocompleteEditTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:ems="10"
android:text=""
android:textColor="#000000" />
</RelativeLayout>
Activity.java file :
package com.code2care.example.autocompleteedittextexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteEditText extends ActionBarActivity {
AutoCompleteTextView autoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_edit_text);
String[] ProgLanguages = { "Java", "C", "C++", ".Net", "PHP", "Perl",
"Objective-c", "Small-Talk", "C#", "Ruby", "ASP", "ASP .NET" };
autoTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteEditTextView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, ProgLanguages);
//Used to specify minimum number of
//characters the user has to type in order to display the drop down hint.
autoTextView.setThreshold(1);
//Setting adapter
autoTextView.setAdapter(arrayAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.auto_complete_edit_text, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
⚠️ You can download the Source code for this tutorial at GitHub: https://github.com/code2care/AutoCompleteEditTextExample
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!