Bun Béo
Bun Béo

Reputation: 41

How to Handle Multiple Click Events In Android?

I already watched so many tutorials about how to press a button and play a sound for that button with an event. My question here is: If I want to create a list of tones(Example Do,Re,Mi,Fa,...) and a list of musical instruments. Then I have to create these number of events, events = tone*instr. That is too much for a small app. So how can I reduce the number of events?

Upvotes: 2

Views: 1045

Answers (1)

JLT
JLT

Reputation: 3182

You can implement onClickListener in your Activity. Set onClickListener for each button. Then just detect the corresponding button pressed.

public class YourActivity extends Activity implements OnClickListener
    {
     Button button1, button2;

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

          button1 = (Button)findViewById(R.id.button1);
          button2 = (Button)findViewById(R.id.button2);

          button1.setOnClickListener(this);
          button2.setOnClickListener(this);
     }

     @Override
     public void onClick(View v)
     {
          if(v == button1)
          {
             //play button1 sound
          }

          else if(v == button2)
          {
             //play button2 sound
          }

          else 
          {
             ......
          }
     }
}

Hope it helps.

Upvotes: 4

Related Questions