0% found this document useful (0 votes)
9 views

4.3.4 Autocomplete

AutoCompleteTextView is a subclass of EditText that provides a dropdown list of suggestions while the user types. It includes various attributes like id, text, hint, textColor, and methods for setting up the view and its behavior. Additionally, it utilizes ArrayAdapter to display a list of single-type items, making it suitable for showing suggestions such as contacts or country names.

Uploaded by

Rajwardhan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

4.3.4 Autocomplete

AutoCompleteTextView is a subclass of EditText that provides a dropdown list of suggestions while the user types. It includes various attributes like id, text, hint, textColor, and methods for setting up the view and its behavior. Additionally, it utilizes ArrayAdapter to display a list of single-type items, making it suitable for showing suggestions such as contacts or country names.

Uploaded by

Rajwardhan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

AutoCompleteTextView-

-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-->

Setting Text Of AutoCompleteTextView In Java class:

AutoCompleteTextView autoView = AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);


autoView.setText("Country");
4. hint: hint attribute gives the hint to the user that what should he Enter in this AutoCompleteTextView. Whenever he start
to type in AutoCompleteTextView the hint will automatically disappear.

<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Country Name Here" /> <!--display hint-->

Setting hint For AutoCompleteTextView In Java class:


AutoCompleteTextView autoView = (AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);
autoView.setHint("Enter Your Name Here");
5. textColor: This attribute set the color of a text in AutoCompleteTextView. Color value can be in the form of “#argb”, “#rgb”,
“#rrggbb”, or “#aarrggbb”.

<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-->

Setting TextColor of AutoCompleteView Text In Java class:


AutoCompleteTextView AutoView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);
AutoView.setTextColor(Color.RED);//red color for text
6. textColorHint: This attribute is used to set the color of displayed hint.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Name Here"
android:textColorHint="#0f0"/><!--green color for hint-->

Setting Color to Hint Of AutoCompleteTextView In Java class:


AutoCompleteTextView AutoView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);
AutoView.setHintTextColor(Color.green(0));
7. textSize: This attribute set the size of text in AutoCompleteTextView. We can set the text size in sp(scale independent pixel)
or dp(density pixel).
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"
android:textSize="25sp" /><!--set the text size-->

Setting Text Size In Java class:


AutoCompleteTextView simpletView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);
simpleView.setTextSize(25);
8. textStyle: textStyle attribute is used to give text style of a AutoCompleteTextView. We can add bold, italic and normal style.
If we want to use two or more styles for a AutoCompleteTextView then “|” operator is used for that.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"
android:textSize="25sp"
android:textStyle="bold|italic"/><!--bold and italic text style-->
9. background and padding: background attribute is used to set the background of a AutoCompleteTextView. We can set a
color or a drawable in the background of a AutoCompleteTextView.
padding attribute is used to set the padding from left, right, top or bottom.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:hint="Enter Your Name Here"
android:padding="15dp"
android:textColorHint="#fff"
android:textStyle="bold|italic" />
Setting Background of AutoCompleteTextView In Java class:
AutoCompleteTextView simpleView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);
simpleView.setBackgroundColor(Color.BLACK);//set black background color
Using Array Adapter To Display Text Values In AutoCompleteTextView:

-To display the Array content in an autocompletetextview we need to implement Adapter. In


AutoCompleteTextView we mainly display text values so we use Array Adapter for that.

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:

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)


An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds
the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on
different views like listview,gridview.
• Whenever you have a list of single type of items which is backed by an array, you can use ArrayAdapter. For instance, list of
phone contacts, countries or names.
• By default, ArrayAdapter expects a Layout with a single Textview.

Here is code of ArrayAdapter in Android:


ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Parameters used in ArrayAdapter:
1. context:- The first parameter is used to pass the context means the reference of current class. Here this is a
keyword used to show the current class reference. We can also use getApplicationContext(), getActivity() in the place of this
keyword. getApplicationContext() is used in a Activity and getActivity() is used in a Fragment.
Example- ArrayAdapter arrayAdapter = new ArrayAdapter(this, int resource, int textViewResourceId, T[] objects);
2. resource:- The second parameter is resource id used to set the layout(xml file) for list items in which you have a text view.
Below is the example code in which we set the layout.

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, int textViewResourceId, T[] objects);

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.

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, T[] objects);

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.

String animalList[] = {"Lion","Tiger","Monkey","Elephant","Dog","Cat","Camel"};

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, animalList);


Methods--
1. void setThreshold(int threshold)- Specifies the minimum number of characters the user has to type in
the edit box before the drop down list is shown.
2. void setAdapter(T adapter)- Changes the list of data used for auto completion.
Example-

You might also like