0

Total Android beginner here...

I have an Activity with an OnClick Listener as in this example: Multiple Buttons `OnClickListener()` android

And now I'm setting up a listener on Floating Action Button in a different activity. What I'm not sure about is whether it is possible to use that same listener, or does each class have to have it's own?

ACTIVITY #1 // a regular activity
public class Requests extends AppCompatActivity implements View.OnClickListener {...}

ACTIVITY #2 // A RecyclerView, CardView type activity
public static class MyViewHolder extends RecyclerView.ViewHolder {...}

I'd really like to have one Listener to handle the cardview click events, as well as the Floating Action Button.

Maxcot
  • 1,513
  • 3
  • 23
  • 51
  • 1
    if it's only for buttons, you can specify which method it's supposed to execute in the xml: `andorid:onClick=methodName` – Century Oct 11 '17 at 09:29
  • 1
    If those classes has the same functionality then yes you can implement the same OnClickListener otherwise you need to implement the different listener in order to do the different task – Lokesh Pandey Oct 11 '17 at 09:29
  • @Century I don't think the OP is asking this. If you would have read the question he is asking to implement one listener to handle the different click events – Lokesh Pandey Oct 11 '17 at 09:31
  • Or else you can do one thing you can write a single class to implement listener and based on the button id you can create another activity. – Lokesh Pandey Oct 11 '17 at 09:34

3 Answers3

2

in general you can use the same listener for multiple buttons. if they are in different activities , you can write a seperate class that implements View.OnClickListener . Like this:

public class MyButtonListener implements View.OnClickListener {

@Override
public void onClick(View v) {
  switch (v.getId()) {

    case R.id.oneButton:
        // do your code
        break;

    case R.id.twoButton:
        // do your code
        break;

    case R.id.threeButton:
        // do your code
        break;// default method for handling onClick Events..
}

}

Then you just have to set your button listener like button.setOnClicklistener(new MyOnClickListener());

If your buttons are all doing the same action, you wont need the switch-case block.

Creating a seperate listener class is not a bad idea at all BUT: you should try to implement one for each activty to keep an overview over your button actions.

Pynnie
  • 136
  • 12
  • please mark this as the answer if it solved your problem :) – Pynnie Oct 11 '17 at 13:40
  • This approach makes sense to me. But could you please explain something else... what if the one click is from a "card" in a cardview and the other click is from a menu option, and the third from a button in a view. I'm not sure that all three event sources would be accessible through the `public void onClick(View v)`. Unless I'm not understanding what the `View v` reference means....? – Maxcot Oct 15 '17 at 08:24
  • @Maxcot all those UI elements you mentioned extend the `View` class. Will have specified them in an XML file, that belongs to an activity or works as a ListView item. However, in the XML you have to declare a unique ID for every element, you want to behave in a seperate way. All these IDs are accessable throu the `R` class. So it is possible to access them in a onClickListener class. Hope you could follow my explanation – Pynnie Oct 15 '17 at 19:01
  • "... all those UI elements you mentioned extend the View class..." Ah, well, that explains that nicely. Thanks. – Maxcot Oct 16 '17 at 00:53
0

You can use below code snipet mentioned in the link below

Handle click item in Recycleview

In that onClick and onItemClick Override listener you can implement both Floating Action Button and cardview click events position wise

Kaushik
  • 6,150
  • 5
  • 39
  • 54
yash786
  • 1,151
  • 8
  • 18
0

To perform same operation on click event of different view or viewGroup implement an anonymous class for OnClickListener as

View.OnClickListener mOnClickListener= new View.OnClickListener() {  
 @Override public void onClick(View v) {    /*do your code */ }};   

or call it in your activity as

 mbutton.setOnClickListener(mOnClickListener);   
    mcardView.setOnClickListener(mOnClickListener);
Seema
  • 1
  • 3