W
W
Woozy222018-09-30 21:52:51
Android
Woozy22, 2018-09-30 21:52:51

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();
    }

}

Ps I ask for more details, because the teapot (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2018-10-01
@Woozy22

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);
    }

S
sunblossom, 2018-09-30
@sunblossom

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 question

Ask a Question

731 491 924 answers to any question