Skip to content

Commit

Permalink
OTPView implemented with three types and updated readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
priyal-p-simformsolutions committed Jul 22, 2022
1 parent 4005621 commit 158cc5d
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 38 deletions.
120 changes: 119 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,119 @@
# SSComposeOTPPinView
# SSComposeOTPPinView

[![Android-Studio](https://img.shields.io/badge/Android%20Studio-Chipmunk-orange.svg?style=flat)](https://developer.android.com/studio/)
[![Kotlin Version](https://img.shields.io/badge/Kotlin-1.7.0-blue.svg)](https://kotlinlang.org)
[![Platform](https://img.shields.io/badge/Platform-Android-green.svg?style=flat)](https://www.android.com/)

A custom view to enter a code usually used in authentication.
Different type of OTPViews. Easy to use and configure your own view and character of OTP using all the attributes.

# Features
* Simple OTPView
* Box OTPView
* Underline OTPView
* We can manage our own OTPCount
* We can manage the OTP character color
* We can manage OTP character size
* We can manage the border color of OTPView as well
* We can enabled and disabled the visibility of OTP characters
* We can customize your OTP characters using attributes
* If we entered the incorrect length of OTP then button will not be clickable

# 🎬 Preview

| Simple OTPView with dot character | Box OTPView with dot character |
|--|--|
| ![](gifs/simple_dot.gif) | ![](gifs/box_dot.gif) |

| Underline OTPView with dot character | Simple OTPView with star character |
|--|--|
![](gifs/underline_dot.gif) | ![](gifs/simple_star.gif) |

| Box OTPView with star character | Underline OTPView with star character |
|--|--|
![](gifs/box_star.gif) | ![](gifs/underline_star.gif) |

## How to use the library?
- Using Compose
Just use the `OtpView` composable function where you need to display the view
```kotlin
OtpView(
otpText = otpValue,
onOtpTextChange = {
otpValue = it
},
type = OTP_VIEW_TYPE_NONE,
password = true,
containerSize = 48.dp,
passwordChar = "",
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
charColor = Color.White
)
```
Next in your code create `OtpView` composable function like below:
```kotlin
fun OtpView(
modifier: Modifier = Modifier,
otpText: String = "",
charColor: Color = Color.Black,
charBackground: Color = Color.Transparent,
charSize: TextUnit = 16.sp,
strokeColor: Color = Color.Black,
containerSize: Dp = charSize.value.dp * 2,
otpCount: Int = 4,
type: Int = OTP_VIEW_TYPE_UNDERLINE,
enabled: Boolean = true,
password: Boolean = false,
passwordChar: String = "",
keyboardOptions: KeyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onOtpTextChange: (String) -> Unit
)
```

### All Attributes
------------------------

| Attribute | Description |
| --- | --- |
| `charColor` | To change otp character color. |
| `charSize` | Set custom character size. |
| `containerSize` | To change the size of otp character container. |
| `otpCount` | Set the custom range of otp characters. |
| `password` | Show/hide the otp characters. |
| `passwordChar` | Set the custom character for otp password. |
| `strokecolor` | Set the custom color to container border. |
| `type` | For customization we have created three types: </br>1. OTP_VIEW_TYPE_NONE </br>2. OTP_VIEW_TYPE_UNDERLINE </br>3. OTP_VIEW_TYPE_BOX |
| `keyboardOptions` | Set the custom keyboard which you need. |

## Official Documentations
- [Jetpack Compose](https://developer.android.com/jetpack/compose)
- [Jetpack Compose Pathways](https://developer.android.com/courses/pathways/compose)

## Find this samples useful? ❤️
Support it by joining __[stargazers](https://github.com/SimformSolutionsPvtLtd/SSComposeOTPPinView/stargazers)__ for this repository.⭐

## How to Contribute🤝

Whether you're helping us fix bugs, improve the docs, or a feature request, we'd love to have you! 💪
Check out our __[Contributing Guide](https://github.com/SimformSolutionsPvtLtd/SSComposeOTPPinView/blob/main/CONTRIBUTING.md)__ for ideas on contributing.

## Bugs and Feedback
For bugs, feature requests, and discussion please use [GitHub Issues](https://github.com/SimformSolutionsPvtLtd/SSComposeOTPPinView/issues).

## License

```
Copyright 2022 Simform Solutions
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
84 changes: 68 additions & 16 deletions app/src/main/java/com/example/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.example.myapplication

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
Expand All @@ -19,22 +33,60 @@ class MainActivity : ComponentActivity() {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
Greeting("Android")
var otpValue by remember { mutableStateOf("") }
val otpCount by remember { mutableStateOf(4) }
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
) {
TopAppBar(
title = { Text(text = getString(R.string.underline_otpview_with_star_char), color = Color.White) },
)
Text(
modifier = Modifier.padding(
start = 0.dp,
top = 40.dp,
end = 0.dp,
bottom = 0.dp
),
text = getString(R.string.title),
fontWeight = FontWeight.Bold,
color = Color.Blue,
)
Text(
modifier = Modifier.padding(32.dp),
textAlign = TextAlign.Center,
text = getString(R.string.instruction_title),
color = Color.Blue,
)
OtpView(
otpText = otpValue,
onOtpTextChange = {
otpValue = it
},
type = OTP_VIEW_TYPE_UNDERLINE,
otpCount = otpCount,
password = true,
containerSize = 48.dp,
passwordChar = "*",
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
strokeColor = Color.Blue,
charColor = Color.Blue
)
Button(
onClick = {
Toast.makeText(this@MainActivity, getString(R.string.otp_correct), Toast.LENGTH_SHORT)
.show()
},
modifier = Modifier.padding(32.dp),
enabled = otpValue.length == otpCount,
) {
Text(text = getString(R.string.validate))
}
}
}
}
}
}
}

@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MyApplicationTheme {
Greeting("Android")
}
}
145 changes: 145 additions & 0 deletions app/src/main/java/com/example/myapplication/OtpView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.example.myapplication

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

const val OTP_VIEW_TYPE_NONE = 0
const val OTP_VIEW_TYPE_UNDERLINE = 1
const val OTP_VIEW_TYPE_BOX = 2

/**
* create the otpview.
*/
@Preview
@Composable
fun OtpView(
modifier: Modifier = Modifier,
otpText: String = "",
charColor: Color = Color.Black,
charBackground: Color = Color.Transparent,
charSize: TextUnit = 16.sp,
strokeColor: Color = Color.Black,
containerSize: Dp = charSize.value.dp * 2,
otpCount: Int = 4,
type: Int = OTP_VIEW_TYPE_UNDERLINE,
enabled: Boolean = true,
password: Boolean = false,
passwordChar: String = "",
keyboardOptions: KeyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onOtpTextChange: (String) -> Unit
) {
BasicTextField(
modifier = modifier,
value = otpText,
onValueChange = {
if (it.length <= otpCount) {
onOtpTextChange.invoke(it)
}
},
enabled = enabled,
keyboardOptions = keyboardOptions,
decorationBox = {
Row(horizontalArrangement = Arrangement.SpaceAround) {
repeat(otpCount) { index ->
Spacer(modifier = Modifier.width(2.dp))
CharView(
index = index,
text = otpText,
charColor = charColor,
strokeColor = strokeColor,
charSize = charSize,
containerSize = containerSize,
type = type,
charBackground = charBackground,
password = password,
passwordChar = passwordChar,
)
Spacer(modifier = Modifier.width(2.dp))
}
}
})
}

/**
* Set the view of otp character.
*/
@Composable
private fun CharView(
index: Int,
text: String,
charColor: Color,
strokeColor: Color,
charSize: TextUnit,
containerSize: Dp,
type: Int = OTP_VIEW_TYPE_UNDERLINE,
charBackground: Color = Color.Transparent,
password: Boolean = false,
passwordChar: String = ""
) {
val modifier = if (type == OTP_VIEW_TYPE_BOX) {
Modifier
.size(containerSize)
.border(
width = 1.dp,
color = strokeColor,
shape = MaterialTheme.shapes.medium
)
.padding(bottom = 4.dp)
.background(charBackground)
} else Modifier
.width(containerSize)
.background(charBackground)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
val char = when {
index >= text.length -> ""
password -> passwordChar
else -> text[index].toString()
}
Text(
text = char,
color = charColor,
modifier = modifier.wrapContentHeight(),
style = MaterialTheme.typography.body1,
fontSize = charSize,
textAlign = TextAlign.Center,
)
if (type == OTP_VIEW_TYPE_UNDERLINE) {
Spacer(modifier = Modifier.height(2.dp))
Box(
modifier = Modifier
.background(strokeColor)
.height(1.dp)
.width(containerSize)
)
}
}
}

9 changes: 0 additions & 9 deletions app/src/main/java/com/example/myapplication/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ private val LightColorPalette = lightColors(
primary = Purple500,
primaryVariant = Purple700,
secondary = Teal200

/* Other default colors to override
background = Color.White,
surface = Color.White,
onPrimary = Color.White,
onSecondary = Color.Black,
onBackground = Color.Black,
onSurface = Color.Black,
*/
)

@Composable
Expand Down
Loading

0 comments on commit 158cc5d

Please sign in to comment.