Page·Free
Login screen (Compose)
Email and password screen in Material 3. It checks both fields when you tap Sign in, shows the error your server returns, and can add Forgot password and fingerprint buttons. You call BiometricPrompt yourself.
Free to install.
If PocketCode is installed on your Android device, this opens the page in the app.
Preview
LoginScreen.kt · 176 lines
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
/**
* Email and password sign-in screen in Material 3.
*
* - Checks the email format and a minimum password length when the user taps Sign in, then shows
* the error under the field.
* - [errorMessage] is for the server's answer ("Wrong password"); screen readers announce it.
* - [isLoading] shows a spinner in the button and blocks a second tap.
* - [onBiometricSignIn]: pass it and a "Use fingerprint or face" button appears. This file does
* not call BiometricPrompt: do that in your callback with androidx.biometric.
*
* The texts are in English inside the code; move them to strings.xml for your app.
* Only needs androidx.compose.material3.
*/
@Composable
fun LoginScreen(
onSignIn: (email: String, password: String) -> Unit,
modifier: Modifier = Modifier,
isLoading: Boolean = false,
errorMessage: String? = null,
onForgotPassword: (() -> Unit)? = null,
onBiometricSignIn: (() -> Unit)? = null,
) {
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var passwordVisible by rememberSaveable { mutableStateOf(false) }
var submitted by rememberSaveable { mutableStateOf(false) }
val focusManager = LocalFocusManager.current
val emailError = submitted && !isValidEmail(email)
val passwordError = submitted && password.length < MIN_PASSWORD_LENGTH
val submit: () -> Unit = {
submitted = true
if (isValidEmail(email) && password.length >= MIN_PASSWORD_LENGTH && !isLoading) {
focusManager.clearFocus()
onSignIn(email.trim(), password)
}
}
Column(This preview is shortened. You get the full content when you install it.
Changes in 1.0.0
First version.