1

As I'm creating a library and I wanted to track the button clicks from the application using that library(kind of analytics clicks tracking library). So I have tried so many things and those didn't work for me. I have gone through the below link and I came to know that we can add multiple listeners using the composite pattern.

Attaching multiple listeners to views in android?

Please help me here to achieve my requirement.

Here is the code, which I have tried

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_demo);

    final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frameFragment, new FragmentA());
    fragmentTransaction.commit();

    Button button = findViewById(R.id.buttonDemo);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("test", "Button Click 1");
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("test", "Button Click 2");
            /*Field field = null;
            try {
                Log.e("test", "CLASS NAME: "+v.getClass().getName());
                field = Class.forName(v.getClass().getName()).getDeclaredField("mOnClickListener");
                View.OnClickListener retrievedListener = (View.OnClickListener) field.get(v);
                Log.e("test", ""+retrievedListener.toString());
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                Log.e("test", "Exception: NoSuchFieldException");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                Log.e("test", "Exception: ClassNotFoundException");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Log.e("test", "Exception: IllegalAccessException");
            }*/


            ClickListener listenerCollector = new ClickListener();
            listenerCollector.addOnclickListener(this);
        }
    });



}

}

another class

public class ClickListener implements View.OnClickListener {
private ArrayList<View.OnClickListener> clickListeners = new ArrayList<>();

public void addOnclickListener(View.OnClickListener listener){
    clickListeners.add(listener);
}
@Override
public void onClick(View v) {
    for(View.OnClickListener l : clickListeners){
        l.onClick(v);
        Log.e("test", "onclick from composite click");
    }
}

}

Suresh
  • 427
  • 1
  • 6
  • 22
  • If you can add, what you are trying to achieve little bit more, it will be easy to help. – Shaleen Apr 20 '20 at 11:58
  • @Suresh can you please post your code also? – keshav kowshik Apr 20 '20 at 12:07
  • does this link answer your question? https://stackoverflow.com/questions/5465204/how-can-i-set-up-multiple-listeners-for-one-event – keshav kowshik Apr 20 '20 at 12:09
  • @Keshav1234, I have gone through the above link, It is not clear for me. – Suresh Apr 20 '20 at 12:27
  • @Suresh ok please post code of what you have tried and understood from other resources. – keshav kowshik Apr 20 '20 at 12:35
  • @Keshav1234, I have updated my question with code, which I'm trying – Suresh Apr 20 '20 at 12:57
  • @Suresh what is not working with this code? can you please explain that? – keshav kowshik Apr 20 '20 at 13:39
  • Here is my requirement, I need to get onclickListener in another class ("ClickListener") also. When I click the button from MainActivity it should print below log Log.e("test", "Button Click 2"); as well as need to print Log.e("test", "onclick from composite click"); from another class also. Something wrong I did here. – Suresh Apr 20 '20 at 13:43

1 Answers1

0

Try this way:

Add the following in onCreate of MainActivity and onCreate of another class:

Make Clicklistener class a singleton so that it contains single object at any time.

ClickListener listenerCollector = new ClickListener.getInstance();
listenerCollector.addOnclickListener(this);

In the onClick of your button in MainActivity add the following:

for (ClickListener listener: listenerCollector.clickListeners){
      listener.onClick();
}

Remove for loop in ClickListener OnClick method.

Hope this helps.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45