2

Let me clarify. I am creating a soundboard (android application) for a class of mine and I used this method 30 times, one for each button/mp3

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class pageOneSounds extends Activity {
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    setContentView(R.layout.pageonesounds);

final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.sound1);

Button playSound1 = (Button) this.findViewById(R.id.sound1Button);

playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


    pg1.start();

    }
});

So this method works well, but I am going to have 30 buttons with a scroll view on this layout. I was wondering if there is a method I could use that would allow me to have just one method and not have to copy and paste that .setOnClickListener 30 times. Let me know if I am making sense or not. Really new to java and android I don't really know what I'm doing/might not understand you.

frosty
  • 307
  • 4
  • 15

5 Answers5

2

In your xml file do this for each button:

<Button android:id="@+id/sound1Button"
  android:onClick="buttonClick"
  ... />
<Button android:id="@+id/sound2Button"
  android:onClick="buttonClick"
  ... />
<Button android:id="@+id/sound3Button"
  android:onClick="buttonClick"
  ... />
etc

And then in your pageOneSounds activity, do this:

public void buttonClick(View v) {
  switch(v.getId()) {
    case R.id.sound1Button:
    // do what u want here for button 1
    break;
    case R.id.sound2Button:
    // do what u want here for button 2
    break;
    case R.id.sound3Button:
    // do what u want here for button 3
    break;
    // etc
  }
}
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • Would you mind expanding on what exactly is going on here? I understand that you are telling each button to use the same method (buttonClick), but what is the method doing? What does case mean? what about break? If there are very broad questions, don't feel obligated to explain them to me. Thanks again. – frosty Dec 23 '13 at 04:45
  • @frosty I think you should look at Java programming first. Or even C if you have no programming background. switch, case, breaks are all keywords in Java (and other languages). – VJ Vélan Solutions Dec 23 '13 at 04:47
  • Alright, thanks. Some python and html background, but that's it. I just recently jumped into android apps so I am making a soundboard and learning on the way. Thanks. @harikris – frosty Dec 23 '13 at 04:53
  • @frosty Happy learning. The best resource for Android is the developer.android.com and for Java, i use Head First series. – VJ Vélan Solutions Dec 23 '13 at 05:01
  • thanks. One more thing; is the method you gave me more 'clean' or better than what I was doing before (posting the same method 30 times in the .java)? – frosty Dec 23 '13 at 05:15
  • @frosty yeah. from maintenance perspective, my suggestion is better. Mentioning onClick method in xml helps big time to in this case. Would you mind accepting my answer if you feel it's the right answer? Click on the Tick button to the left of my answer to accept it. – VJ Vélan Solutions Dec 23 '13 at 05:26
  • @karikris yeah I was going to. I cant get the code to work; does your code go inside the onCreate method or after it separately? – frosty Dec 23 '13 at 05:30
  • No, this is a separate method. Outside onCreate() – VJ Vélan Solutions Dec 23 '13 at 05:33
1

What you can do is

make your class implements OnClickListener

@Override
public void onClick(View view) {
    switch(view.getId())
    {
    case R.id.button1:
    {
        break;
    }
    case R.id.button2:
    {
        finish();
    }
           // define your working of button 
    }

}

dont forget to set button1.setOnClickListener(this)

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

If I understand your question correctly, you want to save your OnClickListener to a variable so that you can simply pass it to all of your setOnClickListener() calls? This is possible by creating a variable for it like you would for any other type.

View.OnClickListener playSoundListener = new View.OnClickListener(){
    @Override
    public void onClick(View v){
        pg1.start();
    }
};

Then pass it to setOnClickListener() like so:

playSound1.setOnClickListener(playSoundListener);
Kyle
  • 374
  • 1
  • 11
0

Implement OnClickListener then use either switch case or write the mp3 play method of yours in onClick() (if you dont have any other function keys)

public class pageOneSounds extends Activity implements View.OnClickListener{
    final MediaPlayer pg1;
    Button playSound1;
    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.pageonesounds);
        pg1 = MediaPlayer.create(this, R.raw.sound1);
        playSound1 = (Button) this.findViewById(R.id.sound1Button);
        playSound1.setOnClickListener(this);
   }

    @Override
    public void onClick(View v) {
        pg1.start();
    }
}
Sar009
  • 2,166
  • 5
  • 29
  • 48
0

You can make a method which will return an Object of MediaPlayer`. And on button click you can start your playing your sound.

public static MediaPlayer (Context context,int Idofmediaplayer) {
    MediaPlayer pg1 = MediaPlayer.create(context, Idofmediaplayer);
    return pg1;
}

Use Switch case or if else loop in OnClickListener method

MediaPlayer m = MediaPlayer (pageOneSounds.this, R.raw.sound1);
m.start();