OTPView is a view made in Jetpack compose. It is highly customisable and can be used to show OTP view with different length and shapes.

Overview

OTPView

OTPView is a highly costumizable OTP view made in the Jetpack compose UI.

Usage:

CircleOtpView is a sample composable that calls the OtpView which actually draws the view.

@Composable
fun CircleOtpView() {
    var otpStr by remember {
        mutableStateOf("")
    }
    var isError by remember {
        mutableStateOf(false)
    }
    OtpView(
        modifier = Modifier.width(240.dp),
        otpStr = otpStr,
        isError = isError,
        shape = PinShape.CircleShape,
        numberOfFields = 4
    ) { otp ->
        otpStr = otp
        if (otpStr.length == 4) {
            // Completely filled
            isError = otpStr != "1234"
            if (isError) {
                otpStr = ""
            }
        }

        if (isError && otpStr.isNotEmpty()) {
            isError = false
        }
    }
}

PinShape

Different shapes can be created using PinShape, which is an interface and contains only one method drawShape(size: Size): Path. Implementor class has to implement this method and return a Path which needs to be drawn with the given size in the method param.

class LineShape(private val strokeWidth: Float): PinShape {
      override fun drawShape(size: Size): Path {
         return Path().apply {
             reset()
             addRect(Rect(0f, size.height - (strokeWidth / 2), size.width, size.height))
             close()
         }
      }
}

PinShape is passed as a parameter in the OtpView composable and it is used in the PinField Modifier's drawBehind {}.

@Composable
fun PinField(
    modifier = Modifier,
    shape: PinShape,
    .
    .
    .
) {
    Box(
        modifier = modifier
            .width(width)
            .fillMaxHeight()
            .drawBehind {
                drawPath(
                    path = shape.drawShape(this.size),
                    .
                    .
                )
            }
            .clickable {
                onClick(index)
            },
    ) {
        ....
    }
}

Courtesy:

  1. Idea of using single TextField and displaying different view for the pins: https://github.com/ch8n/Compose-OtpView

  2. Custom shapes https://juliensalvi.medium.com/custom-shape-with-jetpack-compose-1cb48a991d42

You might also like...
Formula 1 app to show current Standings from a REST API.Built using jetpack compose and kotlin.
Formula 1 app to show current Standings from a REST API.Built using jetpack compose and kotlin.

Formula1 Formula 1 app to show Standings fetching data from REST API ๐Ÿ“ธ Screenshots Tech Stack. Kotlin - Kotlin is a programming language that can run

Jetpack Compose Text composable to show html text from resources
Jetpack Compose Text composable to show html text from resources

HtmlText Current Compose Version: 1.0.3 Compose HtmlText Text composable to show html text from resources Add to your project Add actual HtmlText libr

A Clean Architecture App to show use of multi module architecture in a Jetpack Compose
A Clean Architecture App to show use of multi module architecture in a Jetpack Compose

Nextflix-Composable A Clean Architecture App to show use of multi-module-archite

Icontent - Jetpack Compose component to show all random content sended by Inmersoft guide backend

icontent IContent is a library that allows loading audio visual content generica

Stock Ticker / Watchlist App being used as an excuse to learn Compose and other Jetpack libs.

StockTicker App Simple app that displays a few FAANG stock tickers and allows the user to search for and add different tickers to a watchlist. API Key

A Collection of major Jetpack compose UI components which are commonly used.๐ŸŽ‰๐Ÿ”๐Ÿ‘Œ
A Collection of major Jetpack compose UI components which are commonly used.๐ŸŽ‰๐Ÿ”๐Ÿ‘Œ

SSComposeCookBook A Collection of major Jetpack compose UI components which are commonly used. Introduction Jetpack Compose is a modern toolkit for bu

A Jetpack Compose component used for displaying Markdown-formatted text.
A Jetpack Compose component used for displaying Markdown-formatted text.

MarkdownText A library for displaying Markdown contents within Jetpack Compose. Uses Coil Current limitations Lists that are annotated with the * must

Resume of Louis CAD, made with Jetpack Compose. Supports the Web, Desktop, and Android.
Resume of Louis CAD, made with Jetpack Compose. Supports the Web, Desktop, and Android.

ResumeComposition What This project is the source code of the resume/CV of Louis CAD. The latest PDF export is available here, it is ready to print. H

New style for app design and Movies App with Movies API JetMaxMovies made in Jetpack Compose.๐Ÿ˜‰๐Ÿ˜Ž
New style for app design and Movies App with Movies API JetMaxMovies made in Jetpack Compose.๐Ÿ˜‰๐Ÿ˜Ž

JetMaxMovie New style for app design and Movies App with Movies API JetMaxMovies made in Jetpack Compose. ๐Ÿ˜‰ ๐Ÿ˜Ž (Navigation Compose,Dagger-Hilt, Mater

Owner
kunalsale
kunalsale
A library that you can use in 4 different types toast written with Jetpack Compose

Composable Sweet Toast A library that you can use in 4 different types(Success, Error, Warning, Info) written with Jetpack Compose. You can use this t

Talha FakฤฑoฤŸlu 67 Dec 19, 2022
A library that you can use in 4 different types(Success, Error, Warning, Info) written with Jetpack Compose.

Composable Sweet Toast A library that you can use in 4 different types(Success, Error, Warning, Info) written with Jetpack Compose. You can use this t

Talha FakฤฑoฤŸlu 67 Dec 19, 2022
This app can be used to track open slots for vaccination. This is built using Jetpack Compose.

Covid Vaccine This projects used the Cowin APIs provided by the government. Screenshots Tech Stack Used Kotlin Dagger Hilt Clean Architecture with Mod

Himanshu Singh 101 Dec 15, 2022
Lull is an Android mobile application made with Jetpack Compose, where you can download wallpapers.

Lull Lull is an Android mobile application made with Jetpack Compose, where you can download wallpapers. Table of Contents Features Screenshots Light

Simge ลžengรผn 12 Oct 10, 2022
๐Ÿ’ป A cross-platform desktop application to identify libraries used inside an android application. Made possible by Compose Desktop โšก

?? stackzy A desktop app to analyse APK. Built using Compose desktop โœจ Demo Watch demo ??๏ธ Usage Show usage ?? Install Platform Download Status Linux

theapache64 876 Dec 24, 2022
Highly experimental routing feature for Compose Web

RoutingCompose Highly experimental routing feature for Compose Web Install This package is uploaded to GitHub Packages. repositories { maven {

Philip Wedemann 86 Dec 20, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023
Jetpack Compose based project, used to stress-testing compose features / integrations and explore non-trivial functionality

Project containing Jetpack Compose samples For pagination & network images it uses CATAAS. Known issues Navigation-Compose Issue with fast tapping on

Denis Rudenko 59 Dec 14, 2022
Compose Curved-Scroll is an Android Jetpack Compose library made with โค๏ธ

Compose-Curved-Scroll-library Compose Curved-Scroll is an Android Jetpack Compos

mohamed tamer 23 Aug 24, 2022
An android library to highlight different features of the app built using Jetpack Compose.

An android library to highlight different features of the app built using Jetpack Compose.

Canopas Software 230 Dec 23, 2022