4.3.4 Autocomplete
4.3.4 Autocomplete
-A AutoCompleteTextView is a view that is similar to EditText, except that it shows a list of completion suggestions
automatically while the user is typing. The list of suggestions is displayed in drop down menu. The user can choose an item
from there to replace the content of edit box with.
-Android AutoCompleteTextView is the subclass of EditText class.
Attributes of AutoCompleteTextView:
1. id: id is an attribute used to uniquely identify a text AutoCompleteTextView.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
2. text: text attribute is used to set the text in a AutoCompleteTextView. We can set the text in XML as well as in the java
class.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"/> <!--display text "Country"-->
3. gravity: The gravity attribute is an optional attribute which control the alignment of the text like left, right, center, top,
bottom, center_vertical, center_horizontal etc.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"
android:gravity="right"/> <!--right gravity for text-->
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Country Name Here" /> <!--display hint-->
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"
android:textColor="#f00"/><!--red color for text-->
ArrayAdapter In Android:
ArrayAdapter is used when we need list of single type of items which is backed by an Array. For
example, list of phone contacts, countries or names.
ArrayAdapter code:
3.textViewResourceId:- The third parameter is textViewResourceId which is used to set the id of TextView where you want to
display the actual text.
Below is the example code in which we set the id(identity) of a text view.
4. objects:- The fourth parameter is an array of objects, used to set the array of elements in the textView. We can set the
object of array or array list here.
Below is the example code in which we set the Animal array in adapter to display the Animal name’s list.