Autocomplete TextView is an editable view of the text that automatically provides suggestions for completion when the user is typing. A drop-down menu shows the list of options from which the user can select an object to replace the contents of the edit box with.
The drop down can be rejected at any time by pressing the back key, or by pressing the centre key enter / dpad if no object is selected in the drop down.
The list of suggestions is collected from a data adapter and appears only after the given number of characterr specified in the threshold.
The following code snippet illustrates how to create a text view that suggests names of different sesons while the user types:
In the below code, we have stored the seasons list in an ArrayAdapter with an android.R.layout.select_dialogue_item layout imported from the Android SDK and a threshold count of 1 depicts that we need to type in at least one character to show the autocomplete drop-down list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version=”1.0" encoding=”utf-8"?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_margin=”16dp”
android:orientation=”vertical”>
<android.support.v7.widget.AppCompatAutoCompleteTextView
android:id=”@+id/autoTextView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”Enter value”
android:textColor=”#000000"
android:textColorHint=”#000000" />
</LinearLayout>
|
Below code shows how to create adapter and set threshold
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| package com.example.demoproject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.widget.ArrayAdapter;
public class MainActivity extends AppCompatActivity {
private String[] seasons = {summer, “winter”, “autumn”, “monsoon”};
private AppCompatAutoCompleteTextView autoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoTextView = (AppCompatAutoCompleteTextView)
findViewById(R.id.autoTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, seasons);
//will start working from first character
autoTextView.setThreshold(1);
autoTextView.setAdapter(adapter);
}
}
|
The Android autocomplete only starts after one letter. Sometimes we need the autocomplete textview to show suggestion list even without typing on just click or even after clearing the text in the textbox.
Extend the AutoCompleteTextView, overriding the enoughToFilter() methods and the threshold methods so that it doesn’t replace the 0 threshold with a 1 threshold. The below class will show the autocomplete textview for threshold zero. Also when the autocomplete gains focus.
1
2
3
4
5
6
7
8
| <com.widgets.AutoCompleteTextViewCustom
android:id="@+id/editorText"
android:text="value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:inputType="text"
/>
|
Below code shows how to create adapter and set threshold
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| class AutoCompleteTextViewCustom : AutoCompleteTextView {
constructor(context: Context) : super(context) {
}
constructor(context: Context, attrs: AttributeSet) :
super(context, attrs) {
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
super(context, attrs, defStyleAttr) {
}
//Overriding the filter to show suggestions for 0 characters typed.
override fun enoughToFilter(): Boolean {
return true
}
//Show suggestions on focus changed.
override fun onFocusChanged(focused: Boolean, direction: Int,
previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
if (focused && adapter != null) {
if (windowVisibility == View.VISIBLE) {
performFiltering(text, 0)
showDropDown()
}
}
}
}
|