-3

i just started developing in android a couple of weeks ago. i am working a simple app that has 2 activities but one activity has 15 buttons. right now i am using

button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {/* Some Code */ });

for each button inside my onCreate. this approach seems excessive to me and maybe not the best way.

I know about the 5 different forms of using onClick and implementing onClickListeners but I am asking which way would be the proper way of doing it. Best practices.

Thank You.

  • 1
    what do those 15 buttons do? it seems like a lot of buttons in a single view. – Carlo Moretti Jun 29 '15 at 13:57
  • Onheiron, i have 6 buttons that subtract a different value each. for example. i ahve strikes, balls fouls, out and score. so i added a clickListener for each subtract button. maybe i could have one clicklistener and just compare the views to see which button was pressed and then have the logic there? – Manuel de Oca Jun 29 '15 at 14:02

3 Answers3

4

In that case, I might add implements View.OnClickListener to the activity class, and then make a single onClick that looks like:

@Override
public void onClick(View v){
  switch(v.getId()){
  case R.id.button1:
    // handle button1 click
    break;
  case R.id.button2:
    // handle button3 click
    break;
  case R.id.button3:
    // handle button3 click
    break;
  // etc...
  }
}

This only really makes sense if the actions of all those buttons are somehow related, and if they are then it can significantly reduce code duplication. You would still have to do view.setOnClickListener(this) for each one, too.

Alternatively, if you want you can remove the implements View.OnClickListener and the @Override and simply register your method in the XML, like Kevin said. That would allow you to have multiple of these "grouped" click listeners.

nkorth
  • 1,684
  • 1
  • 12
  • 28
1

I think that the best way to do it when you have so much buttons is to implement the View.OnClickListener to your activity and then to the following:

  1. For every single button just one line:

    button.setOnClickListener(this);

  2. override the onClick() method and check the id of the selected button and do something with the currently selected button like this for example:

    // onClick is called when a view has been clicked.
    @Override
    // Parameter v stands for the view that was clicked.  
    public void onClick(View v) { 
    
        // getId() returns the id of the clicked view.
        if(v.getId() == R.id.button1){
            //you clicked the first button
        }else if(v.getId() == R.id.button2){
            //you clicked the second button
        } else if ... // and so on for all of your buttons
    }
    
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

You can use the xml attribute onClick:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="myFancyMethod" />
<!-- even more layout elements -->

More Information

Community
  • 1
  • 1
Kevin Brey
  • 1,233
  • 1
  • 12
  • 18