How to create AutoCompleteTextView:
An editable text view that shows completion suggestions
automatically 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 drop down can be dismissed at any time by pressing the
back key or, if no item is selected in the drop down, by pressing the
enter/dpad center key.
The list of suggestions is obtained from a data adapter and
appears only after a given number of characters defined by the threshold.
First we create an Array to set data in AutoCompleteTextView
ArrayAdapter class is used to set the data on textview by using setAdapter method.
MainActivity.java:
public class MainActivity extends Activity {
AutoCompleteTextView
avt;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
avt =
(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final String[] arr = {"select
language","Java" ,"Android", "C", "C++"};
ArrayAdapter<String>
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,arr);
avt.setAdapter(adapter);
avt.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent
event) {
if(event.getAction() ==
KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
return false;
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:ems="10"
android:text=""
android:completionThreshold="1">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
**************************************************************************************************************************
Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com
No comments:
Post a Comment