Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to get status bar height in compose?

Usually when using Accompanist Modifier.statusBarsHeight() the height will change depends on the status bar visibility, if it's visible either 24.dp or more and if it's invisible the height will be 0.dp. But i want the height won't change to zero regardless of its visibility.

I've been using this for a while:

// TODO: use better solution to get a fixed status bar height
val statusBarHeight = with (LocalDensity.current) { LocalWindowInsets.current.statusBars.top.toDp() }
val fixedStatusBarHeight = remember { statusBarHeight }
like image 666
uragiristereo Avatar asked Nov 23 '25 21:11

uragiristereo


2 Answers

The problem is getting complicated you should use it like this

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .statusBarsPadding().navigationBarsPadding()
            ) {

            }

            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .statusBarsPadding().systemBarsPadding()
            ) {

            }
        }
    }
}
like image 123
Đốc.tc Avatar answered Nov 26 '25 11:11

Đốc.tc


You can use WindowInsets.statusBars.getTop(this)

For example (map takes all screen - behind status and navigation bar):

GoogleMap(
    modifier = Modifier.fillMaxSize(),
    cameraPositionState = cameraPositionState,
    properties = MapProperties(isMyLocationEnabled = true),
    contentPadding = with(LocalDensity.current) {
        PaddingValues(
            top = WindowInsets.statusBars.getTop(this).toDp(),
            bottom = WindowInsets.navigationBars.getBottom(this).toDp()
        )
    }
) {

Result:

enter image description here

like image 45
user924 Avatar answered Nov 26 '25 12:11

user924