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

Text in FlatList is not selectable on Android #26264

Closed
maltoze opened this issue Aug 30, 2019 · 24 comments
Closed

Text in FlatList is not selectable on Android #26264

maltoze opened this issue Aug 30, 2019 · 24 comments
Labels
Bug Component: FlatList Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@maltoze
Copy link
Contributor

maltoze commented Aug 30, 2019

React Native version: 0.60.5

System:
    OS: macOS 10.14.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 317.94 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.3.1 - /usr/local/opt/nvm/versions/node/v12.3.1/bin/node
    Yarn: 1.16.0 - ~/.yarn/bin/yarn
    npm: 6.9.0 - /usr/local/opt/nvm/versions/node/v12.3.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
    Android SDK:
      API Levels: 23, 28, 29
      Build Tools: 28.0.3, 29.0.1
      System Images: android-28 | Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5692245
    Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6
    react-native: 0.60.5 => 0.60.5
  npmGlobalPackages:
    react-native-cli: 2.0.1

Steps To Reproduce

export default class App extends React.Component {
  _renderItem = ({item}) => {
    return (
      <View>
        <Text selectable>{item}</Text>
      </View>
    )
  }

  _keyExtractor = (item, index) => index.toString();

  render() {
    return (
      <View style={styles.container}>
        <Text selectable>This is selectable Text...</Text>
        <FlatList
          data={['not selectable text', 'not selectable text']}
          renderItem={this._renderItem}
          keyExtractor={this._keyExtractor}
        />
      </View>
    );
  }
}

Describe what you expected to happen:

Need to be able to select Text in FlatList.

Snack, code example, screenshot, or link to a repository:

Snack: https://snack.expo.io/@maltoze/selectable-bug-in-flatlist

@react-native-bot
Copy link
Collaborator

Thanks for submitting your issue. Can you take another look at your description and make sure the issue template has been filled in its entirety?

👉 Click here if you want to take another look at the Bug Report issue template.

@react-native-bot react-native-bot added Ran Commands One of our bots successfully processed a command. Resolution: Needs More Information labels Aug 30, 2019
@baldursson
Copy link
Contributor

Also noticed this. I just upgraded my app from React Native 0.57.4 to 0.60.5 and now I can't select text on Android.

@chathuraSupun
Copy link

chathuraSupun commented Sep 20, 2019

Text component rendered in FlatList is not selectable even if selectable={true} is specified.

@baldursson
Copy link
Contributor

I curse the day I let React Native into my life

@tryvin
Copy link

tryvin commented Oct 2, 2019

Confirmed in 0.59.8 too, spent a good 30 minutes trying to figure out why this wasn't working on an Android 9.

@Kiryhas
Copy link

Kiryhas commented Oct 13, 2019

Can confirm, the Text component is selectable, but when rendered as a FlatList element, it is not selectable regardless of the 'selectable' prop.

@tryvin
Copy link

tryvin commented Oct 13, 2019

In my case I could remove the flat list, and use just a scroll view, but this isn't the ideal.

I even did a if for the platform.OS, ios uses flat list, Android uses scroll view

@ezranbayantemur
Copy link

I have this problem too. Text component can't selectable on FlatList..

@Ericky14
Copy link

Same problem here

@fenglu09
Copy link

fenglu09 commented Nov 22, 2019

Same problem in 0.59.10
In my case, add key={Math.random()} solved it

@kangsu-dev
Copy link

key={Math.random()} not works for me.(rn 0.61.2)
Instead, adding removeClippedSubviews={false} for Flatlist solved it.

@Rohansi
Copy link

Rohansi commented Nov 30, 2019

Expo SDK 35.0.0 also has this problem. RN 0.59.8.

removeClippedSubviews={false} does work around it for me.

@khorark
Copy link

khorark commented Dec 9, 2019

Added pointerEvents={'box-none'} for wrapper View solved this problem.

@stale
Copy link

stale bot commented Apr 21, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 21, 2020
@Rohansi
Copy link

Rohansi commented Apr 21, 2020

I'm on Expo SDK 37 now (RN 0.61.4). Still need removeClippedSubviews={false} to select text.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 21, 2020
@fabOnReact
Copy link
Contributor

fabOnReact commented May 18, 2020

as displayed in the following Snack adding removeClippedSubviews={false} to the FlatList fixes the issue. @maltoze would you consider closing this issue? React-Native has currently 744 open issues. 😃 ☮️ Thanks Fabrizio

@Rohansi
Copy link

Rohansi commented May 19, 2020

as displayed in the following Snack adding removeClippedSubviews={false} to the FlatList fixes the issue. @maltoze would you consider closing this issue? React-Native has currently 744 open issues. 😃 ☮️ Thanks Fabrizio

That's not a fix -- it's a workaround. It sacrifices FlatList performance to be able to select text.

@fabOnReact

This comment has been minimized.

@fabOnReact
Copy link
Contributor

fabOnReact commented May 19, 2020

SOLVED

I'm preparing a PR, applying the same solution from #28852 adding super.setTextIsSelectable(true); to ReactTextView

  @Override
  public void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (mContainsImages && getText() instanceof Spanned) {
      Spanned text = (Spanned) getText();
      TextInlineImageSpan[] spans = text.getSpans(0, text.length(), TextInlineImageSpan.class);
      for (TextInlineImageSpan span : spans) {
        span.onAttachedToWindow();
      }
    }
    super.setTextIsSelectable(true);
  }

I am preparing the PR. Thanks 😃

I will need to verify for regression of functionalities included in a0268a7

@stale
Copy link

stale bot commented Aug 22, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 22, 2020
@stale
Copy link

stale bot commented Aug 29, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Aug 29, 2020
@zoolyka
Copy link

zoolyka commented Sep 15, 2020

Actually there is another workaround which does not require to disable removeClippedSubviews. Example:

export default class SelectableText extends React.Component {

  constructor ( props ) {

    super(props);

    this.state = {
      selectableFix: false
    };

    this.selectableFixTimeout = null;

  }

  shouldComponentUpdate ( nextProps, nextState ) {
    return(
      this.props.text !== nextProps.text ||
      this.state.selectableFix !== nextState.selectableFix
    );
  }

  componentDidMount () {
    this.selectableFixTimeout = setTimeout(() => {
      this.setState({
        selectableFix: true
      });
    }, 100);
  }

  componentWillUnmount () {
    clearTimeout(this.selectableFixTimeout);
  }

  render () {
    return(
      <Text
        selectable={this.state.selectableFix}
      >
        {this.props.text}
      </Text>
    );
  }

}

@fabOnReact
Copy link
Contributor

fabOnReact commented Feb 25, 2021

This issue should not been closed. My pr #28952 was written to fix this issue, I can improve the pr but please don't close the issue.

@douglasjunior
Copy link

douglasjunior commented Jun 22, 2021

The problem still exists on React Native 0.64.2 10 months later after being reported.

Settings removeClippedSubviews={false} on FlatList temporarily solves the issue.

Thank you, @fabriziobertoglio1987, I hope they pay attention to your PR!

facebook-github-bot pushed a commit that referenced this issue Sep 1, 2021
Summary:
This issue fixes #26264 fixes #27107
Text is not selectable inside a FlatList on Android. The solution is to invalidate the ReactTextView after a change of the selectable prop. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future and make the Text selectable.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Fix non selectable Text in FlatList

Pull Request resolved: #28952

Test Plan:
**<details><summary>CLICK TO OPEN TESTS RESULTS</summary>**
<p>

The issue was demonstrated in the following [snack](https://snack.expo.io/fabrizio.bertoglio/selectable-bug-in-flatlist) (more info in issue #26264).

The solution is:

1) Calling `invalidate()` from [setSelectableText][1] after changing the `selectable` prop and `mSelectableText` value. [`invalidate()`](https://developer.android.com/reference/android/view/View#invalidate()) triggers the `onDraw` callback.

[1]: https://github.com/fabriziobertoglio1987/react-native/blob/8027524947cafd5cbdc492e4ef9c92b346fe23fc/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java#L427-L430

2) calling `setTextIsSelectable(mSelectableText);` from the [`onDraw`][2] callback

[2]: https://github.com/fabriziobertoglio1987/react-native/blob/8027524947cafd5cbdc492e4ef9c92b346fe23fc/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java#L456-L460

The example below is availabe in RNTester FlatList example. Two options (`onPressDisabled` and `textSelectable`) have been added to test the functionality inside a FlatList.

<p align="center">
<img src="https://user-images.githubusercontent.com/24992535/82672536-c2e74d80-9c40-11ea-8fd8-156bfacfac8a.gif" width="200" height="" />
</p>

</p>
</details>

Reviewed By: ShikaSD

Differential Revision: D30000870

Pulled By: lunaleaps

fbshipit-source-id: 4851a294960df0af057d006793aa9ba97c51e3f9
@facebook facebook locked as resolved and limited conversation to collaborators Oct 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Component: FlatList Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

Successfully merging a pull request may close this issue.