Composables
Docs Login
Pairel

Text Field

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.InputTransformation
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.SolidColor
import com.composables.pairel.theme.Space
import com.composables.pairel.theme.Theme
import com.composeunstyled.TextInput
import com.composeunstyled.UnstyledTextField

@Composable
fun TextField(
    label: String,
    state: TextFieldState,
    modifier: Modifier = Modifier,
    placeholder: String = "",
    enabled: Boolean = true,
    singleLine: Boolean = true,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    inputTransformation: InputTransformation? = null,
) {
    UnstyledTextField(
        state = state,
        modifier = modifier,
        enabled = enabled,
        keyboardOptions = keyboardOptions,
        inputTransformation = inputTransformation,
        accessibilityLabel = label,
        lineLimits =
            if (singleLine) TextFieldLineLimits.SingleLine
            else TextFieldLineLimits.MultiLine(minHeightInLines = 3),
        cursorBrush = SolidColor(Theme.colors.accent),
        textStyle = Theme.typography.body.copy(color = Theme.colors.onSurface),
    ) {
        Column {
            Text(
                label,
                color = Theme.colors.onBackgroundMuted,
                style = Theme.typography.caption,
                modifier = Modifier.padding(bottom = Space * 2),
            )
            TextInput(
                modifier =
                    Modifier.fillMaxWidth()
                        .clip(Theme.shapes.small)
                        .background(Theme.colors.surfaceVariant)
                        .padding(horizontal = Space * 4, vertical = Space * 3),
                placeholder = { Text(placeholder, color = Theme.colors.onBackgroundMuted) },
            )
        }
    }
}

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.