Skip to content

Commit 86994a6

Browse files
zoontekfacebook-github-bot
authored andcommitted
Fix Dimensions window values on Android < 15 (#52481)
Summary: Pull Request resolved: #52481 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): <img width="300" alt="Screenshot 2025-06-27 at 16 23 02" src="https://github.com/user-attachments/assets/c7d11334-9298-4f7f-a75c-590df8cc2d8a" /> Using `WindowMetricsCalculator` from AndroidX: <img width="300" alt="Screenshot 2025-06-27 at 16 34 01" src="https://github.com/user-attachments/assets/7a4e3dc7-a83b-421b-8f6d-fd1344f5fe81" /> Fixes #47080 ## Changelog: [Android] [Fixed] Fix `Dimensions` `window` values on Android < 15 when edge-to-edge is enabled Pull Request resolved: #47554 Test Plan: Run the example app on an Android < 15 device. Rollback Plan: Reviewed By: cortinico Differential Revision: D77906644 Pulled By: alanleedev fbshipit-source-id: 121cd6bc4133973f06b28eb9e79c9387ac7070a1
1 parent b795d61 commit 86994a6

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

packages/react-native/ReactAndroid/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ dependencies {
621621
api(libs.androidx.autofill)
622622
api(libs.androidx.swiperefreshlayout)
623623
api(libs.androidx.tracing)
624+
api(libs.androidx.window)
624625

625626
api(libs.fbjni)
626627
api(libs.fresco)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLay
865865
private int mDeviceRotation = 0;
866866

867867
/* package */ CustomGlobalLayoutListener() {
868-
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
868+
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext());
869869
mVisibleViewArea = new Rect();
870870
mMinKeyboardHeightDetected = (int) PixelUtil.toPixelFromDIP(60);
871871
}
@@ -988,7 +988,7 @@ private void checkForDeviceOrientationChanges() {
988988
return;
989989
}
990990
mDeviceRotation = rotation;
991-
DisplayMetricsHolder.initDisplayMetrics(getContext().getApplicationContext());
991+
DisplayMetricsHolder.initDisplayMetrics(getContext());
992992
emitOrientationChanged(rotation);
993993
}
994994

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/DisplayMetricsHolder.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import android.util.DisplayMetrics
1313
import android.view.WindowManager
1414
import androidx.core.view.ViewCompat
1515
import androidx.core.view.WindowInsetsCompat
16+
import androidx.window.layout.WindowMetricsCalculator
1617
import com.facebook.react.bridge.WritableMap
1718
import com.facebook.react.bridge.WritableNativeMap
19+
import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn
1820

1921
/**
2022
* Holds an instance of the current DisplayMetrics so we don't have to thread it through all the
@@ -62,16 +64,28 @@ public object DisplayMetricsHolder {
6264
@JvmStatic
6365
public fun initDisplayMetrics(context: Context) {
6466
val displayMetrics = context.resources.displayMetrics
65-
windowDisplayMetrics = displayMetrics
67+
val windowDisplayMetrics = DisplayMetrics()
6668
val screenDisplayMetrics = DisplayMetrics()
69+
70+
windowDisplayMetrics.setTo(displayMetrics)
6771
screenDisplayMetrics.setTo(displayMetrics)
72+
73+
if (isEdgeToEdgeFeatureFlagOn) {
74+
WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(context).let {
75+
windowDisplayMetrics.widthPixels = it.bounds.width()
76+
windowDisplayMetrics.heightPixels = it.bounds.height()
77+
}
78+
}
79+
6880
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
6981
// Get the real display metrics if we are using API level 17 or higher.
7082
// The real metrics include system decor elements (e.g. soft menu bar).
7183
//
7284
// See:
7385
// http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
7486
@Suppress("DEPRECATION") wm.defaultDisplay.getRealMetrics(screenDisplayMetrics)
87+
88+
DisplayMetricsHolder.windowDisplayMetrics = windowDisplayMetrics
7589
DisplayMetricsHolder.screenDisplayMetrics = screenDisplayMetrics
7690
}
7791

packages/react-native/gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ androidx-swiperefreshlayout = "1.1.0"
1616
androidx-test = "1.5.0"
1717
androidx-test-junit = "1.2.1"
1818
androidx-tracing = "1.1.0"
19+
androidx-window = "1.4.0"
1920
assertj = "3.21.0"
2021
binary-compatibility-validator = "0.13.2"
2122
download = "5.4.0"
@@ -64,6 +65,7 @@ androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidx-
6465
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test" }
6566
androidx-tracing = { module = "androidx.tracing:tracing", version.ref = "androidx-tracing" }
6667
androidx-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
68+
androidx-window = { module = "androidx.window:window", version.ref = "androidx-window" }
6769

6870
fbjni = { module = "com.facebook.fbjni:fbjni", version.ref = "fbjni" }
6971
fresco = { module = "com.facebook.fresco:fresco", version.ref = "fresco" }

0 commit comments

Comments
 (0)