37

In my application I am using sound pool for the button click audio effect. The problem is that if in the device's settings "Audible selection" is ticked, then my buttons will produce two sounds: the system one and my one at the same time.

It seems that if in each button properties I set "Sound Effects Enabled" to false, the system sound is not heard any more. But I have many buttons across a dozen of activities, plus I am adding a matrix of buttons in code, so it is rather inconvenient to set "Sound Effects Enabled" to false manually for each one of them. Not sure how I do this in code..

Is there a more global way to stop "Audible selection" in my application or at least for the one activity?

Lumis
  • 21,517
  • 8
  • 63
  • 67

4 Answers4

57

Create a theme file "res/values/styles.xml"

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="AppBaseTheme" parent="android:Theme.Black.NoTitleBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:soundEffectsEnabled">false</item>
</style>

</resources>

And then reference to it in your "AndroidManifest.xml"

<application
    ...
    android:theme="@style/AppTheme" >
David Douglas
  • 10,377
  • 2
  • 55
  • 53
30

I just had the same problem for my application. I was able to turn off sound feedback globally by putting android:soundEffectsEnabled=false in a theme.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
JessicaM
  • 386
  • 2
  • 3
  • Perhaps that is the answer I was looking for. This would work for only one activity, right? I would have then to assign the theme to every activity... Though I don't think every device behaves the same way, I have experienced that some devices turne off the sound globally on the first instance of a button whith android:soundEffectsEnabled=false but not all. – Lumis Mar 31 '11 at 21:17
  • 1
    You can apply the theme to the entire application from the manifest file. – JessicaM Apr 07 '11 at 20:17
11

You could create your own Button class and use that in the XML layout files...

package com.mycompany.myApp

public class MyButton extends Button {

    public MyButton (Context context, AttributeSet attrs) {
        this.setSoundEffectsEnabled(false);
    }
}

Then in the XML layout files use...

<com.mycompany.myApp.MyButton
    ...
</com.mycompany.myApp.MyButton>

...for your Buttons.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1
    Thank you for a good example. That is a lot of changes I have 17 layouts... wish if it was an easier way. – Lumis Feb 16 '11 at 23:21
  • then why not put 'android:soundEffectsEnabled="false"' in xml view instead creating custom button – Rahul_Pawar May 26 '16 at 10:52
1

You could create a custom view that extends from Button. Then just set the Sound Effect Enabled to false on its creation.

http://developer.android.com/guide/topics/ui/custom-components.html

You can also go further and make the view know which new custom sound should be played.

ddcruver
  • 911
  • 1
  • 7
  • 12
  • My application is almost finished, I would have to change all my layouts and code for each of a dozen of activities to do this. – Lumis Feb 16 '11 at 23:13
  • 1
    You can't just do a simple string replace in all your layout XML files to switch to the custom button?! – EboMike Feb 16 '11 at 23:16