Skip to content
Page·Free

Settings screen (Compose)

Material 3 settings screen with switches, a theme choice and a text size slider. It holds no state: you pass a SettingsState in and get the changed copy back. Each row is one touch target of 56 dp or more that TalkBack reads as a switch or radio button.

Free to install.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

SettingsScreen.kt · 190 lines
// Settings screen for Jetpack Compose and Material 3, no extra libraries.
// It holds no state of its own: keep SettingsState in your ViewModel and save it where you like.
//
// val state by viewModel.settings.collectAsState()
// SettingsScreen(state = state, onStateChange = viewModel::update, onAboutClick = { /* open About */ }, appVersion = "1.4.2")
//
// Each row is one touch target of at least 56 dp, and TalkBack reads it as a switch or a radio button.
// Move the texts to strings.xml before you translate the app.

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.selection.toggleable
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

enum class ThemeChoice(val label: String) {
    System("Use the system setting"),
    Light("Light"),
    Dark("Dark"),
}

data class SettingsState(
    val pushNotifications: Boolean = true,
    val weeklySummary: Boolean = false,
    val theme: ThemeChoice = ThemeChoice.System,
    /** Multiplier for your app's text size, from 0.8 to 1.4. */
    val textScale: Float = 1f,
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
    state: SettingsState,
    onStateChange: (SettingsState) -> Unit,
    onAboutClick: () -> Unit,
    modifier: Modifier = Modifier,
    appVersion: String = "",
) {
    Scaffold(
        modifier = modifier,
        topBar = { TopAppBar(title = { Text("Settings") }) },
    ) { padding ->
        Column(
            Modifier
                .fillMaxSize()
                .padding(padding)
                .verticalScroll(rememberScrollState()),
        ) {
            SectionHeader("Notifications")
            SwitchRow(
                title = "Push notifications",

This preview is shortened. You get the full content when you install it.

Changes in 1.0.0

First version.

Related