I have an object where the text cycles and displays status messages. When the messages change, I want the click event of the object to change to take you to the activity that the message is relating to.
So, I have a TextView mTitleView
and I'm assigning the event like this.
public void setOnTitleClickListener(OnClickListener listener) {
mTitleView.setOnClickListener(listener);
}
How do I remove that click event? There are some status messages that do not have an actionable area so I'd like to turn off the click event. I'd also like to be able to cycle through these click events and dispose of them properly, but I'm unsure of the best practice.
Setting
setOnClickListener(null)
is a good idea to remove click listener at runtime.And also someone commented that calling
View.hasOnClickListeners()
after this will returntrue
, NO my friend.Here is the implementation of
hasOnClickListeners()
taken fromandroid.view.View
classThank GOD. It checks for
null
.So everything is safe. Enjoy :-)
mTitleView.setOnClickListener(null)
should do the trick.A better design might be to do a check of the status in the OnClickListener and then determine whether or not the click should do something vs adding and clearing click listeners.
Note that if a view is non-clickable (a TextView for example), setting
setOnClickListener(null)
will mean the view is clickable. UsemMyView.setClickable(false)
if you don't want your view to be clickable. For example, if you use a xml drawable for the background, which shows different colours for different states, if your view is still clickable, users can click on it and the different background colour will show, which may look weird.Perhaps
setOnClickListener(null)
?The above answers seem flighty and unreliable. I tried doing this with an ImageView in a simple Relative Layout and it did not disable the onClick event.
What did work for me was using setEnabled.
You can then check whether the View is enabled with:
Another option is to use setContentDescription(String string) and String getContentDescription() to determine the status of a view.