xago xago - 6 months ago 42
Java Question

How can I use a button to change and image AND play a sound?

I am new here and I have been looking for a way to have my app play a sound AND change the image in the background when I click a button.
The app is very simple it has a relative layout with an image and a button.
I have made it to play the sound but it doesn't change the image on the screen.

I found how to change the image on the screen and how to play the sound but I can't get it to do both on the same button click.

here's is my code for layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="us.xago.chtmpier.MainActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pier"
android:scaleType="centerCrop"/>
<ImageView
android:id="@+id/alfredo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/alfredo"
android:scaleType="centerCrop"/>

<Button
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHTM PIER"/>
</RelativeLayout>


and the java

import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
ImageView alfredo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alfredo = (ImageView) findViewById(R.id.alfredo);
//set invisible
alfredo.setVisibility(View.INVISIBLE);
alfredo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
alfredo.setImageResource(R.drawable.alfredo);
// set visible
alfredo.setVisibility(View.VISIBLE);
}
});

Button one = (Button) this.findViewById(R.id.button1);

alfredo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
// set visible
alfredo.setVisibility(View.VISIBLE);

}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
one.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mp.start();
}
});
}
}

Answer

The simplest way is to reuse your listener, so you have to store it in a variable. After that you can reuse your listener on your button listener.

import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView alfredo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alfredo = (ImageView) findViewById(R.id.alfredo);
        //set invisible
        alfredo.setVisibility(View.INVISIBLE);

        final OnClickListener changeImage = new OnClickListener() {
            public void onClick(View v) {
                //show image on the center of screen
                //set image
                alfredo.setImageResource(R.drawable.alfredo);
                // set visible
                alfredo.setVisibility(View.VISIBLE);
            }
        }

        alfredo.setOnClickListener(changeImage);

        Button one = (Button) this.findViewById(R.id.button1);

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
        one.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mp.start();
                changeImage.onClick(v);
            }
        });
    }
}