Android Jetpack Compose - Change the Screen Orientation Programmatically using a Button
Last Updated :
15 Sep, 2022
Many times in android applications we have to programmatically change the screen orientation to perform some task. If we want to play a video in landscape mode then we have to switch to landscape mode within our android application. So we have to change screen orientation in our android application programmatically. In this article, we will take a look at How to change screen orientation programmatically using Button in android using Jetpack Compose. A sample video is given below to get an idea about what we are going to do in this article.
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: Adding a new color to the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Kotlin
package com.example.newcanaryproject.ui.theme
import androidx.compose.ui.graphics.Color
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)
Step 3: 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.example.newcanaryproject
import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.provider.Settings.*
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import com.example.newcanaryproject.ui.theme.greenColor
class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
// on below line we are specifying
// background color for our application
Surface(
// on below line we are specifying
// modifier and color for our app
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
// on the below line we are specifying
// the theme as the scaffold.
Scaffold(
// in scaffold we are
// specifying the top bar.
topBar = {
// inside top bar we are specifying
// background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying
// title for our top bar.
title = {
// in the top bar we are specifying
// title as a text
Text(
// on below line we are specifying
// text to display in top app bar.
text = "GFG",
// on below line we are specifying
// modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// on below line we are specifying
// text alignment.
textAlign = TextAlign.Center,
// on below line we are specifying
// color for our text.
color = Color.White
)
})
}) {
// on below line we are calling our
// method to display UI
screenOrientation(LocalContext.current)
}
}
}
}
}
}
@Composable
fun screenOrientation(
context: Context,
) {
// on below line creating a
// variable for screen orientation.
val portrait = remember {
mutableStateOf(true)
}
val activity = context as Activity
// on below line we are creating a column,
Column(
// on below line we are adding a modifier to it,
modifier = Modifier
.fillMaxSize()
// on below line we are adding a padding.
.padding(all = 30.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
// on below line we are adding a text for heading.
Text(
// on below line we are specifying text
text = "Welcome to Geeks for Geeks",
// on below line we are specifying text color,
// font size and font weight
color = greenColor, fontSize = 20.sp, fontWeight = FontWeight.Bold
)
// on below line adding a spacer.
Spacer(modifier = Modifier.height(20.dp))
// on below line creating a button
Button(onClick = {
// on below line we are
// changing screen orientation
if (portrait.value) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} else {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
// opposite the value of isPortrait
portrait.value = !portrait.value
}, modifier = Modifier.width(300.dp)) {
// on below line adding a text for our button.
Text(text = "Change Screen Orientation", color = Color.White)
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
How to Change the Screen Orientation Programmatically using a Button in Android? Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a develop
3 min read
How to Check if the Battery is Charging or not in Android Programmatically using Jetpack Compose? Many times while building an android application we have to check the charging status of the battery whether the battery is currently charging or not. For checking the battery charging status within the android application. We use battery manager. In this article, we will take a look at How to use B
5 min read
Detect Screen Orientation in Android using Jetpack Compose 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 applicati
2 min read
How to Get the Device's IMEI/ESN Programmatically in Android using Jetpack Compose? Android applications many times require the unique identity of the user while developing applications. This unique identity is used to identify the user of the application. Many android apps use the IMEI number as the unique identity of each user. In this article, we will take a look at How to get D
3 min read
Increase or Decrease the Volume Programmatically in Android using Jetpack Compose Many times while building an android application such as using an audio player like a music player. We have to add functionality to add a volume controller for our media player within our application. So that we can control the volume of our media player. In this article, we will take a look at How
6 min read
Switch Button in Android using Jetpack Compose A Switch or a Switch Button in Android is a UI element that is used to switch between two states upon click. It can be assumed as a Boolean button with two different values. Some states where you may find a Switch in your Android device can be WIFI ON and OFF, Bluetooth ON and OFF, Dark Mode and Lig
2 min read
Icon Toggle Button in Android using Jetpack Compose Toggle Buttons are used in most applications. These buttons are used to perform multiple operations. Toggle buttons are seen used in many social media applications such as Instagram. In social media apps, we can get to see a heart icon that is used to like the image. We can also unlike that image by
3 min read
Change Button Size in Android Jetpack Compose In Android, Button is a very common UI element that is used to call a function or perform a task when clicked by the user. A Button by default occupies specific space depending upon the text that it displays. If the Button text is longer or has a larger font size, the Button width, and height increa
3 min read
How to Open an External URL on Button Click in Android using Jetpack compose? Many times in android applications we have to open a web page from an URL inside the default browser of the user's device. In that case, we have to simply pass that URL to the web browser to load that page. In this article, we will take a look at How to open a URL in an android web browser from our
5 min read
Material Design Buttons using Jetpack Compose in Android Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material desig
4 min read