-
Notifications
You must be signed in to change notification settings - Fork 182
Description
Describe the bug
After upgrading to Capacitor 7 (which uses compileSdkVersion and targetSdkVersion 35), the camera preview does not occupy the full screen height on Android 11+ when the height parameter is not supplied (i.e. full-screen preview is expected) and the device is in portrait orientation. This issue can be reproduced using the official example-app included in this repository - please see below

Note the extra white space above the nav bar
To Reproduce
Steps to reproduce the behavior:
- Build and run
example-appincluded into this repo - make sure the device is in portrait orientation
- choose to Show Read Camera PreviewNote the extra white space above the nav bar
Please also see the repository with the reproducible example - https://github.com/ryaa/camera-preview-not-full-screen-in-capacitor-7
Expected behavior
No the extra white space above the nav bar and the preview taking the entire screen height
Screenshots
See above
Additional context
The CameraPreview plugin calculates preview height using Display.getSize() or converts DIP input when height is provided.
However, when height is omitted, the code falls back to Display.getSize(), which is not accurate on Android 11+ with edge-to-edge layouts.
As a result, the camera preview ends up shorter than the actual screen height and leaves a blank space at the bottom.
In my case:
rootHeight(from android.R.id.content) = 2265
Calculated preview height = 2177 → incorrect
Please note that Display.getSize(Point) is a legacy API that returns the app window size, excluding certain inset areas (especially on Android 10–14).
On modern full-screen devices (especially gesture nav), this may exclude areas like:
- status bar
- nav bar
- keyboard area
- cutouts
Please note that Android 11+ began deprecating and limiting behavior of Display.getSize(...):
This method may return a size smaller than the actual display size if the window is inset or restricted. - see Android Docs
On Android 13+ with edge-to-edge enforced (Capacitor 7 sets targetSdkVersion=35), the app is now inset-aware by default. That means the app content can go under the system bars.
getSize() seems to only return the "safe" layout bounds (i.e., not including edge-to-edge area).
Potential Fix
Replace the use of Display.getSize() with WindowMetrics (API 30+) or contentView.getHeight() for backward compatibility:
public static int getActualScreenHeight(Activity activity, boolean excludeInsets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = metrics.getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return excludeInsets
? metrics.getBounds().height() - insets.top - insets.bottom
: metrics.getBounds().height();
} else {
View contentView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
return contentView != null ? contentView.getHeight() : fallbackSize.y;
}
}
Workaround
Setting the height explicitly or using MATCH_PARENT in the layout parameters resolves the issue in full-screen preview mode.
However, this workaround breaks if setRect() is used for partial preview mode, so the calculation must still be corrected.
Affected:
capacitor-community/camera-previewv7.0.1- Android 11+ (Capacitor 7+ edge-to-edge mode)
- Only when
heightis not provided, device is in portrait orientation and full-screen preview is expected