0 votes
ago in Education by (1.7m points)
I want to change the images in image view on Onclick function. I have already tried this:

bt_audiocapture.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            if (checkPermission()) {

                if (bt_audiocapture.getResources().getDrawable(R.drawable.ic_mic).equals(R.drawable.ic_mic)) {

                    start();

                    bt_audiocapture.setImageResource(R.drawable.ic_stop);

                } else if (bt_audiocapture.getResources().getDrawable(R.drawable.ic_stop).equals(R.drawable.ic_stop)) {

                    stop();

                    bt_audiocapture.setImageResource(R.drawable.ic_play);

                } else if (bt_audiocapture.getResources().getDrawable(R.drawable.ic_play).equals(R.drawable.ic_play)) {

                    play();

                }

            }

        }

    });

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
ago by (1.7m points)
bt_audiocapture.getResources().getDrawable(R.drawable.ic_mic) returns a Drawable object. You can't compare it to R.drawable.ic_mic, which is integer. That is something like comparing a car to green color.

To accomplish your task, make some field like private int state = 0;, and some constants like

private final STATE_PLAYING = 1;

private final STATE_STOPPED = 2;

private final STATE_NONE = 0;

and then:

if (state == STATE_NONE) {

    start();

    bt_audiocapture.setImageResource(R.drawable.ic_stop);

    state = STATE_PLAYING;

} else if (state == STATE_PLAYING) {

    stop();

    bt_audiocapture.setImageResource(R.drawable.ic_play);

    state = STATE_STOPPED;

} else if (state == STOPPED) {

    play();

    state = STATE_PLAYING;

}
...