Skip to content Skip to sidebar Skip to footer

How To Set Setonclicklistener For Autocompletetextview?

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

Solution 1:

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.

publicvoidclicked(View v) { 
  // on click do ..
} 

Or you can set it directly in your code:

ACTV.setOnClickListener(newOnClickListener() {
    @OverridepublicvoidonClick(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(newOnItemClickListener() {
    @OverridepublicvoidonItemClick(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!

Solution 2:

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

Solution 3:

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

Solution 4:

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.

interfaceIOnClick{
    funonClick(model: Model)
}

overridefunonClick(model: Model) {
        viewModel.setFormDataOnIdSelection(Model)
        binding.tietName.dismissDropDown()
    }

overridefungetView(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.

Post a Comment for "How To Set Setonclicklistener For Autocompletetextview?"