GK100
GK100

Reputation: 3684

Android: How to set Max Width to match parent's width with layout_width="wrap_content"?

The width of the TextView below needs to match it's content width (in a ConstraintLayout), however when the content includes a long text, it currently extends horizontally beyond the width of the parent - as a single line. How do I make it wrap to multiple lines, instead?

Will setting android:maxWidth work? How do I set it to the parent's width (not a constant)?

<TextView
        android:id="@+id/hold_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/image"
        android:layout_marginBottom="16dp"
        android:text="could be a short or a long label" />

Upvotes: 0

Views: 81

Answers (2)

Jayanta Sarkar
Jayanta Sarkar

Reputation: 644

If you want your textview to take full screen width you can simply use

 android:layout_width="match_parent"

instead of

 android:layout_width="wrap_content"

But if you want to set the width according to the constraints using android:layout_width="0dp". and then manage the maxWidth of the textView then you can do is add this attribute.

android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_max="200dp"

NOTE : The 0dp will only work with constraint layout with correct contraints set.

Upvotes: 0

Winxo
Winxo

Reputation: 1

Use this line of code

app:layout_constrainedWidth="true"

Upvotes: 0

Related Questions