Android Kotlin EditText

Viewed 26

I have requirement where I have to show a edit text with below properties

  1. Maxlength of the edittext is 9 ( 7 digits and 2 commas) User can enter 4 digits max and rest 3 zeros will be automatically appended

i.e when user enters 1 , edit text become 1,000 when user enters 23 , edit text becomes 2,300

This am able to achieve , however i have 2 more questions

  1. How to prevent the user from entering 5th digits ( max user is allowed to enter 4 digits and maxlength of edit text will be 9 digits)

I tried setting InputType = InputType.NULL , setEnabled(false) in onTextChange() none seems to working fully

1 Answers

if you have already done the part to know if its digit or character then all you have to do is in your onTextChanged or whatever watcher you are using use something like String.valueOf(s.length() which will give you the length at runtime and then you can just tell the user to not enter the 5th digit or you can also keep a variable which changes its value and if user still wants to continue you can check against that variable again and don't let the user advance

Related