Composables
Docs Login
Pairel

Edit bio

Jetpack Compose Screen

Download
package com.composables.pairel

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.composables.pairel.components.CenteredAppBar
import com.composables.pairel.components.Icon
import com.composables.pairel.components.IconButton
import com.composables.pairel.components.ScreenScaffold
import com.composables.pairel.components.Text
import com.composables.pairel.components.TextField
import com.composables.pairel.theme.ArrowLeft
import com.composables.pairel.theme.Check
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Theme

private const val BioLimit = 300

@Composable
fun EditBioScreen(
    navigateBack: () -> Unit,
    modifier: Modifier = Modifier,
    initialBio: String = YourBio,
) {
    val bio = remember(initialBio) { TextFieldState(initialBio) }
    val length = bio.text.length

    ScreenScaffold(
        modifier = modifier,
        topBar = {
            CenteredAppBar(
                title = { Text("Bio") },
                navigation = {
                    IconButton(onClick = navigateBack) {
                        Icon(
                            Icons.ArrowLeft,
                            contentDescription = "Back",
                            modifier = Modifier.size(24.dp),
                        )
                    }
                },
                actions = {
                    IconButton(onClick = navigateBack, enabled = length in 1..BioLimit) {
                        Icon(
                            Icons.Check,
                            contentDescription = "Save bio",
                            modifier = Modifier.size(24.dp),
                        )
                    }
                },
            )
        },
    ) {
        Column(
            Modifier.fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(top = 8.dp, bottom = 24.dp)
                .windowInsetsPadding(
                    WindowInsets.navigationBars
                        .union(WindowInsets.ime)
                        .union(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal))
                )
                .padding(horizontal = 20.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Column(
                Modifier.widthIn(max = 560.dp).fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(12.dp),
            ) {
                Text(
                    "A few lines about you. Keep it light and true to you.",
                    color = Theme.colors.onBackgroundMuted,
                )
                TextField(
                    label = "Bio",
                    state = bio,
                    placeholder = "What should people know about you?",
                    singleLine = false,
                    modifier = Modifier.fillMaxWidth(),
                )
                Text(
                    "$length / $BioLimit",
                    style = Theme.typography.caption,
                    color =
                        if (length > BioLimit) Theme.colors.destructive
                        else Theme.colors.onBackgroundMuted,
                    modifier = Modifier.align(Alignment.End),
                )
            }
        }
    }
}

More screens 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.