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

AutoCompleteTextViewDemo

AutoCompleteTextView in Android is an editable text view that displays a dropdown list of suggestions based on user input. It inherits properties from EditText and requires a minimum number of characters (threshold) to show suggestions. The document includes examples of how to create and set up AutoCompleteTextView in XML and Java, along with commonly used attributes.

Uploaded by

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

AutoCompleteTextViewDemo

AutoCompleteTextView in Android is an editable text view that displays a dropdown list of suggestions based on user input. It inherits properties from EditText and requires a minimum number of characters (threshold) to show suggestions. The document includes examples of how to create and set up AutoCompleteTextView in XML and Java, along with commonly used attributes.

Uploaded by

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

AutoCompleteTextView

 In android, AutoCompleteTextView is an editable text view which is used to show the


list of suggestions based on the user typing text.
 The list of suggestions will be shown as a dropdown menu from which the user can
choose an item to replace the content of the textbox.
 The AutoCompleteTextView is a subclass of EditText class so we can inherit all the
properties of EditText in AutoCompleteTextView based on our requirements.
 Following is the pictorial representation of using AutoCompleteTextView in android
applications.

 Generally, the dropdown list of suggestions can be obtained from the data adaptor and
those suggestions will be appeared only after giving the number characters defined in
the Threshold limit.
 The Threshold property of AutoCompleteTextView is used to define the minimum
number of characters the user must type to see the list of suggestions.
 The dropdown list of suggestions can be closed at any time in case if no item is selected
from the list or by pressing the back or enter key.

Create AutoCompleteTextView in Layout


File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/re
s/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoComp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Set the Text of Android


AutoCompleteTextView
 In android, we can set the text of AutoCompleteTextView control by
using setAdapter() method in Activity file.

 Example

String[] Countries =
{ "India", "USA", "Australia", "UK", "Italy", "Ireland", "Africa" }
;

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,


android.R.layout.simple_dropdown_item_1line, Countries);

AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoComp);

actv.setAdapter(adapter);

Android AutoCompleteTextView Attributes


Following are the some of commonly used attributes related to AutoCompleteTextView
control in android applications.

Attribute Description

android:id It is used to uniquely identify the control

android:gravity It is used to specify how to align the text like left, right, center, top,
etc.

android:text It is used to set the text.

android:hint It is used to display the hint text when text is empty


Attribute Description

android:textColor It is used to change the color of the text.

android:textColorHint It is used to change the text color of hint text.

android:textSize It is used to specify the size of text.

android:textStyle It is used to change the style (bold, italic, bolditalic) of text.

android:background It is used to set the background color for autocomplete textview


control

android:width It makes the TextView be exactly this many pixels wide.

android:height It makes the TextView be exactly this many pixels tall.

android:textColorHighlight It is used to change the color of the text selection highlight.

android:fontFamily It is used to specify the fontFamily for the text.

Android AutoCompleteTextView Control Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/re
s/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical"
android:id="@+id/linear_Layout">
<AutoCompleteTextView
android:id="@+id/ac_Country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="Enter Country Name"/>
</LinearLayout>
MainActivity.java
package com.example.autocompletetextviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{


String[] Countries =
{ "India", "USA", "Australia", "UK", "Italy", "Ireland", "Africa" };
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, Countries);
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.ac_Country);
actv.setThreshold(1);
actv.setAdapter(adapter);

actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View
view, int position, long id) {
Toast.makeText(getApplicationContext(), "Selected Item:
" + parent.getSelectedItem(), Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like