Android PinView / OtpView

Additional

Language
Kotlin
Version
3.0.0 (Mar 20, 2022)
Created
Jun 22, 2016
Updated
Mar 22, 2022
Owner
Mukesh Solanki (mukeshsolanki)
Contributors
Sandeep Reddy (MSandeep96)
Mukesh Solanki (mukeshsolanki)
Andriy Antoniv (aantoniv)
William Tan (williamtan89)
DimOOOn (LisovyiDK)
Hazem Madkour (Hazem-Madkour)
6
Activity
Badge
Generate
Download
Source code
APK file

Commercial

Compose OtpView/PinView



A custom control to enter a code usually in cases of authentication.

        

Supporting Compose OtpView/PinView

Compose PinView/OtpView is an independent project with ongoing development and support made possible thanks to your donations.

How to integrate into your app?

Integrating the project is simple. All you need to do is follow the below steps

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url "https://jitpack.io" }
  }
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.mukeshsolanki:android-otpview-pinview:3.0.0'
}

How to use the library?

Okay seems like you integrated the library in your project but how do you use it? Well its really easy.

  • Using Compose Just use the OtpView composable where you need to display the view like.
....
var otpValue by remember { mutableStateOf("") }
OtpView(
    otpText = otpValue,
    onOtpTextChange = {
        otpValue = it
        Log.d("Actual Value", otpValue)
    },
    type = OTP_VIEW_TYPE_BORDER,
    password = true,
    containerSize = 48.dp,
    passwordChar = "",
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
    charColor = Color.White
)
....
  • Using Older View System (aka XML) Add a compose view in your xml file like
.....
<androidx.compose.ui.platform.ComposeView
    android:id="@+id/otp_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
.....

Next in your code assign otp_view with the OtpView composable likewise.

....
val otpView = findViewById(R.id.otp_view)
otpView.composeView.apply {
    // Dispose of the Composition when the view's LifecycleOwner is destroyed
    setViewCompositionStrategy(DisposeOnViewTreeLifecycleDestroyed)
    setContent {
       // In Compose world
       MaterialTheme {
           var otpValue by remember { mutableStateOf("") }
           OtpView(
               otpText = otpValue,
               onOtpTextChange = {
                   otpValue = it
                   Log.d("Actual Value", otpValue)
               },
               type = OTP_VIEW_TYPE_BORDER,
               password = true,
               containerSize = 48.dp,
               passwordChar = "",
               keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
               charColor = Color.White
           )
       }
    }
}
....

That's pretty much it and your all wrapped up.

Author

Maintained by Mukesh Solanki

Contribution

  • Bug reports and pull requests are welcome.

License

MIT License

Copyright (c) 2018 Mukesh Solanki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.