Reputation: 51
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="HardcodedText">
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="abcdefghijklmn"
android:textSize="17.5sp"
app:layout_constraintEnd_toStartOf="@id/btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="do something"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
How can I make the width of MaterialTextView
wrap_content
but no larger than MaterialButton
in ConstraintLayout
?
I did that in XML
above, But the MaterialTextView
Not wrap_content
it seems like match_parent
.
I watched this question and that was not helpful for me or maybe I missed something.
Upvotes: 0
Views: 926
Reputation: 9103
To set the width of MaterialTextView
to wrap to its content but no larger than MaterialButton
you need to add the below attributes in your MaterialTextView xml layout: android:layout_width="wrap_content"
, app:layout_constrainedWidth="true"
and app:layout_constraintHorizontal_bias="0"
.
Example is like the below:
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Small Text"
android:background="@color/teal_200"
android:textSize="17.5sp"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintEnd_toStartOf="@id/btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Small Text:
Long Text:
Upvotes: 2