Android - EditText gives IndexOutOfBounds Exception while using textAllCaps
I'm trying to create a very simple registration page using a relative layout. This registration page is linked to a fragment called RegistrationFragment.
I have five EditText fields for this layout: name, phone number, email, password, and confirm password. For some reason, I can enter text into password and confirm password, but whenever I try to enter any text into the other fields, they immediately crash the application with an IndexOutOfBounds Exception.
Here's the full stack trace:
java.lang.IndexOutOfBoundsException
at android.graphics.Paint.getTextRunAdvances(Paint.java:1879)
at android.text.TextLine.handleText(TextLine.java:747)
at android.text.TextLine.handleRun(TextLine.java:898)
at android.text.TextLine.measureRun(TextLine.java:414)
at android.text.TextLine.measure(TextLine.java:293)
at android.text.TextLine.metrics(TextLine.java:267)
at android.text.Layout.getLineExtent(Layout.java:998)
at android.text.Layout.drawText(Layout.java:329)
at android.widget.Editor.drawHardwareAccelerated(Editor.java:1380)
at android.widget.Editor.onDraw(Editor.java:1303)
at android.widget.TextView.onDraw(TextView.java:5163)
at android.view.View.draw(View.java:14465)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13300)
at android.view.View.getDisplayList(View.java:13404)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13300)
at android.view.View.getDisplayList(View.java:13404)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13300)
at android.view.View.getDisplayList(View.java:13404)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13300)
at android.view.View.getDisplayList(View.java:13404)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13300)
at android.view.View.getDisplayList(View.java:13404)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
My xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5">
<TextView
android:id="@+id/fragment_registration_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:text="@string/registration_title"
android:textAllCaps="true"
android:textSize="35sp"
android:layout_centerHorizontal="true"
android:textColor="@color/blue"
/>
<RelativeLayout
android:id="@+id/fragment_registration_edit_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_below="@id/fragment_registration_title"
android:layout_centerHorizontal="true">
<EditText
android:id="@+id/fragment_registration_legal_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/full_name_prompt"
android:maxLines="1"
android:textAllCaps="true"
android:background="@drawable/edit_text_top_rounded"
/>
<EditText
android:id="@+id/fragment_registration_cell_phone"
android:layout_below="@id/fragment_registration_legal_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/cell_phone_prompt"
android:maxLines="1"
android:textAllCaps="true"
android:background="@drawable/edit_text_white"
/>
<EditText
android:id="@+id/fragment_registration_email"
android:layout_below="@id/fragment_registration_cell_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/email_prompt"
android:maxLines="1"
android:textAllCaps="true"
android:background="@drawable/edit_text_white"
/>
<EditText
android:id="@+id/fragment_registration_password"
android:layout_below="@id/fragment_registration_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/confirm_password_prompt"
android:maxLines="1"
android:background="@drawable/edit_text_bottom_rounded"
/>
</RelativeLayout>
<Button
android:id="@+id/fragment_registration_button"
android:background="@drawable/button_registration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/registration_button"
android:textColor="#ffffff"
android:layout_below="@+id/fragment_registration_edit_text_layout"
android:layout_marginTop="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
If relevant, I'm "looking" at my Fragment code using the below, which is at the end of the onCreate() method of my main activity for testing purposes. I got the same errors when I tried testing Registration as an activity though.
RegistrationFragment test = new RegistrationFragment();
setContentView(R.layout.fragment_registration);
Solution 1:
I had the same problem with textAllCaps
for EditText
in my application.
I have found that
textAllCaps
is a property forTextView
only. You can not use this property forEditText
.
So, I did R&D for it and found a better solution for this issue.
Rather than using
textAllCaps
we can useandroid: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 the shift key and type text it may convert the 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
Solution 2:
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...
Solution 3:
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
.