Reputation: 2914
I want to capizalize every word typed in EditText
inside onTextChanged
. I've tried some solutions but none of them worked. Problem what I'm facing is if you change capitalize letter on keyboard and you will type James JoNEs
it should repair that String to correct form after you type E
character to Jone
. This is not working with default android:inputType="textCapWords"
. I've used some function what I've found but it is not working at all.
fun onFieldChanged(s: String, tv: TextWatcher, et: EditText) {
et.removeTextChangedListener(tv)
val changedString = capitalizeFirstLetterWord(s)
with(et) {
text.clear()
append(changedString)
setSelection(changedString.length)
}
et.addTextChangedListener(tv)
}
fun capitalizeFirstLetterWord(s: String): String{
var finalStr = ""
if(s != "") {
val strArray = s.split("[\\s']")
if (strArray.isNotEmpty()) {
for(i in strArray.indices){
finalStr+= capitalize(strArray[i])
}
}
}
return finalStr
}
Upvotes: 0
Views: 110
Reputation: 489
You can try to achieve that with something like this
"yourString".split(" ").map { it.toLowerCase().capitalize() }.joinToString(" ")
Upvotes: 3