AutoCompleteTextView, subclass of EditText with Auto-complete function

AutoCompleteTextView is a subclass of EditText that shows completion suggestions automatically while the user is typing.


To implement AutoCompleteTextView in your app:

Add the AutoCompleteTextView to your layout:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidedittextchanged.MainActivity">

android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

android:id="@+id/autocompletetextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />






Edit res/values/strings.xml to add array that contains all text suggestions.

AndroidEditTextChanged

January
February
March
April
May
June
July
August
September
October
November
December




Set adapter using the string array for the AutoCompleteTextView
package com.blogspot.android_er.androidedittextchanged;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

AutoCompleteTextView autoCompleteTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView =
(AutoCompleteTextView)findViewById(R.id.autocompletetextview);

String[] suggestion = getResources().getStringArray(R.array.suggestion);
ArrayAdapter adapter =
new ArrayAdapter(this,
android.R.layout.simple_list_item_1, suggestion);
autoCompleteTextView.setAdapter(adapter);


}

}





Popular posts from this blog

OnePlus Releases OxygenOS 4.5 OTA For OnePlus 3 and OnePlus 3T

Report: Incredibly Accurate GPS Chips are Coming to Smartphones Next Year

Black Friday saw the lowest price yet for the Roomba j7 from iRobot.