Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add listener to autocompletetextview, android?

I am trying to add listener that will react when an item is selected on the autocompletetextview...can anyone help //phonename is the autocompletetextview

PhoneName.setOnItemSelectedListener(new OnItemSelectedListener() {              public void onItemSelected(AdapterView<?> arg0, View arg1,                     int arg2, long arg3) {                 Toast.makeText(check.this," selected", Toast.LENGTH_LONG).show();              }              public void onNothingSelected(AdapterView<?> arg0) {                 // TODO Auto-generated method stub              }         }); 
like image 400
Tony Avatar asked Mar 08 '12 11:03

Tony


People also ask

How do you get the ID of the selected item of the AutoCompleteTextView in Android?

OnItemClickListener and set onItemClickListener() on the AutoCompleteTextView to get the user selected item value from the list. Notice that while using the ArrayAdapter , we have provided a layout object as argument android. R.

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when 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.

What is Android Completionhint attribute in AutoCompleteTextView?

This defines the hint view displayed in the drop down menu. This defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu.

How do I create an AutoCompleteTextView field in XML?

In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically. First we create a new project by following the below steps: Click on File, then New => New Project. After that include the Kotlin support and click on next.


2 Answers

try this:

phoneName.setOnItemClickListener(new OnItemClickListener() {          @Override         public void onItemClick(AdapterView<?> parent, View arg1, int pos,                 long id) {               Toast.makeText(check.this," selected", Toast.LENGTH_LONG).show();          }     }); 
like image 150
Nishant Avatar answered Sep 30 '22 15:09

Nishant


In kotlin, this would be:

autoCompleteTextView.setOnItemClickListener { _, _, position, _ ->      // You can get the label or item that the user clicked:     val value = adapter.getItem(position) ?: ""     Toast.makeText(this, value, Toast.LENGTH_LONG).show(); } 

I also recommend you name your variables starting with a lowercase letter to not confuse them with types.

like image 44
Terry Avatar answered Sep 30 '22 16:09

Terry