Open In App

Detect Screen Orientation in Android using Jetpack Compose

Last Updated : 01 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, Screen Orientation is an important aspect where the user would like to run an activity in Fullscreen or a Wide Landscaped mode. Most commonly running applications that can switch between portrait and landscape mode can be Image Viewers, Video Players, Web Browsers, etc. In such applications, it is important to detect screen orientation to display content likewise.

Screen Orientation in Android

 

So in this article, we will show you how you could detect Screen Orientation in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcscreenorientation
  
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.tooling.preview.Preview
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            
            // Creating a Scaffold Layout
            Scaffold(
                
                   // Creating a Top Bar
                   topBar = { TopAppBar(title = { Text("GFG | Screen Orientation", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
                   content = {
                    
                    // Creating a Column to display Text
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                           // Fetching current app configuration
                           val configuration = LocalConfiguration.current
                        
                        // When orientation is Landscape
                        when (configuration.orientation) {
                            Configuration.ORIENTATION_LANDSCAPE -> {
                                Text("Landscape")
                            }
                              
                        // Other wise
                        else -> {
                                Text("Portrait")
                            }
                        }
                    }
                }
            )
        }
    }
}


Output:

In the below recording of the application, you can see that the Text correctly displays the type of orientation.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads