title: Using Switch Statement to Handle Button Clicks
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I realize the code below is not correct, but am looking for what changes I would need to accomplish the above.</p>
  
  <p>I want to control the MediaPlayer depending on which button is clicked.  I have:</p>
  
  <pre><code>   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;
           }
     }
      });
  </code></pre>
  
  <p>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.</p>