enable/disable keyboard sound and vibration programmatically
Asked Answered
C

3

9

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 ?

Copperplate answered 6/5, 2013 at 9:18 Comment(9)
I guess this really depends on the implementation of the Keyboard. There might be keyboards out there that play a movie each time a key is tapped. I do not think you can achieve this. Anyway, goodluck.Wenn
thanks @SherifelKhatib for your reply , but in settings we have the possiblity to enable/disable key tap sound , and key tap vibrator, my question is : is there a way to do this programmatically , as we can do with Wifi via WifiManager, and Brightness, Volume via AudioManager..etcCopperplate
Actually the options you're talking about are KEY PAD sound (for the dial keypad) and the touch sound (for each time the user touches the screen)Wenn
no , in keyboard settings , there are options for keyboard to enable vibration on key tap , and sound tooCopperplate
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);.Surculose
no it doesn't help @Surculose , 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.Copperplate
Are you talking about one specific keyboard? Or want to control arbitrary any keyboard installed on a user device?Chaeta
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 ) ,Copperplate
This sounds promising, but it didn't work for me. Maybe I'm missing somethingExtrados
C
4

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.

Chaeta answered 15/5, 2013 at 7:47 Comment(7)
Can you explain this? I can't understand where and how to implement this. @ChaetaIberia
@UpendraShah, are you developing an IME?Chaeta
Actually I want to handle vibrate_on_keypress and sound_on_keypress from my application, so I want to enable or disable these pragmatically.Iberia
@UpendraShah: as mentioned in the answer, that is not possible at this moment.Chaeta
Is is possible if I get root access to device?? @ChaetaIberia
@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.Chaeta
I have done these with root access. You can check that in Answer below @ChaetaIberia
E
11

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" >
Extrados answered 14/5, 2013 at 1:29 Comment(4)
this is not for keyboard keys ,Copperplate
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?Extrados
i'm talking about soft keyboard, like in SAMSUNG GALAXY S2 , HTC ONE ...etcCopperplate
Then you might need a dirty hack (only available for rooted devices), as described in #7392567.Extrados
C
4

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.

Chaeta answered 15/5, 2013 at 7:47 Comment(7)
Can you explain this? I can't understand where and how to implement this. @ChaetaIberia
@UpendraShah, are you developing an IME?Chaeta
Actually I want to handle vibrate_on_keypress and sound_on_keypress from my application, so I want to enable or disable these pragmatically.Iberia
@UpendraShah: as mentioned in the answer, that is not possible at this moment.Chaeta
Is is possible if I get root access to device?? @ChaetaIberia
@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.Chaeta
I have done these with root access. You can check that in Answer below @ChaetaIberia
I
0

Yes, You can do this if you have root access. Its a lengthy process but you can do this :

Step : 1 Create xml file named com.android.inputmethod.latin_preferences.xml and save in assets.

com.android.inputmethod.latin_preferences.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <boolean name="popup_on" value="false" />
    <string name="auto_correction_threshold">1</string>
    <boolean name="pref_enable_metrics_logging" value="true" />
    <boolean name="pref_voice_input_key" value="true" />
    <boolean name="pref_key_use_personalized_dicts" value="true" />
    <boolean name="pref_key_block_potentially_offensive" value="true" />
    <int name="last_shown_emoji_category_id" value="1" />
    <boolean name="sound_on" value="false" />
    <string name="emoji_recent_keys">[{&quot;Integer&quot;:128533}]</string>
    <boolean name="auto_cap" value="true" />
    <boolean name="show_suggestions" value="true" />
    <boolean name="pref_key_use_contacts_dict" value="true" />
    <boolean name="next_word_prediction" value="true" />
    <boolean name="pref_key_use_double_space_period" value="true" />
    <int name="emoji_category_last_typed_id1" value="0" />
    <boolean name="vibrate_on" value="false" />
</map>

Step 2 : Copy this file into your application folder (anywhere where you can access) using asset manager for that you need

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

this function will copy file from assets

public static void copyAssets(Context context, String assetPath, String outFilename) {
        AssetManager assetManager = context.getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(assetPath);
            File outFile = new File(context.getExternalFilesDir(null), outFilename);

            out = new FileOutputStream(outFile);
            copyFile(in, out);
        } catch (IOException e) {
            Log.e(TAG, "Failed to copy asset: " + outFilename, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

Step 3 : overwrite the system preference file system path (destPath) is /data/data/com.android.inputmethod.latin/shared_prefs

public static void copyToSystem(final String sourceFilePath, final String destPath) {
        Thread background = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream os = new DataOutputStream(process.getOutputStream());
//                    
                    os.writeBytes("cp -f " + sourceFilePath + " " + destPath + "\n");
                    os.flush();
                    os.writeBytes("exit\n");
                    os.flush();
                    process.waitFor();
                    process.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                }
            }
        });
        background.start();
    }

Step 4 : Reboot device

That's all done. These step will off key press sound and key press vibration

Iberia answered 2/11, 2017 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.