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

TextInput onContentSizeChange returns incorrect layout size instead of content size on Android #29702

Open
Traviskn opened this issue Aug 19, 2020 · 18 comments
Labels
Component: TextInput Related to the TextInput component. Needs: Triage 🔍 Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications.

Comments

@Traviskn
Copy link

Description

The onContentSizeChange prop of the TextInput component does not work on Android as it does on iOS. On Android it seems to be returning the layout height and width of the TextInput view, rather than the height and width of the text.

This same issue has been reported before in #25487 and #19696, but does not seem to have been fixed

React Native version:

System:
OS: macOS 10.15.6
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 71.45 MB / 32.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.6 - ~/.nvm/versions/node/v12.18.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /Users/travisnuttall/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK:
API Levels: 28, 29, 30
Build Tools: 28.0.3, 29.0.2, 30.0.1
System Images: android-30 | Google Play Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 4.0 AI-193.6911.18.40.6626763
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_242-release - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: 0.63.2 => 0.63.2
npmGlobalPackages:
react-native: Not Found

Steps To Reproduce

Run the following code on both iOS and Android platforms, note the inconsistency in the logged values from `contentSize:

import React from 'react';

import { View, TextInput } from 'react-native';

export default class App extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, marginTop: 40 }}>
        <TextInput
          defaultValue="test"
          onLayout={ e=> {
            console.warn("single line layout", e.nativeEvent.layout)
          }}
          style={{ width: 200, lineHeight: 30, backgroundColor: '#CCC' }}
          onContentSizeChange={ e=> {
            console.warn(e.nativeEvent.contentSize)
          }}
        />
        <TextInput
          defaultValue="test"
          onLayout={ e=> {
            console.warn(e.nativeEvent.layout)
          }}
          multiline
          style={{ width: 200, lineHeight: 30, backgroundColor: '#CCC' }}
          onContentSizeChange={ e=> {
            console.warn(e.nativeEvent.contentSize)
          }}
        />
      </View>
    )
  }
}

Expected Results

On iOS as you edit text on single or multiline TextInput you get the actual width of the characters entered. I would expect the same behavior on Android. Effectively this functionality is not actually implemented on Android at the moment.

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

See code sample in reproducible steps

@react-native-bot react-native-bot added Component: TextInput Related to the TextInput component. Platform: Android Android applications. labels Aug 19, 2020
@stale
Copy link

stale bot commented Dec 25, 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 Dec 25, 2020
@jer-sen
Copy link

jer-sen commented Dec 27, 2020

This is still an issue !

@Bardiamist
Copy link
Contributor

I changing value not from onChangeText
My conthent height now: 59.5
But onContentSizeChange fires once with previous height: 43

it's wrong. onContentSizeChange should trigger with 59.5 😔

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 17, 2021
@gabormagyar
Copy link

This is still still an issue! onContentSize returns the size of the textinput, not the text inside it on Android.

@diegobugs
Copy link

This is still an issue !

JHSieber referenced this issue Jun 22, 2021
Summary:
Previously <TextInput>'s onContentSizeChange event fires very rearly, usually just once after initial layout. This diff fixed that.
I also considered to a bunch of another things to get the native notification, but I found that overriding `onTextChanged` is the most reliable, easy and effitient way to implement this.

I tried/considered:
 * onLayout (does not fire)
 * OnPreDrawListener (fires to often)
 * OnGlobalLayoutListener (does not fire)
 * OnLayoutChangeListener (does not fire)
 * isLayoutRequested (too hacky)

(I also fixed the <AutoExpandingTextInput> demo to illustrate the fix.)

And just heads up, we will remove `contentSize` info from `onChange` event very soon.

GH issue: #11692

Reviewed By: achen1

Differential Revision: D5132589

fbshipit-source-id: e7edbd8dc5ae891a6f4a87b51d9450b8c6ce4a1e
@kevinleemans
Copy link

Still an issue

@stale
Copy link

stale bot commented Jan 9, 2022

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 Jan 9, 2022
@Bardiamist
Copy link
Contributor

Still an issue

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 9, 2022
@mustafaasif1
Copy link

still an issue

@popocreator
Copy link

Still an issue

@juanlarra1
Copy link

still an issue

@FontSanguinetti
Copy link

FontSanguinetti commented Sep 1, 2022

Guys, I found the solution. It's incredible unintuitive but try adding a backgroundColor to the input...

CleanShot.2022-09-01.at.17.35.13.mp4

@SimonAM
Copy link

SimonAM commented Oct 12, 2022

@FontSanguinetti could you give me a little more detail into how this is related to onContentSizeChange?

@PrimulaX
Copy link

still an issue

@namchuai
Copy link

namchuai commented Jul 1, 2023

This issue is driving me nut. Trying to calculate it using StaticLayout I can't get the correct value..

@eeshankeni
Copy link

This is absolutely messing up my logic to increase the size of the text input as the user types. Wtf man why is react native on android such a pain

Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 25, 2024
@PrimulaX
Copy link

Open sesame

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 26, 2024
@cortinico cortinico added the Never gets stale Prevent those issues and PRs from getting stale label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: TextInput Related to the TextInput component. Needs: Triage 🔍 Never gets stale Prevent those issues and PRs from getting stale Platform: Android Android applications.
Projects
None yet
Development

No branches or pull requests