Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Using TextInput inside ViewPagerAndroid causes context menu (copy/paste) in some cases to not display #20887

Closed
3 tasks done
jamsch opened this issue Aug 28, 2018 · 16 comments
Labels
Bug Component: TextInput Related to the TextInput component. Platform: Android Android applications. Platform: Linux Building on Linux. Resolution: Locked This issue was locked by the bot.

Comments

@jamsch
Copy link
Contributor

jamsch commented Aug 28, 2018

Environment

React Native Environment Info:
    System:
      OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
      CPU: x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
      Memory: 284.57 MB / 15.54 GB
      Shell: 4.4.19 - /bin/bash
    Binaries:
      Node: 8.11.2 - ~/.nvm/versions/node/v8.11.2/bin/node
      Yarn: 1.9.4 - /usr/bin/yarn
      npm: 6.3.0 - ~/.nvm/versions/node/v8.11.2/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      Android SDK:
        Build Tools: 23.0.1, 23.0.2, 23.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 27.0.0, 27.0.1, 27.0.3
        API Levels: 23, 24, 25, 26, 27
    npmPackages:
      @storybook/react-native: ^4.0.0-alpha.16 => 4.0.0-alpha.16
      react: ^16.4.2 => 16.4.2
      react-native: ^0.56.0 => 0.56.0

Description

  1. Place a TextInput in ViewPagerAndroid
  2. Attempt to select text from TextInput
  3. Android logcat will display: "TextView does not support text selection. Selection cancelled."

The following steps will not always reproduce the bug but in some configurations it can cause the effect. One of the apps I'm working on always has this bug however I am yet to find the exact reason why (perhaps due to a heavier component mount tree or subsequent component updates).

Another observation is that this is most prevalent if the device orientation is locked. When rotating to landscape and back to portrait, it seems to regain function. This may be due to View.onMeasure(int, int) and following View.onAttachedToWindow() being called on Native Android.

Another observation is that resuming the app after being in the background for a while will cause this issue.

This has not been working for at least the last couple of RN versions. It does not work on 0.57.0-rc.3 either. Here's someone experiencing the same issue in 2016: link.

Reproducible Demo

https://snack.expo.io/SJurHkrvm

Note: Perform a small change in the snack and let the app hot reload. Text selection ability should be lost.

jamsch added a commit to jamsch/react-native that referenced this issue Aug 28, 2018
In some instances setting style properties such as height to an EditText inside a ViewPager will not allow the user to select text.
Android logcat will instead display: "TextView does not support text selection. Selection cancelled."

This seems to be an Android bug -  https://code.google.com/p/android/issues/detail?id=208169

This workaround toggles the "enabled" state when the EditText is attached to the window to circumvent this bug.

Fixes facebook#20887
@react-native-bot react-native-bot added Platform: Android Android applications. Component: TextInput Related to the TextInput component. Platform: Linux Building on Linux. labels Aug 28, 2018
@jamsch jamsch changed the title [Android] setting height on TextInput inside ViewPagerAndroid causes context menu (copy/paste) to not display [Android] Using TextInput inside ViewPagerAndroid causes context menu (copy/paste) in some cases to not display Aug 28, 2018
@jamsch
Copy link
Contributor Author

jamsch commented Aug 28, 2018

Here's also a current hacky workaround tested for the current RN version without having to change native code.

class InputField extends React.Component<Props, State> {

  static defaultProps = {
    editable: true,
  }
  
  constructor(props) {
    super(props);
    this.state = {
      editable: !props.editable
    };
  }

  componentDidMount() {
    if (this.props.editable) {
      setTimeout(() => {
        this.setState({ editable: true });
      }, 100);
    }
  }

  render() {
    const { editable } = this.state;
    return <TextInput {...this.props} editable={editable} />;
  }
}

@jamsch
Copy link
Contributor Author

jamsch commented Aug 30, 2018

Updated Snack with minimal repro. Just perform a small edit, let the app hot reload and attempt to select text from the first text box.

@jamsch
Copy link
Contributor Author

jamsch commented Aug 30, 2018

Here's a small example. Notice that text selection isn't possible on the first TextInput after a hot reload.

peek 2018-08-31 11-22

@AlexandreMagniez
Copy link

AlexandreMagniez commented Oct 18, 2018

I suffered from this issue too. In my case, a 2nd render is necessary to enable contextMenu on Android.
Thanks to @jamsch comment, I've been able to make it work.

@imwexpex
Copy link

imwexpex commented Nov 28, 2018

Hi, i have this issue too! Any updates or native workarounds?

Also, reproducable example: https://snack.expo.io/S1A0eJ30X

@jamsch
Copy link
Contributor Author

jamsch commented Dec 12, 2018

Looks like 0.58.0-rc.1 seems to solve this issue. There's about a thousand commits between the previous release so I'm not sure exactly what had fixed it.

@stwiname
Copy link

This is still an issue in 0.59.2

@shortcarver
Copy link

Still an issue in 0.59.3

@jacquesdev
Copy link

jacquesdev commented Jun 25, 2019

Still an issue in 0.59.9, but at least the workaround does the job #20887 (comment)

@jacquesdev
Copy link

jacquesdev commented Jun 25, 2019

@jamsch

if (this.props.editable) {
      setTimeout(() => {
        this.setState({ editable: true });
      }, 100);
    }

Should this not always update the state to the prop? So something like this

setTimeout(() => {
  this.setState({ editable: this.props.editable });
}, 100);

@youngjuning
Copy link

@jamsch why close this bug!!!

@timgws
Copy link

timgws commented Sep 16, 2019

@cpojer can you please reopen this issue? This issue still appears in 0.59.9.

EDIT: this seems very similar to another, older bug from version 0.32.1 #9958

@vladbars
Copy link

still issue in RN 0.60.3

@JulianKingman
Copy link

If you're using react-native-screens, the issue could be as referenced here: software-mansion/react-native-screens#40 (comment)

@WouterFlorijn
Copy link

@cpojer Could you please re-open this?

@chumakoff
Copy link

Still an issue in RN 0.61.4
The #20887 workaround works when a TextInput is editable.
However, when a TextInput is not editable (editable={false}) the workaround doesn't help.

I used a 'hooks' version of the workaround:

const [editable, setEditable] = useState(false);

useEffect(() => {
    setEditable(true);
}, []);

<TextInput editable={editable} />

@facebook facebook locked as resolved and limited conversation to collaborators Feb 15, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Feb 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Component: TextInput Related to the TextInput component. Platform: Android Android applications. Platform: Linux Building on Linux. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests