Hey, I want to restrict zero at the beginning of t...
# android
v
Hey, I want to restrict zero at the beginning of text input when the user type. So How it would be possible to restrict to enter zero at the be beginning, but the user can enter in the middle of the text. For example Not enter Zero at beginning
Copy code
0111 user will not allow displaying in edit text. only 111 is allowed
Allow zero at middle
Copy code
1101 it can allow zero in middle.
😶 4
r
Try using a custom input filter inside which you can check if the entered string contains zero at start or not and prevent entering it
v
Can you give me example please how?
r
This isn’t Kotlin related, but
EditText
can have
onTextChanged
listeners attached to it. I’d use that along with
String.first() == '0'
to solve your problem.
e
it can be trickier: what if the user enters 100, then deletes the 1
but yeah, just use existing Android APIs, nothing Kotlin specific about them
r
true that,
onFocusChangeListener
might be better