The code defines a simple app that displays the current screen orientation ("portrait" or "landscape") on the screen.
The app uses the following Jetpack Compose components:
Scaffold
: This is the top-level layout for the app. It provides a default app bar, content area, and floating action button (FAB).TopAppBar
: This is the app bar at the top of the screen. It can display a title, navigation icon, and actions.Box
: This is a layout element that can contain other composables. It fills the available space and positions its children according to the specified alignment.Text
: This is a composable that displays text. It can be styled using theTextStyle
modifier.
The MainContent
composable is responsible for displaying the screen orientation. It retrieves the current screen orientation using the LocalConfiguration.current
composable and stores it in a mutable state variable called orientation
. The orientation
variable is then used to display the text "Screen Orientation\n$orientation" on the screen.
Here is a breakdown of the code:
The
MainActivity
class is the main activity for the app. It sets the content of the app using thesetContent
function. The content is defined by theGetScaffold
composable.The
GetScaffold
composable defines the layout of the app. It uses aScaffold
composable to provide a top app bar, content area, and background color.The
MainContent
composable is the content of the app. It retrieves the current screen orientation and displays it on the screen.
In summary, this code demonstrates how to use Jetpack Compose to detect screen orientation changes and display the current orientation on the screen.
package com.cfsuman.jetpackcompose
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {GetScaffold()}
}
@Composable
fun GetScaffold() {
Scaffold(
topBar = {TopAppBar(
title = {Text(text = "Compose - Detect Orientation Change")},
backgroundColor = Color(0xFF592720),
contentColor = Color.White
)},
content = { MainContent() },
backgroundColor = Color(0xFFEEDC82)
)
}
@Composable
fun MainContent() {
val configuration = LocalConfiguration.current
val orientation by remember { mutableStateOf(
when (configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> {
"Landscape"
}
else -> {
"Portrait"
}
}
)}
Box(Modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
Text(
text = "Screen Orientation\n$orientation",
style = TextStyle(
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center
)
)
}
}
}
- jetpack compose - How to hide status bar
- jetpack compose - How to hide navigation bar
- jetpack compose - How to hide system bars
- jetpack compose ktor - How to get api data
- jetpack compose - Get text from url
- jetpack compose ktor - How to pass parameters
- jetpack compose - Kotlinx serialization ignore unknown keys
- jetpack compose - Kotlinx serialization alternative json names
- jetpack compose - Kotlinx serialization decode from string
- jetpack compose - Kotlinx serialization encode defaults
- jetpack compose - Kotlinx serialization build json array
- jetpack compose - Kotlinx serialization build json object
- jetpack compose - Flow current time
- jetpack compose - How to flow a list
- jetpack compose - How to use ViewModel state