Olatunji Oluwayomi Olatunji Oluwayomi - 5 months ago 15
Java Question

Editext gives IndexOutOfBound Exception

I created a custom EditText so as to be able to move and resize the EditText. It is working on my phone but crashed on other phone when i start to type.

When i added

android:inputType="text"
to the Xml Code it worked but the whole purpose of the application was lost
Here is the full stack error

java.lang.IndexOutOfBoundsException
at android.graphics.Paint.getTextRunAdvances(Paint.java:1841)
at android.graphics.Paint.getTextRunAdvances(Paint.java:1818)
at android.text.TextLine.handleText(TextLine.java:755)
at android.text.TextLine.handleRun(TextLine.java:907)
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:977)
at android.text.Layout.drawText(Layout.java:329)
at android.widget.Editor.drawHardwareAccelerated(Editor.java:1384)
at android.widget.Editor.onDraw(Editor.java:1298)
at android.widget.TextView.onDraw(TextView.java:5061)
at com.oluwayomi.quotage.custom.DragEditText.onDraw(DragEditText.java:340)
at android.view.View.draw(View.java:13917)
at android.view.View.getDisplayList(View.java:12832)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
at android.view.View.getDisplayList(View.java:12755)
at android.view.View.getDisplayList(View.java:12876)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1249)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2520)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2387)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2189)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)


Here is the XML Code

<RelativeLayout
android:id="@+id/TextRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.oluwayomi.quotage.custom.DragEditText
android:id="@+id/etText"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="124dp"
android:background="@drawable/null_background"
android:gravity="center"
android:padding="10dp"
android:hint="Type text here.\n Double tap to edit \n and long press to resize."
android:textSize="22sp" />
</RelativeLayout>


Here is the code for my custom EditText

public DragEditText(Context context) {
this(context, null);

}

public DragEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintFrame = new Paint();
mPaintTranslucent = new Paint();
c = context;

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutParams);
}

public DragEditText(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs);

} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
int viewHeight = MeasureSpec.getSize(heightMeasureSpec);

mParentWidth = c.getResources().getDisplayMetrics().widthPixels;
mParentHeight = c.getResources().getDisplayMetrics().heightPixels;
mParentRect = new RectF(0, 0, viewWidth, viewHeight);
}
@Override
protected void onDraw(Canvas canvas) {
drawDragFrame(canvas);
super.onDraw(canvas);
}
private void drawDragFrame(Canvas canvas) {
if (showResizeFrame) {
drawFrame(canvas);
drawHandleShadows(canvas);
drawHandles(canvas);
} else if (showDragFrame) {
drawHighlight(canvas);
}
}

private void drawFrame(Canvas canvas) {
mPaintFrame.setAntiAlias(true);
mPaintFrame.setFilterBitmap(true);
mPaintFrame.setStyle(Paint.Style.STROKE);
mPaintFrame.setColor(getResources().getColor(R.color.colorAccent));
mPaintFrame.setStrokeWidth(1);
canvas.drawRect(mFrameRect.left + mHandleSize / 2, mFrameRect.top, mFrameRect.right - mHandleSize / 2, mFrameRect.bottom, mPaintFrame);
}

private void drawHighlight(Canvas canvas) {
mPaintTranslucent.setAntiAlias(true);
mPaintTranslucent.setFilterBitmap(true);
mPaintTranslucent.setStyle(Paint.Style.FILL);
mPaintTranslucent.setColor(TRANSLUCENT_WHITE);
mPaintTranslucent.setStrokeWidth(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawRoundRect(mFrameRect.left, mFrameRect.top, mFrameRect.right - mHandleSize, mFrameRect.bottom, mHandleSize, mHandleSize, mPaintTranslucent);
} else
canvas.drawRect(mFrameRect.left, mFrameRect.top, mFrameRect.right - mHandleSize, mFrameRect.bottom, mPaintTranslucent);
}

private void drawHandles(Canvas canvas) {
//drawHandleShadows(canvas);
mPaintFrame.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintFrame.setColor(getResources().getColor(R.color.colorAccent));


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawRoundRect(mFrameRect.left, mFrameRect.top + mHandleSize, mFrameRect.left + mHandleSize, mFrameRect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
canvas.drawRoundRect(mFrameRect.right - mHandleSize, mFrameRect.top + mHandleSize, mFrameRect.right, mFrameRect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
} else {
canvas.drawRect(mFrameRect.left, mFrameRect.top + mHandleSize, mFrameRect.left + mHandleSize, mFrameRect.bottom - mHandleSize, mPaintFrame);
canvas.drawRect(mFrameRect.right - mHandleSize, mFrameRect.top + mHandleSize, mFrameRect.right, mFrameRect.bottom - mHandleSize, mPaintFrame);
}

}

private void drawHandleShadows(Canvas canvas) {
mPaintFrame.setStyle(Paint.Style.FILL);
mPaintFrame.setColor(TRANSLUCENT_BLACK);
RectF rect = new RectF(mFrameRect);
rect.offset(0, 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawRoundRect(rect.left, rect.top + mHandleSize, rect.left + mHandleSize, rect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
canvas.drawRoundRect(rect.right - mHandleSize, rect.top + mHandleSize, rect.right, rect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
} else {
canvas.drawRect(rect.left, rect.top + mHandleSize, rect.left + mHandleSize, rect.bottom - mHandleSize, mPaintFrame);
canvas.drawRect(rect.right - mHandleSize, rect.top + mHandleSize, rect.right, rect.bottom - mHandleSize, mPaintFrame);
}
}

Answer

After searching without seeing a helpful solution, I finally discovered the problem

style="@style/Widget.AppCompat.Button.Borderless"

Cannot be applied EditText.

Thanks to everyone.

Comments