Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Switch Statement to Handle Button Clicks

I'm trying to wrap my head around Views, Listeners etc. I have an Activity with 2 Buttons: buttonplay and buttonstop. My problem is I can't wrap my head around the Views and Listeners completely enough to generate a working switch statement.

For example, I would LIKE to create a SINGLE Listener and somehow use it to determine which button is clicked. Then somehow use the ID of the button clicked in my switch statement, But everything I find online seems to use SEPARATE listeners for every button and then somehow use the View as the argument to the Switch statement.

I realize the code below is not correct, but am looking for what changes I would need to accomplish the above.

I want to control the MediaPlayer depending on which button is clicked. I have:

   Button b1 = (Button) findViewById(R.id.buttonplay);       
    b1.setOnClickListener(new View.OnClickListener()         
    {

        public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {
          case R.id.buttonplay:
          //Play voicefile
          MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
          break;
          case R.id.buttonstop:
          //Stop MediaPlayer
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
          break;
         }
   }
    });

Ultimately I would like the most straighforward way to switch on whatever button is clicked. I believe a big part of my confusion stems from the way onClickListeners and Views are used in this context.

like image 354
user615525 Avatar asked Feb 13 '11 23:02

user615525


People also ask

Which are the ways to handle onClick event of button?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

When to use Switch statement?

The switch statement is used to test the equality of a variable against several values specified in the test cases. Java is one of the most widely used programming languages used today.


2 Answers

One way of achieving this is to make your class implement OnClickListener and then add it to your buttons like this:

Example:

//make your class implement OnClickListener
public class MyClass implements OnClickListener{

        ...

        //Create your buttons and set their onClickListener to "this"
        Button b1 = (Button) findViewById(R.id.buttonplay);   
        b1.setOnClickListener(this);

        Button b2 = (Button) findViewById(R.id.buttonstop);   
        b2.setOnClickListener(this);

        ...

        //implement the onClick method here
        public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {
            case R.id.buttonplay:
              //Play voicefile
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
              break;
            case R.id.buttonstop:
              //Stop MediaPlayer
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
              break;
          }
       }
}

For more information see Android Developers > Handling UI Events.

like image 65
dogbane Avatar answered Sep 29 '22 11:09

dogbane


Just change the class (I suppose it's the main Activity) to implement View.OnClickListener and on the onClick method put the general onClick actions you want to put:

public class MediaPlayer extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    Button b1 = (Button) findViewById(R.id.buttonplay);       
    b1.setOnClickListener(this);
    //{YOUR APP}
}


@Override
public void onClick(View v) {
    // Perform action on click
    switch(v.getId()) {
    case R.id.buttonplay:
        //Play voicefile
        MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
        break;
    case R.id.buttonstop:
        //Stop MediaPlayer
        MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
        break;
    }

}}

I took it from this video: http://www.youtube.com/watch?v=rm-hNlTD1H0 . It's good for starters.

like image 25
user614538 Avatar answered Sep 29 '22 10:09

user614538