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
More Posts related to Android,
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
More Posts:
- Changing Android Intent Tittle using java code - Android
- Angular CLI ng Error: You need to specify a command before moving on. Use --help to view the available commands - HowTos
- Ubuntu Linux: Unzip a zip file using Terminal - Ubuntu
- Android Studio : Build Tools 23.0.0 rc1 is deprecated - Android-Studio
- Fahrenheit to Celsius Temperature Convertor: Tool & Formula - Tools
- 33: Python Program to send an email vid GMail - Python
- How to recover SharePoint FARM if the SQL Server IP changes - SharePoint
- [Fix] Ubuntu E: Unable to locate package sudo error - Ubuntu