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
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- How to press shortcut CTRL + ALT + DEL on Windows Remote Desktop - HowTos
- How to remove app from Dock when closed [macOS Big Sur] - MacOS
- Get the Current Date using LocalDate in Java - Java
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available - Java
- Difference between using Scanner Class and String args for user input in Java - Java
- How to delete a dir or folder using Python code - Python
- Android: programmatically turn Bluetooth on or off using Java code - Android
- How to disable button in Bootstrap - Bootstrap