Button Append Example

1.1k views Asked by At

I am trying to add text to button name for example button "ButtonName" be "ButtonNameNow" by button.append("now") that is giving Uncaught Exception

Is there is any example or reason why I am getting this I have searched for example finding nothing also I didn't understand Google documentation about Button append but I have tested it before on Textview

E/UncaughtException: java.lang.IndexOutOfBoundsException
                                                                                                             at android.graphics.Paint.getTextRunAdvances(Paint.java:1908)
                                                                                                             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:1401)
                                                                                                             at android.widget.Editor.onDraw(Editor.java:1324)
                                                                                                             at android.widget.TextView.onDraw(TextView.java:5259)
                                                                                                             at android.view.View.draw(View.java:14520)
                                                                                                             at android.view.View.getDisplayList(View.java:13404)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.draw(View.java:14523)
                                                                                                             at android.widget.FrameLayout.draw(FrameLayout.java:472)
                                                                                                             at android.view.View.getDisplayList(View.java:13404)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.getDisplayList(View.java:13399)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.View.draw(View.java:14237)
                                                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
                                                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
                                                                                                             at android.view.View.draw(View.java:14523)
                                                                                                             at android.widget.FrameLayout.draw(FrameLayout.java:472)
                                                                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2386)
                                                                                                             at android.view.View.getDisplayList(View.java:13404)
                                                                                                             at android.view.View.getDisplayList(View.java:13446)
                                                                                                             at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1609)
                                                                                                             at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1484)
                                                                                                             at android.view.ViewRootImpl.draw(ViewRootImpl.java:2476)
                                                                                                             at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2340)
                                                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1963)
                                                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1053)
                                                                                                            at android.vi
2

There are 2 answers

2
GVillani82 On BEST ANSWER
button.setText(button.getText()+"now");
0
Mihai Coman On

I had the same problem. If I append a text to a Button, the app crash with the java.lang.IndexOutOfBoundsException fatal error. This issue I found only for Android 6 (API23).
The fix was:

String noItems = String.valueOf(tokens.length) + " " + getResources().getString(R.string.items); 
lastConnectedItems.setText(noItems);  

Instead of:

lastConnectedItems.setText(String.valueOf(tokens.length));
lastConnectedItems.append(" ");
lastConnectedItems.append(getResources().getString(R.string.share));

lastConnectedItems is Button.
Hope it helps somebody :)