Auto Read OTP

Jetpack Compose building blocks for one-time code screens: an OTP input with a cell per digit, automatic SMS reading through Google's SMS User Consent API, and a countdown timer for code expiry.
A real SMS sent to an emulator. The user taps Allow once, and the code fills itself in.
See seven designs built with it ↓
| Artifact | What it gives you |
|---|
otp-ui | OtpInputField with default, outlined and underline cells, masking, autofill and screen reader support, plus CountdownTimer, a HH : MM : SS countdown that hides itself when time is up |
otp-sms | SmsUserConsentEffect, which reads the code from the next SMS after the user agrees. No SMS permission needed |
otp-bom | Keeps both on matching versions |
API reference: burkido.github.io/auto-read-otp
Installation
The libraries are on Maven Central, so no extra repository is needed. They require minSdk 24, compileSdk 37 and Jetpack Compose.
In gradle/libs.versions.toml:
[versions]
otp = "3.0.0"
[libraries]
otp-bom = { module = "io.github.burkido:otp-bom", version.ref = "otp" }
otp-ui = { module = "io.github.burkido:otp-ui" }
otp-sms = { module = "io.github.burkido:otp-sms" }
In your module's build.gradle.kts, add only the parts you use:
dependencies {
implementation(platform(libs.otp.bom))
implementation(libs.otp.ui)
implementation(libs.otp.sms)
}
Without a version catalog:
dependencies {
implementation(platform("io.github.burkido:otp-bom:3.0.0"))
implementation("io.github.burkido:otp-ui")
implementation("io.github.burkido:otp-sms")
}
Coming from 2.0.0? otp-input and otp-timer are now one artifact, otp-ui. Coming from 1.x? Coordinates, packages and several APIs changed. The migration guide covers both.
A complete verification screen
@Composable
fun VerifyScreen(onVerify: (String) -> Unit, onResend: () -> Unit) {
val code = rememberTextFieldState()
var attempt by rememberSaveable { mutableIntStateOf(0) }
var canResend by rememberSaveable { mutableStateOf(false) }
var expiresAt by rememberSaveable { mutableLongStateOf(System.currentTimeMillis() + 2.minutes.inWholeMilliseconds) }
val timer = rememberCountdownTimerState(endTimeMillis = expiresAt)
OtpInputField(
state = code,
otpLength = 6,
onComplete = onVerify,
)
SmsUserConsentEffect(
codeLength = 6,
onCodeReceive = { received -> code.fillOtp(received, 6) },
onError = { error -> Log.w("Verify", "No code from SMS: $error") },
restartKey = attempt,
)
CountdownTimer(state = timer, onTimerFinish = { canResend = true })
TextButton(
enabled = canResend,
onClick = {
code.clearOtp()
attempt++
expiresAt = System.currentTimeMillis() + 2.minutes.inWholeMilliseconds
canResend = false
onResend()
},
) {
Text("Resend code")
}
}
Sample app
The sample module shows each part of the library on its own and all of them working together. It's the quickest way to see what the library can do before you write any code.
 |  |  |  |
| Home | Sign-in flow | Playground | SMS lab |