Set ToggleButton to play default sound when clicked?
Asked Answered
A

2

6

My android apps has a regular button:

<Button
  android:id="@+id/allOnButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginRight="130dp"
  android:text="@string/sensor_management_all_on" />

And a toggle button:

<ToggleButton
  android:id="@+id/accelerometerToggleButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:checked="true"
  android:textOff="off"
  android:textOn="on" />

When the button is pressed, a nice "click" sound is played, which gives a good feedback to the user that the button was actually pressed.

How do I set a click sound for the ToggleButton?

Acceptation answered 16/9, 2012 at 7:55 Comment(0)
M
3

For a click sound to be heard, you need clickListener to be set.

So set a dummy onClickListener onto the ToggleButton, it should play the sound effect.

Mononuclear answered 16/9, 2012 at 8:57 Comment(1)
WTF? Well it works but how did you figure that out? Thanks for the tip.Motor
F
0
    toggleButton = (ToggleButton)findViewById(R.id.sound);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.theme);
    if(toggleButton.isChecked())
        mp.start();
    toggleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!toggleButton.isChecked()){
                mp.pause();
            }
            else {
                mp.start();
                mp.isLooping();
            }
        }
     });
Fruma answered 28/6, 2016 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.