Monday, October 29, 2012

Using Autocomplete TextView

AutocompleteTextView is 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.

Create A new Project  AutocompleteTextView

Edit .xml file


open your .xml file and write foolowing  to get the following layout



<LinearLayout 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:orientation="vertical">
    
     <TextView
        android:id="@+id/textView1"
         android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose The Country Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
         android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Choose The Country" >

        <requestFocus />
    </AutoCompleteTextView>

    <TextView
        android:id="@+id/textViewCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="You Selected :"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/buttonSelectCountry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginTop="15dp"
        android:layout_gravity="center_horizontal"
        android:text="          OK          " 
        />

</LinearLayout>





Now Edit your Activitty  file



 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       String COUNTRIES[]={"INDIA","ITALY","JAPAN","USA","ICELAND","INDONESIA","UK","IRAN","IRAQ"};
        
        
        final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

        final TextView  textViewSelectedCountry=(TextView)findViewById(R.id.textViewCountry);
        Button btnSelectedCountry=(Button)findViewById(R.id.buttonSelectCountry);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

      // Set the adapter
        autoCompleteTextViewCountry.setAdapter(adapter);
        autoCompleteTextViewCountry.setThreshold(1);
        
        btnSelectedCountry.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

String country=autoCompleteTextViewCountry.getText().toString();
textViewSelectedCountry.setText("Selected Country: "+country);

}
});
        
       
    }

 
}


Point to Remember:

public void setThreshold (int threshold)

Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
When threshold is less than or equals 0, a threshold of 1 is applied.
Now run the project 
you get following 


Hope you like the post . Comments and Questions are welcome.



13 comments:

  1. drop down text not visisble only after clicking the drop down list text is visible can u help plz

    ReplyDelete
  2. where is this item: 'simple_dropdown_item_1line' coming from??? don't see it anywhere.....

    ReplyDelete
  3. how to change the color of dropdown list box

    ReplyDelete
  4. drop down text not visisble only after clicking the drop down list text is visible can u help plz

    ReplyDelete
  5. What is "ems" field in .xml file ?

    ReplyDelete
  6. The site is looking piece gaudy and it gets the guests eyes. Configuration is really basic and a decent easy to understand interface. LeReviewist

    ReplyDelete
  7. This a good way to appreciate the teacher as they put their efforts to train students. UK dissertation Writers appreciates the teachers.  agen asuransi allianz

    ReplyDelete
  8. Hello, Dear ladies! The best Escort agency offer adult erotic sex escort Job as a Escorts Girl Job in Gurgaon. Dear girls, welcome to our agency who provide you the best Female Escorts Job in Gurgaon which exist from many year! We are escort agency, but we work with different areas in India with luxury worldwide models. If you really want and serious about what we offer like: sex love Call Girls job in Gurgaon?

    The number of clients and works depends on the appearance of the girl, services, the quality of real photographs and selfies, and of course on a positive attitude as Escorts Girl Job in Gurgaon. We have the highest prices in Delhi escort and the highest percentage for girls. In escort companions in the Delhi, Russian girls have 60% of the income from the transaction, for example for an hour it is 15000-30000 Rs. so Join as a Female Escorts Job in Gurgaon. We are also open for Call Girls job in Gurgaon.

    ReplyDelete
  9. Avantech provides industry-leading geotechnical software for analyzing soil and rock slopes. Designed for civil engineering & mining. Visit us: https://avantech.in/product-category/engineering-software/geotechnical-modeling/

    ReplyDelete
  10. excel Printers Provide the best carry bag printing in Delhi and a Paper Carry bag printing Manufacturer. Though Carry bags in Delhi. Visit us: https://www.excelprinters.com/product/paper-carry-bag-printing-in-delhi/

    ReplyDelete
  11. Hi,
    Thanks for sharing the information with us it was very informative. https://hangup.in

    ReplyDelete