AutoCompleteTextViewDemo
AutoCompleteTextViewDemo
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.
Example
String[] Countries =
{ "India", "USA", "Australia", "UK", "Italy", "Ireland", "Africa" }
;
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoComp);
actv.setAdapter(adapter);
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top,
etc.
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;
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();
}
});
}
}