Skip to content

Commit 2a7afb6

Browse files
author
Prince Awuah Baffour
committed
added disabling the keyboard programmatically
1 parent 4133104 commit 2a7afb6

File tree

4 files changed

+126
-11
lines changed

4 files changed

+126
-11
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/yogeshpaliyal/speld/MainActivity.kt

Lines changed: 122 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
66
import androidx.compose.foundation.BorderStroke
77
import androidx.compose.foundation.border
8-
import androidx.compose.foundation.layout.Column
9-
import androidx.compose.foundation.layout.Spacer
10-
import androidx.compose.foundation.layout.fillMaxSize
11-
import androidx.compose.foundation.layout.height
8+
import androidx.compose.foundation.layout.*
129
import androidx.compose.foundation.shape.RoundedCornerShape
13-
import androidx.compose.material.MaterialTheme
14-
import androidx.compose.material.Surface
10+
import androidx.compose.material.*
11+
import androidx.compose.material.icons.Icons
12+
import androidx.compose.material.icons.filled.ArrowBack
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.MutableState
1515
import androidx.compose.runtime.mutableStateOf
1616
import androidx.compose.runtime.remember
17+
import androidx.compose.ui.Alignment
1718
import androidx.compose.ui.Modifier
1819
import androidx.compose.ui.graphics.Color
1920
import androidx.compose.ui.unit.dp
@@ -27,9 +28,11 @@ class MainActivity : ComponentActivity() {
2728
// A surface container using the 'background' color from the theme
2829
Surface(color = MaterialTheme.colors.background) {
2930
val text = remember { mutableStateOf("") }
31+
val length = 5 // Define the length of the PIN here
3032

31-
32-
Column(modifier = Modifier.fillMaxSize()) {
33+
Column(modifier = Modifier.fillMaxSize(),
34+
Arrangement.Center, Alignment.CenterHorizontally
35+
) {
3336

3437
Spacer(modifier = Modifier.height(20.dp))
3538

@@ -47,16 +50,126 @@ class MainActivity : ComponentActivity() {
4750
shape = RoundedCornerShape(3.dp)
4851
), value = text.value,
4952
obscureText = "*",
50-
length = 6
53+
length = length, //Use the number defined here
54+
disableKeypad = true, //Do not open the android keypad
5155
) {
5256
text.value = it
5357
}
5458

59+
Spacer(modifier = Modifier.height(20.dp))
60+
61+
KeyPad(input = text, length)
62+
5563
}
5664

5765

5866
}
5967
}
6068
}
6169
}
70+
71+
@Composable
72+
fun KeyPad(input: MutableState<String>, length: Int){
73+
val callback = {
74+
text: String -> handleButtonClick(text, input, length)
75+
}
76+
77+
Column(
78+
modifier = Modifier
79+
.padding(5.dp, 5.dp)
80+
.fillMaxWidth(0.5f)
81+
82+
) {
83+
NumKeypadRow(
84+
listOf("1", "2", "3"),
85+
listOf(0.33f, 0.33f, 0.33f),
86+
callback
87+
)
88+
NumKeypadRow(
89+
listOf("4", "5", "6"),
90+
listOf(0.33f, 0.33f, 0.33f),
91+
callback
92+
)
93+
NumKeypadRow(
94+
listOf("7", "8", "9"),
95+
listOf(0.33f, 0.33f, 0.33f),
96+
callback
97+
)
98+
NumKeypadRow(
99+
listOf("C", "0", ""),
100+
listOf(0.33f, 0.33f, 0.33f),
101+
callback
102+
)
103+
}
104+
}
105+
106+
@Composable
107+
fun NumKeypadRow(
108+
texts: List<String>,
109+
weights: List<Float>,
110+
callback: (text: String) -> Any
111+
) {
112+
Row(
113+
modifier = Modifier
114+
.fillMaxWidth()
115+
.padding(2.dp, 2.dp)
116+
) {
117+
for (i in texts.indices) {
118+
val useIcon = texts[i]==""
119+
MyButton(
120+
text = texts[i],
121+
callback = callback,
122+
modifier = Modifier
123+
.weight(weights[i]),
124+
useIcon,
125+
)
126+
}
127+
}
128+
}
129+
130+
@Composable
131+
fun MyButton(
132+
text: String,
133+
callback: (text: String) -> Any,
134+
modifier: Modifier = Modifier,
135+
useIcon: Boolean = false
136+
) {
137+
Button(
138+
colors = ButtonDefaults.textButtonColors(
139+
backgroundColor = Color.LightGray,
140+
contentColor = Color.Black,
141+
142+
),
143+
onClick = {
144+
callback(text)
145+
},
146+
border = null,
147+
elevation = null,
148+
modifier = modifier
149+
.size(40.dp, 35.dp)
150+
.padding(2.dp, 0.dp)
151+
) {
152+
if (useIcon) {
153+
if (text == "") Icon(
154+
Icons.Filled.ArrowBack,
155+
contentDescription = "Favorite",
156+
modifier = Modifier.size(ButtonDefaults.IconSize)
157+
)
158+
}else Text(text)
159+
}
160+
}
161+
162+
private fun handleButtonClick(
163+
txt: String,
164+
inputTextView: MutableState<String>,
165+
length: Int
166+
) {
167+
when (txt) {
168+
"" -> if (inputTextView.value.isNotEmpty()) {
169+
inputTextView.value = inputTextView.value.dropLast(1) // remove last char
170+
}
171+
"C" -> inputTextView.value = "" // clear all text
172+
else -> if(inputTextView.value.length<length) inputTextView.value += txt // handles key input here
173+
}
174+
}
62175
}

speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ fun PinInput(
6363
modifier: Modifier = Modifier,
6464
length: Int = 5,
6565
value: String = "",
66+
disableKeypad: Boolean = false,
6667
obscureText: String? = "*",
6768
onValueChanged: (String) -> Unit
6869
) {
6970
val focusRequester = remember { FocusRequester() }
7071
val keyboard = LocalSoftwareKeyboardController.current
7172
TextField(
73+
readOnly = disableKeypad,
7274
value = value,
7375
onValueChange = {
7476
if (it.length <= length) {

0 commit comments

Comments
 (0)