Monday, 21 June 2021

How to fill edittext with placeholders until it is not filled

I faced the following problem: I need to implement the solution for the case when the phone should be entered to the edittext. This phone should have non-removable part and last four numbers should be filled with underscore at the beginning and then when user typing them underscores should be changed to numbers, like:

+12345____ -> typing 6 -> +123456___

I implemented the non-removable part. Here's the way how I did it:

binding.etPhoneNumber.filters = arrayOf(InputFilter.LengthFilter(args.phoneNumber?.length ?: 0))

binding.etPhoneNumber.doAfterTextChanged {
            val symbolsLeft = it?.toString()?.length ?: 0
            if (symbolsLeft < phoneNumberUi.length) {
                binding.etPhoneNumber.setText(phoneNumberUi)
                binding.etPhoneNumber.setSelection(symbolsLeft + 1)
            }
        }

But now I do not understand, how to handle the logic with underscores. I tried to append the underscores in doAfterTextChanged, like if ((args.phoneNumber?.length ?: 0) > (it?.toString()?.length ?: 0)) append n underscores, where n is the difference between length, but in this case I cannot add new symbols as edittext is filled and underscores are not removed. So, how can I solve the problem? Thanks in advance for any help!



from How to fill edittext with placeholders until it is not filled

No comments:

Post a Comment