Screen Orientation
Current Screen Orientation
Device Orientation
Detect Orientation
Screen Orientation Guide

How can I get the current screen orientation?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding how to get the current screen orientation is essential for developing responsive applications that adapt to different device orientations. In this article, we will explore various methods to detect screen orientation in web and mobile applications. We'll cover technical details, code examples, and considerations for different platforms, particularly focusing on web browsers and mobile devices.

Understanding Screen Orientation

Screen orientation refers to the way a display screen is oriented concerning its physical environment. Most modern devices can be held in at least two orientations - portrait (vertical) and landscape (horizontal). Detecting these orientations programmatically allows developers to optimize user experiences, such as adjusting layouts or functionalities accordingly.

Key Concepts

  • Portrait Orientation: The display is taller than it is wide.
  • Landscape Orientation: The display is wider than it is tall.
  • Orientation Change Event: An event triggered when there is a change in screen orientation.

Detecting Screen Orientation in Web Browsers

In web development, screen orientation can be determined using the Screen Orientation API and JavaScript's window object. Below, we'll delve into these methods.

Using the Screen Orientation API

The Screen Orientation API provides a standardized way to detect and respond to orientation changes. This API can be accessed through the screen.orientation property.

Example:

javascript
1if ('orientation' in screen) {
2    console.log(`Current orientation: ${screen.orientation.type}`);
3} else {
4    console.log('Screen Orientation API is not supported.');
5}
  • screen.orientation.type: Returns the current orientation (portrait-primary, portrait-secondary, landscape-primary, or landscape-secondary).

Handling Orientation Change Events

You can listen for orientation changes using the orientationchange event:

Example:

javascript
window.addEventListener("orientationchange", () => {
    console.log(`New orientation: ${screen.orientation.type}`);
});

Using CSS Media Queries

CSS media queries provide a non-JavaScript method to handle orientation in styling rules.

css
1/* Portrait */
2@media (orientation: portrait) {
3    body {
4        background-color: lightblue;
5    }
6}
7
8/* Landscape */
9@media (orientation: landscape) {
10    body {
11        background-color: lightcoral;
12    }
13}

Detecting Screen Orientation on Mobile Devices

For mobile devices, the orientation can be accessed using platform-specific implementations, listed below for Android and iOS.

Android (Java)

On Android, orientation can be retrieved using the getResources().getConfiguration().orientation method.

Example:

java
1import android.content.res.Configuration;
2
3int orientation = getResources().getConfiguration().orientation;
4if (orientation == Configuration.ORIENTATION_PORTRAIT) {
5    System.out.println("Portrait mode");
6} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
7    System.out.println("Landscape mode");
8}

iOS (Swift)

In iOS, screen orientation can be accessed through the UIDevice class.

Example:

swift
1import UIKit
2
3let deviceOrientation = UIDevice.current.orientation
4switch deviceOrientation {
5    case .portrait:
6        print("Portrait mode")
7    case .landscapeLeft, .landscapeRight:
8        print("Landscape mode")
9    default:
10        print("Other orientation")
11}

Pros and Cons of Different Methods

Let's summarize the pros and cons of the approaches discussed:

MethodProsCons
Screen Orientation APIEasy to use, modern, browser-supportedLimited to compatible browsers
CSS Media QueriesSimple, non-JS basedLimited to styling only
Android getResourcesPlatform-specific, detailed controlNot cross-platform
iOS UIDevice classBuilt-in, responsiveiOS-specific, not applicable elsewhere

Conclusion

Effectively determining the current orientation is crucial for responsive design in both web and mobile applications. While the Screen Orientation API provides a straightforward approach for web development, mobile platforms offer their own specific methods. Understanding these techniques allows developers to create adaptable and user-friendly applications that function well across a variety of devices and orientations.

When building your application, consider the platform and the features you need to support to choose the right method for detecting screen orientation.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.