-
-
Save petitJAM/040782f19e18f9feca4c28f0df6ed19f to your computer and use it in GitHub Desktop.
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:layout_marginEnd="16dp" | |
android:ellipsize="end" | |
android:maxLines="4" | |
app:layout_constrainedWidth="true" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<!-- layout_constrainedWidth="true" is key --> |
layout_constraintWidth_default="wrap"
is deprecated, so I updated to the thing it wanted me to use
Well, so this current revision doesn't work either. the width doesn't match constraint. i reset it to 0dp
width and removed layout_constrainedWidth
and it's fine?
It's 2020 and I just found my own Gist in a search result and it helped
i have given wrap_content and app:layout_constrainedWidth="true" but textView is constrained within the bounds but the android:ellipsize="end"
is not working. Is it working for you? or there is some-other work around ?
@andor201995 I think this is the view I was working with most recently:
<TextView
android:id="@+id/searchResultsExplainer"
style="?attr/textAppearanceBody1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:ellipsize="end"
android:maxLines="4"
app:layout_constraintBottom_toTopOf="@id/topBarrier"
app:layout_constraintEnd_toStartOf="@id/viewTypeToggle"
app:layout_constraintStart_toEndOf="@id/openFilters"
app:layout_constraintTop_toBottomOf="@id/searchInputLayout"
app:layout_constraintVertical_bias="0.5"
tools:text="Search results: "Dining Room""
tools:visibility="visible" />
Idk if that helps you at all.
Another thing you can try doing is ellipsizing the text manually. Something like:
val text = "Whatever really long string you are setting"
someTextView.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
someTextView.text = TextUtils.ellipsize(
text,
someTextView.paint,
someTextView.width.toFloat(),
TextUtils.TruncateAt.MIDDLE // Or END, etc
)
}
You need to do the change in an OnLayoutChangeListener because your view might not be laid out yet when you are calling this code to set the text. The TextUtils.ellipsize
method needs to know the width of the view the text is going into in order to ellipsize it properly.
Your solution was really helpful in guiding my usecase and used below code
private fun setSubTitle(text: String?) {
// set the layout with expected max width for text
contentText.isSingleLine = true
contentText.text = text
// reset the text with ellipsized text
contentText.doOnNextLayout {
if (it is TextView) {
it.text = TextUtils.ellipsize(
text,
it.paint,
it.width.toFloat(),
TextUtils.TruncateAt.END
)
}
}
}
Thanks a lot.
https://stackoverflow.com/questions/40410786/constraintlayout-chains-and-text-ellipsis-image-on-the-right