Composables
Docs Login
Pairel

App Bar

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.pairel.theme.ProvideContentColor
import com.composables.pairel.theme.ProvideTextStyle
import com.composables.pairel.theme.Space
import com.composables.pairel.theme.Theme

val AppBarHeight = 64.dp

@Composable
fun AppBar(
    title: @Composable () -> Unit,
    navigation: @Composable () -> Unit = {},
    actions: @Composable RowScope.() -> Unit = {},
    modifier: Modifier = Modifier,
) {
    ProvideContentColor(Theme.colors.onBackground) {
        Row(
            modifier =
                modifier
                    .fillMaxWidth()
                    .background(Theme.colors.background)
                    .pointerInput(Unit) {
                        awaitPointerEventScope { while (true) awaitPointerEvent() }
                    }
                    .windowInsetsPadding(
                        WindowInsets.statusBars.union(
                            WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal)
                        )
                    )
                    .height(AppBarHeight)
                    .padding(horizontal = 20.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Box(Modifier.offset(x = -(Space * 3))) { navigation() }
            Box(Modifier.weight(1f), contentAlignment = Alignment.CenterStart) {
                Box(Modifier.heightIn(min = 48.dp), contentAlignment = Alignment.CenterStart) {
                    ProvideTextStyle(
                        Theme.typography.title.copy(fontSize = 26.sp, lineHeight = 38.sp),
                        title,
                    )
                }
            }
            Row(verticalAlignment = Alignment.CenterVertically, content = actions)
        }
    }
}

More components from Pairel

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.