Screen Display in your Napacitor App
The @napacitor/display plugin wraps the .NET MAUI DeviceDisplay API, providing screen metric reads, display change listening, keep-awake control, and runtime orientation locking. One API covers Android, iOS, Mac Catalyst, and Windows (some capabilities differ per platform, see below).
Installation
npm install @napacitor/display
npx nap sync
Reading Screen Info
import { Display } from '@napacitor/display';
const info = await Display.getInfo();
console.log(info);
// { width: 1080, height: 2340, density: 2.75, orientation: 'portrait', rotation: 0, refreshRate: 120 }
Maps to the Width / Height / Density / Orientation / Rotation / RefreshRate of MAUI DeviceDisplay.Current.MainDisplayInfo.
Listening for Display Changes
const handle = await Display.addListener('displayChange', (info) => {
console.log('Display metrics changed:', info);
});
// Remove when no longer needed:
await handle.remove();
Maps to the MAUI MainDisplayInfoChanged event, which fires when any screen metric changes (such as the device orientation switching between portrait and landscape).
Keeping the Screen Awake
// Prevent the device from locking/sleeping (e.g. on a video page):
await Display.setKeepAwake({ keepAwake: true });
// Restore default behavior:
await Display.setKeepAwake({ keepAwake: false });
Maps to MAUI KeepScreenOn.
Screen Orientation
Many apps work well in portrait and landscape device orientations. However, many don't, and there are good reasons to require an app to function solely or occasionally in one mode or the other.
Global Orientation Settings
To set a global setting for orientation in your Napacitor app, you'll set the configuration value necessary for the platform you're targeting.
iOS Configuration
iOS allows for different screen orientations to be supported on iPhones and iPads. To limit the allowed orientations for iOS, open the host project and edit the maui/Platforms/iOS/Info.plist file. Find the following keys: Supported interface orientation and Supported interface orientation (iPad). Using these values, specify the different orientations you would like supported for iPhones and for iPads.
If editing the Info.plist file directly, look for the following keys: UISupportedInterfaceOrientations and UISupportedInterfaceOrientations~ipad. For example, the following settings will limit the orientation to right-side-up Portrait on iPhones and either of the Landscape orientations on iPads:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
Android Configuration
On Android, orientation can be set by modifying the maui/Platforms/Android/AndroidManifest.xml and setting android:screenOrientation on the <activity> entry for your main app activity. See the Android Manifest Documentation for details on the possible entries.
Dynamic Orientation Settings
Many apps need to support multiple orientations, with the ability to lock orientations occasionally depending on the content. Use the lock and unlock methods of @napacitor/display:
import { Display } from '@napacitor/display';
await Display.lock({ orientation: 'portrait' });
await Display.lock({ orientation: 'landscape' });
// To unlock orientation which will default back to the global setting:
await Display.unlock();
Platform notes:
- Android: sets the current Activity's
RequestedOrientation(Portrait/Landscape;Unspecifiedon unlock, falling back to the manifest's global setting). - iOS / Mac Catalyst: runtime orientation locking uses the
UIDeviceorientation switch mechanism and may not take effect in some scenarios on iOS 16+; the orientation you lock must be declared inUISupportedInterfaceOrientationsinInfo.plist. - Windows: runtime locking is not supported (throws
NOT_IMPLEMENTED); resize the window or use system display settings instead. - Web: relies on the Screen Orientation API, which usually requires fullscreen or an installed PWA.
iPad Orientation Lock
By default, an iPad allows Multitasking and its orientation cannot be locked. If you need to lock orientation on an iPad, set the option Requires Full Screen to YES by adding the following to Info.plist:
<key>UIRequiresFullScreen</key>
<true/>
Web Behavior
Browsers have no unified screen-metrics or keep-awake API, so the web implementation of @napacitor/display is best-effort:
getInfo()is based onwindow.screenanddevicePixelRatio(the refresh rate is unknowable in browsers, always 0);setKeepAwake()is based on the Wake Lock API;lock()/unlock()are based on the Screen Orientation API (usually requires fullscreen or an installed PWA);displayChangeis based onresize/orientationchangeevents.