Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a listener from a view in android

Is there any way to remove a Listener from a View in android? I have a Checkbox that I attached a CheckChangedListener to. The problem is that calling setChecked() on it causes my listener to fire.

If I can't just remove a listener, is there a way to prevent the listener from firing when I call setChecked() manually versus it being checked from a touch event?

like image 510
Falmarri Avatar asked Jan 09 '11 20:01

Falmarri


3 Answers

Well, I found the answer. This doesnt appear to be documented anywhere, but I went through the code for the View class and if you pass null to the setClickListener methods, it will remove the listener.

checkbox.setOnCheckChangedListener(null);

This should work for any event listener.

like image 101
Falmarri Avatar answered Nov 03 '22 17:11

Falmarri


Why have you got a checkChangeListener? If you'd use an onClicklListener it might just work for you?

like image 7
Nanne Avatar answered Nov 03 '22 17:11

Nanne


You could try setting the listener to one that does nothing before calling setChecked() and then putting the useful one back afterwards.

like image 2
Null Set Avatar answered Nov 03 '22 17:11

Null Set