MainActivity.kt
package com.cfsuman.jetpackcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
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.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 - Get Screen Orientation")},
backgroundColor = Color(0xFF7CB9E8),
)},
content = { MainContent() },
backgroundColor = Color(0xFFF0FFFF)
)
}
@Composable
fun MainContent() {
val configuration = LocalConfiguration.current
val orientation = configuration.orientation
Box(Modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
Text(
text = "Screen orientation" +
"\n${getOrientationName(orientation)}",
fontSize = 30.sp,
textAlign = TextAlign.Center
)
}
}
}
fun getOrientationName(value:Int):String{
return when (value) {
0->{
"Undefined"
}
1 -> {
"Portrait"
}
2 -> {
"Landscape"
}
else -> {
"Error!"
}
}
}
- jetpack compose - How to use TabRow
- jetpack compose - TabRow indicator color
- jetpack compose - TabRow custon indicator
- jetpack compose - Get primary language
- jetpack compose - How to change StatusBar color
- jetpack compose - How to change NavigationBar color
- jetpack compose - How to change SystemBars color
- jetpack compose - How to hide system bars
- jetpack compose - Detect screen orientation change
- jetpack compose ktor - How to get api data
- jetpack compose ktor - How to post data
- jetpack compose - Kotlinx serialization pretty print
- jetpack compose - Kotlinx serialization lenient parsing
- jetpack compose - Kotlinx serialization not encode null values
- jetpack compose - Kotlinx serialization encode to string