Solution 1 :
fixed by changing it to val end = layout.getLineEnd(layout.lineCount - 1)
Problem :
I have the TextView maxLine set to 5 and when I call getLineEnd(4) on a view that most definitely has 5+ lines it gives me the ArrayIndexOutOfBoundsException. I can’t figure out why this is happening. please help? Thanks!
logs
06-12 02:50:42.256 30175 30175 E ErrorPageActivity: java.lang.ArrayIndexOutOfBoundsException: length=27; index=30
06-12 02:50:42.256 30175 30175 E ErrorPageActivity: at android.text.StaticLayout.getLineStart(StaticLayout.java:1151)
06-12 02:50:42.256 30175 30175 E ErrorPageActivity: at android.text.Layout.getLineEnd(Layout.java:1409)
06-12 02:50:42.256 30175 30175 E ErrorPageActivity: at com.imdb.livingroom.firetv.util.TextViewExtensionsKt.visibleText(TextViewExtensions.kt:13)
TextViewExtensions
fun TextView.visibleText(): String {
val end = layout.getLineEnd(4)
return layout.text.toString().substring(0,end);
}
layout xml
<TextView
android_id="@+id/user_review_description"
style="@style/text_body_two"
android_layout_width="@dimen/review_description_width"
android_layout_height="wrap_content"
android_layout_marginTop="@dimen/micro_margin"
android_ellipsize="end"
android_maxLines="5"
android_layout_marginStart="@dimen/medium_margin"
android_importantForAutofill= "noExcludeDescendants"
app_layout_constraintLeft_toLeftOf="parent"
app_layout_constraintTop_toBottomOf="@+id/user_nickname"
/>
Here’s a picture of the view. The detailed review block is the textview in question