Adam Matan Adam Matan - 5 months ago 21
Android Question

Set ToggleButton to play default sound when clicked?

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?

Answer

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.

Comments