Android tutorial : AutoCompleteTextView example


AutoCompleteTextView is sort of hybridization between an EditText and Spinner. It is a subclass of EditText.
With auto-complete,the completion suggestions are  automatically shown while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.The different  suggestions are displayed in a list of choices that looks like a Spinner. The user can then continue its input (if the word is not in the list) or choose an entry in it which will become the value of the field. 
In addition, the property android: completionThreshold of AutoCompleteTextView allows  to specify a minimum number of characters to be written before the list of proposals  appears. 
We can provide to AutoCompleteTextView an adapter containing the list of  Candidate values ​​using setAdapter () but as the user can enter text that is not in this list, AutoCompleteTextView does not use selection listeners. It is better to save a TextWatcher just like any EditText, to be notified when the text was changed. 
Here is an  xml code  example for  AutoCompleteTextView:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>
</LinearLayout>

The corresponding Java code is :
01import android.app.Activity;
02import android.os.Bundle;
03import android.text.Editable;
04import android.text.TextWatcher;
05import android.view.Menu;
06import android.view.MenuItem;
07import android.widget.ArrayAdapter;
08import android.widget.AutoCompleteTextView;
09import android.widget.TextView;
10
11public class MainActivity extends Activity
12implements TextWatcher {
13TextView selection;
14AutoCompleteTextView edit;
15String[] items={"Tunisia""Algeria""Egypt""France","Germany",
16"Tchad""USA""Cananda""Belgium""KSA",
17"UAE""China""Malta""Spain""UK",
18"Russia""South Africa""Danemark""Finland""Argentina",
19"Poland""Turkey""Oman""Italy""Singapur"};
20@Override
21public void onCreate(Bundle icicle) {
22super.onCreate(icicle);
23setContentView(R.layout.main);
24selection=(TextView)findViewById(R.id.selection);
25edit=(AutoCompleteTextView)findViewById(R.id.edit);
26edit.addTextChangedListener(this);
27edit.setAdapter(new ArrayAdapter<string>(this,
28android.R.layout.simple_dropdown_item_1line,
29items));
30}
31public void onTextChanged(CharSequence s, int start, intbefore,
32int count) {
33selection.setText(edit.getText());
34}
35public void beforeTextChanged(CharSequence s, int start,
36int count, int after) {
37
38}
39public void afterTextChanged(Editable s) {
40}
41}
This time, our activity implements the TextWatcher interface, which means that some methods must be called as OnTextChanged () and beforeTextChanged (). Here, only the first one concerns us: it changes the selection's label to reflect the current choice of the AutoCompleteTextView field.

Commentaires

Posts les plus consultés de ce blog

Ionic 2 : Envoie d'une image vers un serveur web à l'aide de PHP et Angular 2

Premier tutoriel avec Arduino et Proteus ISIS

Login et password ,formulaire d'authentification sous android avec mysql et json