I want to make upper case even if the user write lowercase in edittext.So I am setting ediText text to upper case on addTextChangedListener as per below.
editText.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(s : CharSequence?, start : Int, count : Int, after : Int) {
}
override fun onTextChanged(s : CharSequence?, start : Int, before : Int, count : Int) {
editText.setText(s.toString().toUpperCase())
}
override fun afterTextChanged(s : Editable?) {
}
})
But doing this when I type something in editText, app is hanging and I have to kill the app and restart it again.As I want to use this in binding adapter I can't use AllCaps method of xml
You need to remove the textwatcher before editing the text of the EditText and readd it again when the change is done.
val watcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
editText.removeTextChangedListener(this)
editText.setText(s.toString().toUpperCase())
editText.addTextChangedListener(this)
}
override fun afterTextChanged(s: Editable) {}
}
editText.addTextChangedListener(watcher)
You can use the Input type in your xml or programmatically.
editText.inputType == InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
In your xml you can put
android:inputType="textCapCharacters"
This solution will automatically turn the input to UperCase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With