Skip to content

Fix Dimensions window values on Android < 15#53254

Open
zoontek wants to merge 4 commits intofacebook:mainfrom
zoontek:fix-dimensions-api
Open

Fix Dimensions window values on Android < 15#53254
zoontek wants to merge 4 commits intofacebook:mainfrom
zoontek:fix-dimensions-api

Conversation

@zoontek
Copy link
Contributor

@zoontek zoontek commented Aug 13, 2025

Summary:

⚠️ Note: This relies on #56055 being merged first.

This PR (initially created for edge-to-edge opt-in support, rebased multiple times) fixes the Dimensions API window values on Android < 15, when edge-to-edge is enabled.

Currently the window height doesn't include the status and navigation bar heights (but it does on Android >= 15):

Screenshot 2025-06-27 at 16 23 02

Using WindowMetricsCalculator from AndroidX:

Screenshot 2025-06-27 at 16 34 01

Fixes #47080

Changelog:

[Android] [Fixed] Fix Dimensions window values on Android < 15 when edge-to-edge is enabled

Test Plan:

Run the example app on an Android < 15 device.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 13, 2025
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 13, 2025
@zoontek zoontek force-pushed the fix-dimensions-api branch from 22726c1 to c95b5aa Compare August 13, 2025 17:46
@alanleedev alanleedev requested review from alanleedev, cortinico and javache and removed request for javache August 14, 2025 05:30
@facebook-github-bot
Copy link
Contributor

@alanleedev has imported this pull request. If you are a Meta employee, you can view this in D80237818.

@zoontek zoontek force-pushed the fix-dimensions-api branch from c95b5aa to 47d8ed5 Compare August 20, 2025 10:14
@react-native-bot
Copy link
Collaborator


Warnings
⚠️ ❗ JavaScript API change detected - This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API. Please include a clear changelog message. This change will be subject to extra review.

This change was flagged as: BREAKING

Generated by 🚫 dangerJS against 6b5397a

@facebook-github-bot
Copy link
Contributor

@alanleedev has imported this pull request. If you are a Meta employee, you can view this in D80237818.

@alanleedev
Copy link
Contributor

@zoontek There was a question if we could to" keep initDisplayMetrics as a backwards-compat API and have it call both". I haven't quite figured out the internal crash yet. And it does seem like new API does make things bit more fragile. Do you have any thoughts here?

@zoontek
Copy link
Contributor Author

zoontek commented Sep 2, 2025

@alanleedev One alternative could be to extract the is UI thread method from AndroidX, set both screen and window dimensions when initDisplayMetrics is called, and only update window dimensions when the context passed is UI context. Not perfect as dimensions might be initialize with screen metrics before switching to window ones, but at least it keeps the same API.

@react-native-bot
Copy link
Collaborator

This PR 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.

@react-native-bot
Copy link
Collaborator

This PR is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label.

@react-native-bot react-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 2, 2026
@zoontek zoontek marked this pull request as draft March 2, 2026 10:24
@zoontek zoontek force-pushed the fix-dimensions-api branch 2 times, most recently from 9e4c7de to 6ccb1ef Compare March 2, 2026 12:28
@zoontek zoontek marked this pull request as ready for review March 2, 2026 13:31
@zoontek
Copy link
Contributor Author

zoontek commented Mar 2, 2026

As main evolved a lot since I opened this branch, I reworked on it, was able to keep the changes minimal and only compute window dimensions when required.

@Montiwa11
Copy link

As main evolved a lot since I opened this branch, I reworked on it, was able to keep the changes minimal and only compute window dimensions when required.

@zoontek zoontek force-pushed the fix-dimensions-api branch from 8b4c84a to 84d709e Compare March 2, 2026 21:35
@react-native-bot react-native-bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 3, 2026
}

@JvmStatic
public fun getDisplayMetricsWritableMap(fontScale: Double): WritableMap {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods have been moved to DeviceInfoModule.kt, as they are only used there.

}

@Test
fun getDisplayMetricsWritableMap_returnsCorrectMap() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has been moved to DeviceInfoModuleTest.kt

@Before
fun setUp() {
context = RuntimeEnvironment.getApplication()
DisplayMetricsHolder.setWindowDisplayMetrics(null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PixelUtil.kt is not using window display metrics since this commit. Those can be safely removed.

+ notchHeight;

int heightPixels = getContext().getResources().getDisplayMetrics().heightPixels;
final ReactContext reactContext = getCurrentReactContext();
Copy link
Contributor Author

@zoontek zoontek Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same logic used in DeviceInfoModule.kt getWindowDisplayMetrics, but it has been inlined instead of extracted as a function, since checkForKeyboardEventsLegacy will be deleted here

@zoontek zoontek force-pushed the fix-dimensions-api branch 2 times, most recently from 423f52e to cc6c878 Compare March 11, 2026 13:42
@zoontek zoontek force-pushed the fix-dimensions-api branch from 9e07deb to 9ffbd21 Compare March 11, 2026 14:32
layout.text,
layout,
DisplayMetricsHolder.getWindowDisplayMetrics(),
DisplayMetricsHolder.getScreenDisplayMetrics(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK, as it's only used for screen density.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dimensions.get('window').height not consistent in Android 14 and Android 15

5 participants