Skip to content

Commit 0d38fd8

Browse files
akrigermacdoum1
authored andcommitted
Adds Android click sound to Touchables
Summary: Android apps play a touch sound on press, as long as you have "Touch sounds" enabled in the settings. As and Android user, when building my app using React Native, one of the first things I noticed was that there were not any touch sounds. This is missing from React Native and there have been multiple PRs to have this implemented, but no success. This PR iterates over [facebook#6825](facebook#6825) and [facebook#11136](facebook#11136) This PR keeps it simple by only implementing the enhancement for Android, as iOS apps typically do not use touch sounds, and follows the users' system settings for whether or not the sound is played. I have manually tested this on multiple devices and emulators with zero problems [ANDROID] [ENHANCEMENT] [UIManagerModule.java]- Adds Android click sound to touchables [ANDROID] [ENHANCEMENT] [Touchable] - Adds Android click sound to touchables Closes facebook#17183 Differential Revision: D7560327 Pulled By: hramos fbshipit-source-id: ce1094c437541bc677c7d64b0dba343dd9574422
1 parent f61f347 commit 0d38fd8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Libraries/Components/Touchable/Touchable.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,21 @@ const TouchableMixin = {
741741
this._startHighlight(e);
742742
this._endHighlight(e);
743743
}
744+
if (Platform.OS === 'android') {
745+
this._playTouchSound();
746+
}
744747
this.touchableHandlePress(e);
745748
}
746749
}
747750

748751
this.touchableDelayTimeout && clearTimeout(this.touchableDelayTimeout);
749752
this.touchableDelayTimeout = null;
750753
},
751-
754+
755+
_playTouchSound: function() {
756+
UIManager.playTouchSound();
757+
},
758+
752759
_startHighlight: function(e) {
753760
this._savePressInLocation(e);
754761
this.touchableHandleActivePressIn && this.touchableHandleActivePressIn(e);

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import android.content.ComponentCallbacks2;
1414
import android.content.res.Configuration;
15+
import android.content.Context;
16+
import android.media.AudioManager;
1517
import com.facebook.common.logging.FLog;
1618
import com.facebook.debug.holder.PrinterHolder;
1719
import com.facebook.debug.tags.ReactDebugOverlayTags;
@@ -584,6 +586,14 @@ public void clearJSResponder() {
584586
public void dispatchViewManagerCommand(int reactTag, int commandId, ReadableArray commandArgs) {
585587
mUIImplementation.dispatchViewManagerCommand(reactTag, commandId, commandArgs);
586588
}
589+
590+
@ReactMethod
591+
public void playTouchSound() {
592+
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
593+
if (audioManager != null) {
594+
audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
595+
}
596+
}
587597

588598
/**
589599
* Show a PopupMenu.

0 commit comments

Comments
 (0)