I have set mButton.setClickable(false);
in my code but still this button is invoked by global button.setOnClickListener
of my code.
EDIT:
Use View.setOnClickListener()
before View.setClickable()
,or the method setOnclickLisnter()
will set the flag true.
I wanted to do it on Spinner, and only this one worked for me:
spinner.setOnTouchListener { v, event ->
return@setOnTouchListener true
}
This will work in case of Imageview as well as the button.
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (imageview.isEnabled()){
//I have wrapped all code inside onClick() in this if condition
//Your onClick() code will only execute if the imageview is enabled
//Now we can use setEnabled() instead of setClickable() everywhere
}}
};
Inside onCreate()
, you can do setEnabled(false)
which will be equivalent to setClickable(false)
.
We are able to use setEnabled()
as tag because it's state remains uneffected on invocation of click (unlike setClickable()
whose state changes).
Put setClickable after setOnClickListener
mBtn.setOnClickListener(this);
mBtn.setClickable(false);
if you put setClickable(false) before setOnClickListener(this), it doesn't work.
Like Other friends said, setOnClickListener will override the flag to true.
So the Workaround is to setOnTouchEvent return true
whenever you want to disable clicks and set it to retrun false
when you want to enable click events.
This is because onTouchEvent is called before every clickListener you define for a view, so returning true will say to all listeners that :
"Ok, I received this event here, nobody else can receive it".
So your solution may be something like this:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
HashMap<String, Object> cTa= new HashMap<String, Object>();
cTa= d.getPosition(position)
Button mButton = (Button)convertView.findViewById(R.id.mBtn);
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
if(success)
{
// section-1
mButton.setOnTouchListener((v, event) -> false);
}
else{
// section-2
mButton.setOnTouchListener((v, event) -> true);
}
wrapper.getButton().setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//operation
}
});
return row;
}
someView.setOnClickListener(null)
As @Jan notes, the setOnClickListener
enables the click listener automatically. Therefore, a null click listener can be set to disable future clicks. After setting the view to a null click listener, there are no adverse effects to future clicks on that view.