diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 6c76d3a2197f..54e1eec5b853 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6284,6 +6284,7 @@ public class com/facebook/react/views/textinput/ReactEditText : androidx/appcomp public fun setInputType (I)V public final fun setLetterSpacingPt (F)V public fun setLineHeight (I)V + public fun setLongClickable (Z)V public final fun setMaxFontSizeMultiplier (F)V protected final fun setNativeEventCount (I)V public final fun setOnKeyPress (Z)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index 754f10026624..921d1e6c5bbd 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -1126,6 +1126,14 @@ public open class ReactEditText public constructor(context: Context) : AppCompat public fun setContextMenuHidden(contextMenuHidden: Boolean) { this.contextMenuHidden = contextMenuHidden + isLongClickable = !contextMenuHidden + } + + // A long press selects text for the menu that contextMenuHidden hides, so the field takes none + // while the menu is hidden. TextView turns long-clicking back on whenever setInputType sets a key + // listener and when onAttachedToWindow makes the text selectable, so the gate is in the setter. + override fun setLongClickable(longClickable: Boolean) { + super.setLongClickable(longClickable && !contextMenuHidden) } protected fun applyTextAttributes() { diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt index 82ec014eb89e..0a3997cf3cad 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputPropertyTest.kt @@ -242,6 +242,28 @@ class ReactTextInputPropertyTest { assertThat(view.isEnabled).isTrue } + @Test + fun testContextMenuHidden() { + manager.updateProperties(view, buildStyles()) + assertThat(view.isLongClickable).isTrue + + manager.updateProperties(view, buildStyles("contextMenuHidden", true)) + assertThat(view.isLongClickable).isFalse + + manager.updateProperties(view, buildStyles("contextMenuHidden", true)) + assertThat(view.isLongClickable).isFalse + + // TextView turns long-clicking back on when a keyboard change sets a key listener and when + // attaching to a window makes the text selectable. + manager.updateProperties(view, buildStyles("keyboardType", "numeric")) + assertThat(view.isLongClickable).isFalse + view.setTextIsSelectable(true) + assertThat(view.isLongClickable).isFalse + + manager.updateProperties(view, buildStyles("contextMenuHidden", false)) + assertThat(view.isLongClickable).isTrue + } + @Test fun testAutoCompleteExtendedHints() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {