0

I'm doing the Udacity's android beginner course (even thou I'm not beginner to programming) and decided to fool around a little.

I've managed to make a button either change the value on screen (by adding +1 value) or to play a sound, but when I mix both, the button only plays the sound, but does not add a number / update the value on screen, anyone knows why?

I have three methods; 1 for calling the mediaPlayer:

   // This method calls mediaPlayer
public void mediaPlayer (String sound, String id){
    Uri uriPlayer = Uri.parse("android.resource://" + getPackageName() + "/raw/" + sound );
    final MediaPlayer mp = MediaPlayer.create(this, uriPlayer);

    int playIdInt = getResources().getIdentifier(id, "id", getPackageName());
    Button play_button = (Button)this.findViewById(playIdInt);
    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mp.start();
        }
    });
}
  1. For displaying the value on screen

    /**
    * This method displays the given quantity value on the screen.
    */
     private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
    
  2. For adding +1 value to the variable, calling the display method and the media player method:

    public void increment (View view){
    quantity = quantity + 1;
    display(quantity);
    mediaPlayer("mariocoin", "plus_button");
}

EDIT: not sure if needed, but here is XML of the button:

        <Button
        android:id="@+id/plus_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:onClick="increment"
        android:text="+" />

The thing is that when I play the button the first time, it adds 1 and display on the screen, but no sound played. The second and subsequent times it only plays the sound. It does not add +1 to the variable nor change the value on screen. Why and how can I fix this?

mrbTT
  • 1,399
  • 1
  • 18
  • 31
  • where do you start the quantity variable? – Tiago Oliveira Sep 14 '16 at 19:27
  • @Richard = I'm not sure I understood what you meant. The XML code for the button calls the 1.method: increment that calls both 2.method: display and 3.method: mediaPlayer – mrbTT Sep 14 '16 at 19:27
  • the quantity variable is global with initial value of 0 right? – Tiago Oliveira Sep 14 '16 at 19:30
  • @Tiago: all those methods are inside public class MainActivity extends AppCompatActivity { }. | The quantity is the first thing declared as global variable (int quantity = 0;) after the first method: ---@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } – mrbTT Sep 14 '16 at 19:30
  • playIdInt is = to plus_button? – Tiago Oliveira Sep 14 '16 at 19:34
  • Hey Tiago, thanks for the attention, Varad bellow had the solution! – mrbTT Sep 14 '16 at 19:39
  • @mrbTT I think your XML edit helped, and Varad explained what I was trying to get my head around. I just saw your first click listener that didn't do anything other than play the sound. – Richard Le Mesurier Sep 15 '16 at 05:05

1 Answers1

1

It seems that you have define two ClickListeners for same button. The first one calls increment method and the second one is defined inside media player method. That listener is not required.

//Somewhere in oncreate .. Not required if xml has onclick specified
Button play_button = (Button)this.findViewById(playIdInt);
play_button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       increment();
    }
});


public void increment (View view){
       quantity = quantity + 1;
       display(quantity);
       mediaPlayer("mariocoin", "plus_button");
}

// This method calls mediaPlayer
public void mediaPlayer (String sound, String id){
            Uri uriPlayer = Uri.parse("android.resource://" + getPackageName() + "/raw/" + sound );
            final MediaPlayer mp = MediaPlayer.create(this, uriPlayer);
            mp.start();
        }
Varad Chemburkar
  • 458
  • 2
  • 6
  • 15
  • This is exactly it, thank you very much!! Just another question: So it's a best practice to create the play_button inside the onCreate? Also, I didn't add your line of "public void onClick(View v)" inside the onCreate method, is that necessary? – mrbTT Sep 14 '16 at 19:39
  • 1
    Read this http://stackoverflow.com/questions/21319996/android-onclick-in-xml-vs-onclicklistener – Varad Chemburkar Sep 14 '16 at 20:52