Answer the question
In order to leave comments, you need to log in
How to do two actions on one button?
There is a simple application that launches an audio track at the touch of a button. Is it possible to make it so that when the same button is pressed, the image also opens? (the sound should also start playing).
I am attaching the code:
package com.example.user.myapplication;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Calc extends AppCompatActivity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
}
public void play (View v) {
if (player == null) {
player = MediaPlayer.create (this, R.raw.song);
}
player.start();
}
}
Answer the question
In order to leave comments, you need to log in
Create a method for displaying an image and add its launch to the method void play(View v);
public void play (View v) {
if (player == null) {
player = MediaPlayer.create (this, R.raw.song);
}
player.start();
showImage(R.drawable.id_image);
}
public void showImage(int idImage){
findViewById(R.id.imageView).setImageResource(idImage);
}
Attach a method to the button click event that will call the methods for both starting the sound and showing the picture.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question