Android - IndexOutOfBounds
Exception while using textAllCaps in Edittext
There can be many cases that generate
IndexOutOfBounds
Exception.
While using ArrayList
, looping, and adaptors, are all kinds of lists
that generate this exception. But there is also another case that
generates IndexOutOfBounds
Exception, which is while using the textAllCaps
property in EditText. So let's talk about that.
Generally, when the app requires to fill data in forms or any text
fields in capital form, the first thing that comes to mind is
textAllCaps
. Why? Because it works perfectly while displaying data in
TextViews. Right?
We think that it will also work perfectly with
EditText
. But that's
where things go wrong. If we use the textAllCaps
property in EditText,
it starts giving exceptions while typing, and the app crashes.
The reason is simple.
textAllCaps
is a property of TextView, not
EditText. So, it is not compatible with EditText
and it generates
crashes. Though it EditText
is a direct sub-class of TextView
, it
inherits the property.
If we use
android:inputType="textCapCharacters"
it will convert all
characters into UPPER CASE, like we want in textAllCaps
. There are
some other attributes for that property that you can find on the
developer page.Reference: Android Input Types