Trying to change edittext's input color when character is alphabet. so I added textWatcher in edittext and check it with regex. It looks like working OK-ish but doesn't work when input String start with English. When input's first String start with alphabet then number text also change color as Red even though it is not in regex result.
val regEng = "[a-zA-Z]".toRegex()
private fun setResources() {
et_form.addTextChangedListener(editWatcher)
}
var editWatcher = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
p0.let { et ->
regEng.findAll(et.toString()).map { it.range }.forEach { range ->
Log.d("dddd", et.toString()[range.first].toString())
et?.setSpan(ForegroundColorSpan(Color.RED), range.first, range.first + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, start: Int, before: Int, count: Int) {
}
}
Here is what I tried.
If I input 12ABC then output looks like