AutoCompleteTextView:
AutoCompleteTextView is a single textbox that shows completion
suggestions automatically while the user is typing.The suggestions are
shown based on the list of the data that has supplied to it. 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.It consists
of a single text box which can show suggestions based on a list of data
that I provide as a source for this field.
MultiAutoCompleteTextView:
MultiAutoCompleteTextView is an editable text view, extending
AutoCompleteTextView, that can show completion suggestions for the
substring of the text where the user is typing instead of necessarily
for the entire thing.
Difference Between AutoCompleteTextView and MultiAutoCompleteTextView:
AutocompleteTextView only offers suggestions about the whole sentence
and MultiAutoCompleteTextView offers suggestions for every token in the
sentence. You can specify what is the delimiter between tokens.
AutoCompleteTextView is used for selectiong single Item where as MultiAutoCompleteTextView is used for for selecting multiple Items by using a delimiter(such as comma) in betwwen them.
AutoCompleteTextView is used for selectiong single Item where as MultiAutoCompleteTextView is used for for selecting multiple Items by using a delimiter(such as comma) in betwwen them.
Example:If you were writing an
email app, and you wanted the “To:” field to be an autocomplete field,
getting matches from an address book, chances you want to allow the user
to pick multiple recipients for a message, and would make this field a
MultiAutoCompleteTextView.
On the other hand, the “From:” field in the same example email app, you would need to enforce only a single selection by the user from their configured email accounts. And so an AutoCompleteTextView would be appropriate here.
On the other hand, the “From:” field in the same example email app, you would need to enforce only a single selection by the user from their configured email accounts. And so an AutoCompleteTextView would be appropriate here.
Sample Program that demonstartes AutoCompleteTextView and MultiAutoCompleteTextView:
- Create a new project say AutoCompleteView.
- Open main.xml(res/layout/main.xml) file and insert the following code.
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”>
<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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”/>
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”/>
<AutoCompleteTextView android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/autoCompleteTextView1″
android:text=”">
<requestFocus></requestFocus>
</AutoCompleteTextView>
android:layout_height=”wrap_content”
android:id=”@+id/autoCompleteTextView1″
android:text=”">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<MultiAutoCompleteTextView android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/multiAutoCompleteTextView1″
android:text=”MultiAutoCompleteTextView”>
</MultiAutoCompleteTextView>
android:layout_height=”wrap_content”
android:id=”@+id/multiAutoCompleteTextView1″
android:text=”MultiAutoCompleteTextView”>
</MultiAutoCompleteTextView>
</LinearLayout>
- Create list.xml file in res/layout and insert the following code
List.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<TextView xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”11dp”
android:textSize=”18sp”
android:textColor=”#fff”>
</TextView>
This TextView is used for each item that appears in list of suggestions.
<?xml version=”1.0″ encoding=”utf-8″?>
<TextView xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”11dp”
android:textSize=”18sp”
android:textColor=”#fff”>
</TextView>
This TextView is used for each item that appears in list of suggestions.
- Open AutoCompleteView.Java and insert the following code.
AutoCompleteView.java:
package com.example.autocomplete;
package com.example.autocomplete;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class AutoCompleteViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*AutoCompleteTextView*/
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
String[] Names= new String[]{“Anil”,”Anusha”,”Amrutha”,”Swathi”,”Swapna”,”swetha”,”Srinivas”};
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list, Names);
textView.setThreshold(1);
textView.setAdapter(adapter);
/*MultiAutoCompleteTextView*/AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
String[] Names= new String[]{“Anil”,”Anusha”,”Amrutha”,”Swathi”,”Swapna”,”swetha”,”Srinivas”};
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list, Names);
textView.setThreshold(1);
textView.setAdapter(adapter);
MultiAutoCompleteTextView textView1 = (MultiAutoCompleteTextView)
findViewById(R.id.multiAutoCompleteTextView1);
textView1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
String[] Months= new String[]{“January”,”February”,”March”,”April”,”May”,”June”,”July”,”August”,
“September”,”October”,”November”,”December”};
ArrayAdapter adapter1 = new ArrayAdapter(this, R.layout.list, Months);
textView1.setThreshold(1);
textView1.setAdapter(adapter1);
}
}
No comments:
Post a Comment