24

I am selecting text for AutoCompleteTextView.After i want apply setonclicklistener to selected text.if any have idea.

ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, sampleACTV); 
 AutoCompleteTextView ACTV = (AutoCompleteTextView) findViewById(R.id.spinner);
 ACTV.setAdapter(arrAdapter); 

 }
 private static final String[] sampleACTV = new String[]
         { "android","androidpeople.com","iphone","blackberry" }; 

in my example i am selecting one like android call an intent to go to nest Acitivity

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
sai
  • 2,562
  • 8
  • 31
  • 46
  • http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener) – Nikunj Patel Dec 27 '11 at 12:19

5 Answers5

67

There are different click listeners in AutoCompleteTextView.

The first way is in the layout xml, you can define the onCLick attribute, with the function that you want to be called, in the example below, clicked.

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="clicked" />

Then, in your activity, you define the function clicked.

public void clicked(View v) { 
  // on click do ..
} 

Or you can set it directly in your code:

ACTV.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

If you want to set the click listener when the user clicks in an item in the dropdown list there is another method, the setOnItemClickListener.

ACTV.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
})

And you have a last option, to set the click listener when the user actually selects an item in the dropdown list using setOnItemSelectedListener.

ACTV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
    @Override
    public void onNothingSelected (AdapterView<?> parent) {
        //... your stuff
    }
})

References:

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Good luck!

Raiv
  • 5,731
  • 1
  • 33
  • 51
caiocpricci2
  • 7,714
  • 10
  • 56
  • 88
  • 1
    Don't you think `clicked` method should have `(Views view)` as argument? – Paresh Mayani Dec 27 '11 at 13:05
  • 1
    @caiocpricci2 it should be ACTV.setOnItemSelectedListener not ACTV.setOnSelectedListener – ajdeguzman Feb 25 '14 at 06:02
  • Declaring android:onClick="clicked" would give produce the following error. ```Caused by: java.lang.NullPointerException: Attempt to write to field 'android.view.View$OnClickListener android.widget.AutoCompleteTextView$PassThroughClickListener.mWrapped' on a null object reference``` – Kavin Raju S Oct 05 '20 at 04:44
2

You need to create Custom Adapter and assign OnClick event to the view in getView()

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 1
    Who Said this is not a good suggestion? You are assigning a click on a view and thats so natural right? – Cyph3rCod3r Sep 07 '15 at 07:51
  • Yes, but that view gets recycled, so it's a potential bug source. That being said, it works and in some cases it's the only choice. – sulai May 04 '18 at 18:56
0

Create a custom adapter and in that getView() method put setonclicklistenerforeach view and call interface method.

Override interface in fragment and pass that to adapter so onclick you'll get click in fragment.

interface IOnClick{
    fun onClick(model: Model)
}

override fun onClick(model: Model) {
        viewModel.setFormDataOnIdSelection(Model)
        binding.tietName.dismissDropDown()
    }

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    newView.setOnClickListener {
        iOnClick.onClick(resAllLeadsModel)
    }
}

binding.tietName.setOnItemClickListener { parent, view, position, id ->
    viewModel.setFormDataOnIdSelection(listOfItems[view.tag.toString().toInt()])
}

this will work as you wanted in every scenario.

Please if you have any suggestion do let me know thanks.

Karsh Soni
  • 170
  • 1
  • 12
0

This is a really old post so I'm not sure how helpful this will be but... in Kotlin now this is really easy. I have a text auto-complete that is populated with with values from a dictionary and accessing the dropdown text for an auto-complete called searchAutoComplete is ...

        searchAutoComplete.setOnItemClickListener { 
           parent, view, position, rowId ->
            Log.d(TAG, "searchAutoComplete: setOnItemClickListener," +
                    "view -> ${view.javaClass.simpleName}, " +
                    "pos = $position. " +
                    "text = ${(view as TextView).text}")
        }
steven smith
  • 1,519
  • 15
  • 31
-1

This works perfectly for me

Actvl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
    @Override
    public void onNothingSelected (AdapterView<?> parent) {
        //... your stuff
    }
});
Mustafa
  • 5,624
  • 3
  • 24
  • 40