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

UIManager.measureInWindow on android not work as document #29638

Closed
jinshiyi11 opened this issue Aug 13, 2020 · 12 comments
Closed

UIManager.measureInWindow on android not work as document #29638

jinshiyi11 opened this issue Aug 13, 2020 · 12 comments
Labels
Platform: Android Android applications. Resolution: PR Submitted A pull request with a fix has been provided. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@jinshiyi11
Copy link

Description

UIManager.measureInWindow on android not work as document.
in ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

/**
   * Determines the location on screen, width, and height of the given view relative to the device
   * screen and returns the values via an async callback. This is the absolute position including
   * things like the status bar
   */
  @ReactMethod
  public void measureInWindow(int reactTag, Callback callback) {
    mUIImplementation.measureInWindow(reactTag, callback);
  }

it saids that

This is the absolute position including things like the status bar

.But in _ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java
React Native version 0.60.5,it removes the status bar from the height

public synchronized void measureInWindow(int tag, int[] outputBuffer) {
    UiThreadUtil.assertOnUiThread();
    View v = mTagsToViews.get(tag);
    if (v == null) {
      throw new NoSuchNativeViewException("No native view for " + tag + " currently exists");
    }

    v.getLocationOnScreen(outputBuffer);

    // We need to remove the status bar from the height.  getLocationOnScreen will include the
    // status bar.
    Resources resources = v.getContext().getResources();
    int statusBarId = resources.getIdentifier("status_bar_height", "dimen", "android");
    if (statusBarId > 0) {
      int height = (int) resources.getDimension(statusBarId);
      outputBuffer[1] -= height;
    }

    // outputBuffer[0,1] already contain what we want
    outputBuffer[2] = v.getWidth();
    outputBuffer[3] = v.getHeight();
  }

and in lateset version ,it subtract visibleWindowCoords(the master version)

public synchronized void measureInWindow(int tag, int[] outputBuffer) {
    UiThreadUtil.assertOnUiThread();
    View v = mTagsToViews.get(tag);
    if (v == null) {
      throw new NoSuchNativeViewException("No native view for " + tag + " currently exists");
    }

    v.getLocationOnScreen(outputBuffer);

    // we need to subtract visibleWindowCoords - to subtract possible window insets, split screen or
    // multi window
    Rect visibleWindowFrame = new Rect();
    v.getWindowVisibleDisplayFrame(visibleWindowFrame);
    outputBuffer[0] = outputBuffer[0] - visibleWindowFrame.left;
    outputBuffer[1] = outputBuffer[1] - visibleWindowFrame.top;

    // outputBuffer[0,1] already contain what we want
    outputBuffer[2] = v.getWidth();
    outputBuffer[3] = v.getHeight();
  }

React Native version:

React Native version 0.60.5

Steps To Reproduce

Expected Results

absolute position including things like the status bar

@react-native-bot react-native-bot added the Platform: Android Android applications. label Aug 13, 2020
@safaiyeh safaiyeh added Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available and removed Needs: Triage 🔍 labels Aug 15, 2020
@github-actions
Copy link

⚠️ Missing Reproducible Example
ℹ️ It looks like your issue is missing a reproducible example. Please provide a Snack or a repository that demonstrates the issue you are reporting in a minimal, complete, and reproducible manner.

@github-actions
Copy link

⚠️ Using Old Version
ℹ️ It looks like you are using an older version of React Native. Please upgrade to the latest version, and verify if the issue persists. If it does not, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the current release.

@gedeagas
Copy link
Contributor

gedeagas commented Aug 19, 2020

@safaiyeh just checked this on RNTester and it seems that the comment is wrong. On Android, the measureInWindow function does not include things like the status bar.

The function will return x:0 y:0 instead of x:0 y:${px from statusbar}. That said I don't know what the "correct" behavior is.

Note: haven't tested the behavior on iOS.

@gedeagas
Copy link
Contributor

Related issue #19497

@gedeagas
Copy link
Contributor

gedeagas commented Aug 19, 2020

@safaiyeh Just confirmed there is a feature parity problem between iOS and Android. The ios return y with status bar height while the android implementation does not. This issue is first discovered on #19497 and mentioned on RN Issue Triage Group 2 #24029.

I already make WIP PR on this but need a proper review from the React Native team. ( Multi Window problem, etc.,)
#29691

@safaiyeh
Copy link
Contributor

@gedeagas appreciate the PR!

@safaiyeh safaiyeh added Resolution: PR Submitted A pull request with a fix has been provided. and removed Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available labels Aug 19, 2020
@phasjim
Copy link

phasjim commented May 13, 2021

Hey there!
I am running React Native 0.63.4 and noticing that undefined is still being returned on Android when using measureInWindow. This works fine on iOS. I looked at all the linked issues and tried their suggestions and to no avail. Wanted to bring this to your attention! Thanks

@cosmoyoung
Copy link

collapsable: false

@QuentinGprd
Copy link

collapsable: false

Thanks! Indeed, measureInWindow return values on Android after adding collapsable={false} as a prop on the measured view.

See https://reactnative.dev/docs/view#collapsable-android

@github-actions
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 Jun 23, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity.

@tj-mc
Copy link

tj-mc commented Sep 25, 2023

Confirming that collapsable={false} is working on 0.69 to make Android views report size from measureInWindow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Android Android applications. Resolution: PR Submitted A pull request with a fix has been provided. 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.

8 participants