Android - EditText gives IndexOutOfBounds Exception while using textAllCaps

亡梦爱人 提交于 2019-11-27 20:22:11

I had same problem with textAllCaps for EditText in my application.

I have found that textAllCaps is property for TextView only. You can not use this property for EditText.

So, I did R&D for it and found a better solution for this issue.

Rather than using textAllCaps we can use android:inputType="textCapCharacters".

E.g.

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textCapCharacters"
        android:hint="@string/first_name"
        android:padding="10dp" >
    </EditText>

If we use android:inputType="textCapCharacters" it will convert all characters into UPPER CASE, like we want in textAllCaps.

P.S. If you use shift key and type text it may convert text in lowercase. You can always use toUpper() method in string object to convert it back to uppercase. It may help..

You can read these details from this blog post : https://androidacademic.blogspot.com/2018/05/indexoutofbounds-exception-while-using.html

Ok, the problem is that textAllCaps is not working for EditText controls. It only works for commands that are not editable (like TextView). As per documentation on setAllCaps() (which is the code behind version of the textAllCaps):

This setting will be ignored if this field is editable or selectable

So, it will not work.

Now, even I am not sure why your code crashed, it shouldn't have, it should have just be ignored, but maybe, the problem occurs when textAllCaps is set from the xml file... I wasn't sure, but I just suggested what I would have changed the first, and hoped it would work...

Anyhow, I am glad I helped...

BhavikUp

I don't have enough reputation to make a comment but I had a similar runtime exception (and crash) while using a TextView and textAllCaps set to true.

When I removed that setting, everything worked as expected. So it seems that the advice given above can also apply to TextView in certain cases.

The same happens when you set android:digits and set to true textAllCaps while using EditText. The solution is the same, remove the textAllCaps.

I am using textAllCaps for TextView (not EditText) but it crash
With the same code like yesterday but today it crash (yesterday don't crash) (if I remove textAllCaps code will run normally)

Some case WON'T FIX my problem

  • Run it on different device and API
  • Clean project
  • Restart Android Studio
  • Invalid and restart Android Studio
  • Restart computer
  • Remove all another View, just only keep 1 TextView with textAllCaps attribute in layout

Then, I try to run my code on few different computer and it WORKING (my AndroidStudio is as same as another computer)

  • Then I try to reset all AndroidStudio setting on my computer -> run project again -> it still crash

Finally, I remove all things relevant to Android in my computer -> download Android Studio -> install -> run project -> fortunately it working

My current AS version is: 3.1.14. I am not sure it work in your case or not but hope it help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!