I have a MainActivity class inside which I change various fragments, each of which contains a number of buttons. There is an OnClickListener on these buttons, and I need a check to be performed when the button is clicked, and only if successful will the OnClickListener be triggered. I need to somehow be able to get which element we clicked on when clicking on the screen, how do I do this? I haven't found any information anywhere on the Internet, everyone suggests storing a list of all views or other non-working options, but this doesn't suit me.
I have a MainActivity class inside which I change various fragments, each of which contains a number of buttons. There is an OnClickListener on these buttons, and I need a check to be performed when the button is clicked, and only if successful will the OnClickListener be triggered. I need to somehow be able to get which element we clicked on when clicking on the screen, how do I do this? I haven't found any information anywhere on the Internet, everyone suggests storing a list of all views or other non-working options, but this doesn't suit me.
Share Improve this question asked Mar 23 at 20:12 TEILabsTEILabs 33 bronze badges 2- 3 This seems a backwards way of doing things. If an element isn't clickable, it shouldn't appear clickable to the user. That's just confusing. Instead you should disable it so it appears inactive. Which would also prevent the onClickListener from firing. – Gabe Sechan Commented Mar 23 at 20:33
- @GabeSechan The idea is that when the user clicks, they see that this feature is on subscription and they are offered to buy it, and unfortunately these are the customer's requirements. – TEILabs Commented Mar 24 at 15:07
1 Answer
Reset to default 0Ok, based on your comment I get why you're trying to do this. I would do something like this:
interface InnerClickListener {
boolean subscriptionAllowsAction();
void onClick();
}
class SubscriptionCheckClickListener: View.OnClickListener {
private InnerClickListener listener;
public SubscriptionCheckClickListener(InnerClickListener innerClick) {
listener = innerClick()
}
public void onClick(View view) {
if(listener.subscriptionAllowsAction()) {
listener.onClick();
}
else {
displayErrorScreen();
}
}
}
Then have all your listeners implement SubscriptionCheckClickListener instead. That will make all of them capable of checking the subscription before acting.