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 } });
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.
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.
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.
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.
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(); } });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With