Tony
Tony

Reputation: 1185

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

            }
        });

Upvotes: 46

Views: 46815

Answers (4)

Dmitry Sukhov
Dmitry Sukhov

Reputation: 61

There is a better way to get text of chosen item

phoneName.doOnTextChanged { text, start, before, count ->
        //do something with "text" variable
    }

Upvotes: 1

Tarif Chakder
Tarif Chakder

Reputation: 1856

Kotlin :

    v.autoCompleteTextView.setOnItemClickListener { parent, view, position, id ->
        Toast.makeText(requireContext(),"$position selected",Toast.LENGTH_LONG).show()
    }

Upvotes: 0

Terry
Terry

Reputation: 14867

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.

Upvotes: 9

Nishant
Nishant

Reputation: 32222

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();

        }
    });

Upvotes: 112

Related Questions