javaandroidviewonclicklistenerontouchlistener

How to track all clicks on items in an android activity


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.


Solution

  • Ok, 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.