Martin Martin - 13 days ago 5
Android Question

Toggle only works the first time

I am trying to use the Toggle Button to play a sound when the button is "on" and stop the sound when the button is "off".

This seems to only work when I first toggle between the on/off button.

ToggleButton toggle = (ToggleButton) findViewById(;

toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled


Because you called method stop(). There is the description of the method:

Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state. Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again. Calling stop() has no effect on a MediaPlayer object that is already in the Stopped state.

How to play exactly right,you just need to visit this website of google. click me.