Skip to content

Commit e961241

Browse files
committed
Add code size and code length
1 parent 5163910 commit e961241

File tree

7 files changed

+69
-26
lines changed

7 files changed

+69
-26
lines changed

app/src/main/java/com/otp/code/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.otp.code
22

3+
import android.annotation.SuppressLint
34
import android.content.Intent
45
import androidx.appcompat.app.AppCompatActivity
56
import android.os.Bundle
@@ -37,6 +38,7 @@ class MainActivity : AppCompatActivity() {
3738
}
3839
}
3940

41+
@SuppressLint("SetTextI18n")
4042
override fun onStart() {
4143
super.onStart()
4244
job = CoroutineScope(Dispatchers.Main).launch {

app/src/main/java/com/otp/code/WhiteActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.otp.code
22

3+
import android.annotation.SuppressLint
34
import android.content.Intent
45
import androidx.appcompat.app.AppCompatActivity
56
import android.os.Bundle
@@ -36,11 +37,12 @@ class WhiteActivity : AppCompatActivity() {
3637
binding.btnChangeBg.setOnClickListener { finish() }
3738
}
3839

40+
@SuppressLint("SetTextI18n")
3941
override fun onStart() {
4042
super.onStart()
4143
job = CoroutineScope(Dispatchers.Main).launch {
4244
while (true) {
43-
currentCode = Random.nextInt(999..9999).toString()
45+
currentCode = Random.nextInt(9999999..99999999).toString()
4446
binding.tvCode.text = "Code: $currentCode"
4547
delay(60_000)
4648
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
android:id="@+id/verify_code"
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"
25+
android:layout_marginHorizontal="16dp"
2526
android:layout_marginTop="100dp"
2627
app:layout_constraintTop_toBottomOf="@id/tv_code" />
2728

app/src/main/res/layout/white_activity.xml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
android:id="@+id/verify_code"
2222
android:layout_width="match_parent"
2323
android:layout_height="wrap_content"
24+
android:layout_marginHorizontal="16dp"
25+
android:layout_marginTop="100dp"
26+
app:code_background_color="#ECE6E1"
2427
app:code_color="#CC171717"
25-
app:code_stroke_color="#185C1F"
26-
app:code_done_stroke_color="#1AA327"
27-
app:code_background_color="#667A7979"
28+
app:code_done_stroke_color="#0CBA00"
2829
app:code_error_color="#A50202"
29-
app:code_stroke_error_color="#F11919"
30-
app:code_length="4"
30+
app:code_length="14"
3131
app:code_radius="10dp"
32+
app:code_stroke_color="#990018B6"
33+
app:code_stroke_error_color="#F11919"
3234
app:code_stroke_width="2dp"
33-
android:layout_marginTop="100dp"
35+
app:code_text_size="18sp"
3436
app:layout_constraintTop_toBottomOf="@id/tv_code" />
3537

3638
<Button

otp_code_view/src/main/java/com/otp/otp_code_view/OtpCodeView.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.drawable.GradientDrawable
77
import android.text.InputFilter
88
import android.util.AttributeSet
99
import android.util.DisplayMetrics
10+
import android.util.TypedValue
1011
import android.view.LayoutInflater
1112
import android.view.View
1213
import android.view.inputmethod.InputMethodManager
@@ -16,6 +17,7 @@ import android.widget.TextView
1617
import androidx.core.content.ContextCompat
1718
import androidx.core.widget.doOnTextChanged
1819
import com.otp.otp_code_view.databinding.VerifyOtpCodeViewBinding
20+
import kotlin.math.min
1921

2022
class OtpCodeView @JvmOverloads constructor(
2123
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
@@ -31,27 +33,22 @@ class OtpCodeView @JvmOverloads constructor(
3133
private var codeErrorColor: Int
3234
private var codeDoneStrokeColor: Int
3335
private var codeRadius: Float
36+
private var codeTextSize: Float
3437

3538
init {
3639
binding = VerifyOtpCodeViewBinding.inflate(LayoutInflater.from(context))
3740
addView(binding.root)
3841
val attributes = context.obtainStyledAttributes(
39-
attrs,
40-
R.styleable.OtpCodeView,
41-
defStyleAttr,
42-
defStyleAttr
42+
attrs, R.styleable.OtpCodeView, defStyleAttr, defStyleAttr
4343
)
4444
codeLength = attributes.getInteger(
45-
R.styleable.OtpCodeView_code_length,
46-
6
47-
)
45+
R.styleable.OtpCodeView_code_length, 6
46+
).let { maxOf(min(it, 8), 1) }
4847
codeStrokeWidth = attributes.getDimension(
49-
R.styleable.OtpCodeView_code_stroke_width,
50-
1 * ratioDpToPixels
48+
R.styleable.OtpCodeView_code_stroke_width, 1 * ratioDpToPixels
5149
)
5250
codeRadius = attributes.getDimension(
53-
R.styleable.OtpCodeView_code_radius,
54-
12 * ratioDpToPixels
51+
R.styleable.OtpCodeView_code_radius, 12 * ratioDpToPixels
5552
)
5653
codeColor = attributes.getColor(
5754
R.styleable.OtpCodeView_code_color,
@@ -77,6 +74,9 @@ class OtpCodeView @JvmOverloads constructor(
7774
R.styleable.OtpCodeView_code_done_stroke_color,
7875
ContextCompat.getColor(context, R.color.color_fec70a)
7976
)
77+
codeTextSize = attributes.getDimension(
78+
R.styleable.OtpCodeView_code_text_size, 24f * 6 * ratioSpToPixels / codeLength
79+
)
8080
attributes.recycle()
8181
handleInputCode()
8282
}
@@ -90,6 +90,7 @@ class OtpCodeView @JvmOverloads constructor(
9090
mainEditText.doOnTextChanged { text, _, _, _ -> handleShowCode(text, listTextView) }
9191
listTextView.forEachIndexed { index, textview ->
9292
if (index >= codeLength) textview.visibility = View.GONE
93+
textview.setTextSize(TypedValue.COMPLEX_UNIT_PX, codeTextSize)
9394
textview.setTextColor(codeColor)
9495
textview.background = getDrawable(codeStrokeColor, false)
9596
}
@@ -171,12 +172,14 @@ class OtpCodeView @JvmOverloads constructor(
171172
}
172173
}
173174

175+
174176
companion object {
175177
@JvmField
176178
val ratioDpToPixels =
177179
Resources.getSystem().displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT
178180

179181
@JvmField
180-
val ratioPixelsToDp: Float = (1.0 / ratioDpToPixels.toDouble()).toFloat()
182+
@Suppress("DEPRECATION")
183+
val ratioSpToPixels = Resources.getSystem().displayMetrics.scaledDensity
181184
}
182185
}

otp_code_view/src/main/res/layout/verify_otp_code_view.xml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="wrap_content"
67
android:orientation="vertical">
78

89
<androidx.constraintlayout.widget.ConstraintLayout
910
android:id="@+id/ctn_input_code"
1011
android:layout_width="match_parent"
11-
android:layout_height="60dp"
12-
android:layout_marginHorizontal="16dp"
12+
android:layout_height="wrap_content"
1313
android:layout_marginTop="10dp">
1414

1515
<EditText
1616
android:id="@+id/edt_code"
1717
android:layout_width="match_parent"
18-
android:layout_height="match_parent"
18+
android:layout_height="0dp"
1919
android:autofillHints="smsOTPCode"
2020
android:background="@android:color/transparent"
2121
android:cursorVisible="false"
2222
android:focusable="true"
2323
android:importantForAutofill="yes"
2424
android:inputType="number"
25-
android:textColor="@android:color/transparent" />
25+
android:textColor="@android:color/transparent"
26+
app:layout_constraintBottom_toBottomOf="parent"
27+
app:layout_constraintTop_toTopOf="parent"
28+
tools:ignore="LabelFor" />
2629

2730
<TextView
2831
android:id="@+id/tv1"
@@ -110,20 +113,49 @@
110113
android:textStyle="bold"
111114
app:layout_constraintBottom_toBottomOf="parent"
112115
app:layout_constraintDimensionRatio="1:1"
113-
app:layout_constraintEnd_toEndOf="parent"
116+
app:layout_constraintEnd_toStartOf="@id/tv7"
114117
app:layout_constraintStart_toEndOf="@id/tv5"
115118
app:layout_constraintTop_toTopOf="parent" />
116119

120+
<TextView
121+
android:id="@+id/tv7"
122+
android:layout_width="0dp"
123+
android:layout_height="0dp"
124+
android:layout_marginHorizontal="4dp"
125+
android:gravity="center"
126+
android:textColor="@color/color_f2f2f2"
127+
android:textSize="24sp"
128+
android:textStyle="bold"
129+
app:layout_constraintBottom_toBottomOf="parent"
130+
app:layout_constraintDimensionRatio="1:1"
131+
app:layout_constraintEnd_toStartOf="@id/tv8"
132+
app:layout_constraintStart_toEndOf="@id/tv6"
133+
app:layout_constraintTop_toTopOf="parent" />
134+
135+
<TextView
136+
android:id="@+id/tv8"
137+
android:layout_width="0dp"
138+
android:layout_height="0dp"
139+
android:layout_marginHorizontal="4dp"
140+
android:gravity="center"
141+
android:textColor="@color/color_f2f2f2"
142+
android:textSize="24sp"
143+
android:textStyle="bold"
144+
app:layout_constraintBottom_toBottomOf="parent"
145+
app:layout_constraintDimensionRatio="1:1"
146+
app:layout_constraintEnd_toEndOf="parent"
147+
app:layout_constraintStart_toEndOf="@id/tv7"
148+
app:layout_constraintTop_toTopOf="parent" />
149+
117150
</androidx.constraintlayout.widget.ConstraintLayout>
118151

119152
<TextView
120153
android:id="@+id/tvCodeInvalid"
121154
android:layout_width="match_parent"
122155
android:layout_height="wrap_content"
123-
android:layout_marginHorizontal="20dp"
124156
android:layout_marginTop="10dp"
125157
android:gravity="center_vertical"
126-
android:padding="5dp"
158+
android:padding="4dp"
127159
android:text="@string/invalid_code"
128160
android:textColor="@color/color_c45048"
129161
android:textSize="14sp"

otp_code_view/src/main/res/values/atttrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<declare-styleable name="OtpCodeView">
55
<attr name="code_radius" format="dimension" />
66
<attr name="code_length" format="integer" />
7+
<attr name="code_text_size" format="dimension" />
78
<attr name="code_color" format="color" />
89
<attr name="code_background_color" format="color" />
910
<attr name="code_stroke_color" format="color" />

0 commit comments

Comments
 (0)