How to determine the orientation of an Android device

Oscar de la Hera Gomez
Four flowers that represent Kotlin, Android, Jetpack Compose and Android Studio side by side. Beneath them sits the text “Orientation.”

A step by step guide to determining the orientation of an Activity or Composable Android device using Kotlin and Jetpack Compose.

Step One: Create the Utility

A screenshot of Android Studio showing the getOrientation.kt file with the code that's provided below.

In the Android Studio project, create a file called getOrientation.kt and paste in the code below.

Please note that there are two functions as one can be called anywhere whilst the other can only be called within a Composable.

Step Two: Determine the Orientation

A screenshot of Android Studio showing how we use the utility created in the previous step within an Activity.

Apply the function in your project and determine the orientation using code similar to the sample code provided below.

Step Three: Test

A screenshot of Android Studio showing the device in Landscape with the Logs showing that changes in orientation have occurred. The last log reads Landscape Right which matches the phone orientation.

Run the app and rotate the device or emulator and witness how the logs show the orientation of the device.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details