Thursday, November 29, 2012

AutoCompleteTextView in android.

As we input character the item display accordingly as a hint. This is done using autocomplitedtextview in android.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <AutoCompleteTextView
        android:id="@+id/AutoCompleteTextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="This text here" />

</LinearLayout>

Code for Java file.

public class MainActivity extends Activity {

 private static final String[] samplearray = new String[] { "android", "android-phone", "android-tablet", "Nokia", "iphone", "ipad" };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, samplearray);
  AutoCompleteTextView autotxt = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView01);
  autotxt.setAdapter(arrAdapter);
 }

}

Output:






No comments:

Post a Comment