enable/disable keyboard sound and vibration programmatically

15,618

Solution 1

As per your comments:

i'm talking about the default keyboard in android , i want to have the ability to disable / enable keyboard sound and vibration when user tap a key in keyboard,( like in Settings of Keyboard )

&

i'm talking about soft keyboard, like in SAMSUNG GALAXY S2 , HTC ONE ...etc

AFAIK, you can not accomplish this as each input method keeps its sound/vibration preference values internally. See for example Android (AOSP) IMe (as of this writing lines 30~39):

    <CheckBoxPreference
        android:key="vibrate_on"
        android:title="@string/vibrate_on_keypress"
        android:defaultValue="@bool/config_default_vibration_enabled"
        android:persistent="true" />
    <CheckBoxPreference
        android:key="sound_on"
        android:title="@string/sound_on_keypress"
        android:defaultValue="@bool/config_default_sound_enabled"
        android:persistent="true" />

As you can see it stores the vibrate/sound values in its shared preference. That applies to most of IMe in the market. Hence, you can not control vibrate/sound effect for all IMe's from single point.

Solution 2

Have a look at How to disable default sound effects for all my application or activity for disabling tap-sounds.

To disable Haptic feedback and touch sounds programmatically have a look at http://developer.android.com/reference/android/view/View.html#setHapticFeedbackEnabled(boolean) http://developer.android.com/reference/android/view/View.html#setSoundEffectsEnabled(boolean)

Easier done is by defining the following in your styles.xml

<!-- Application theme. -->
<style name="AppTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:soundEffectsEnabled">false</item>
    <item name="android:hapticFeedbackEnabled">false</item>
</style>

and in your manifest.xml

<application [...] android:theme="@style/AppTheme" >
Share:
15,618

Related videos on Youtube

Houcine
Author by

Houcine

I'm a Mobile Software Engineer Android / IOS. feel free to contact me via my blog if you have any suggestions, questions or issues. Follow us on Facebook

Updated on June 04, 2022

Comments

  • Houcine
    Houcine almost 2 years

    I'm trying to find a way to disable and enable keyboard sound and vibration when tapping keys. I've searched on Stack Overflow and other Android Forums but i didn't found any result.

    I've tried AudioManager to enable vibrate mode, but i want to activate the vibrate mode and sound on keyboard.

    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 
                    AudioManager.VIBRATE_SETTING_ON);
    

    Is there any way how to change android.provider.Settings for keyboard sound and vibration ?

    • Houcine
      Houcine
      no it doesn't help @Ali , i've already tried that , i want the volume of Ring and Media to be NORMAL ,and only disable or enable keyboard tap sound and vibration.
    • Ali
      Ali
      Would putting the phone in silent mode help? This largely depends on the type of app you have. But you could just do: AudioManager audio_mngr = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); then audio_mngr .setRingerMode(AudioManager.RINGER_MODE_SILENT);.
    • Houcine
      Houcine
      i'm talking about the default keyboard in android , i want to have the ability to disable / enable keyboard sound and vibration when user tap a key in keyboard,( like in Settings of Keyboard ) ,
  • Houcine
    Houcine almost 11 years
    this is not for keyboard keys ,
  • Alexander Pacha
    Alexander Pacha almost 11 years
    Could you explain "keyboard keys"? Do you mean the soft-keyboard (which is displayed on the screen), or the hard-keyboard (e.g. attachted to an ASUS Transformer-Pad). So you want to turn off the haptic feedback and sounds, when the user uses the keyboard?
  • Houcine
    Houcine almost 11 years
    i'm talking about soft keyboard, like in SAMSUNG GALAXY S2 , HTC ONE ...etc
  • Alexander Pacha
    Alexander Pacha almost 11 years
    Then you might need a dirty hack (only available for rooted devices), as described in stackoverflow.com/questions/7392067/….
  • Upendra Shah
    Upendra Shah over 6 years
    Can you explain this? I can't understand where and how to implement this. @ozbek
  • ozbek
    ozbek over 6 years
    @UpendraShah, are you developing an IME?
  • Upendra Shah
    Upendra Shah over 6 years
    Actually I want to handle vibrate_on_keypress and sound_on_keypress from my application, so I want to enable or disable these pragmatically.
  • ozbek
    ozbek over 6 years
    @UpendraShah: as mentioned in the answer, that is not possible at this moment.
  • Upendra Shah
    Upendra Shah over 6 years
    Is is possible if I get root access to device?? @ozbek
  • ozbek
    ozbek over 6 years
    @UpendraShah: even with the root access, that's not what you want. You will have disassemble the IME apk(s), locate where they are setting those values, change them to your liking, repacking the IME apk(s), sign the package(s) appropriately, push the apk(s) to the target device.
  • Upendra Shah
    Upendra Shah over 6 years
    I have done these with root access. You can check that in Answer below @ozbek