Othmandroid
Othmandroid

Reputation: 235

How to make my TextView wrapping content and respect a maximum width

I have a constraintLayout with three views (ImageView, TextView, TextView) like this : my constraintLayout layout

I want the green TextView to be wrap_content and have also a maximum width, I don't have the exact value of the maximum with but my TextView should respect the minimum margin of 16dp. otherwise it will be multilines. Can you please help me out with this? Regards

Upvotes: 6

Views: 2187

Answers (3)

pop
pop

Reputation: 579

From version 1.1.0 of ConstraintLayout. This issue is resolved by using

app:layout_constrainedWidth=”true”

Upvotes: 13

Hong Duan
Hong Duan

Reputation: 4294

You can wrap the green TextView inside a RelativeLayout:

<android.support.constraint.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="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="Text1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@color/colorAccent"
            android:text="TEXT TEXT TEXT TEXT TEXT TEXT "/>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

It behaves like this:

Short text

short text

Long text

long text

Upvotes: 1

Maciej Baranowski
Maciej Baranowski

Reputation: 46

Try this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="TextView TextView TextView TextView TextView TextViewTextView TextView TextView TextView TextView TextView "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Upvotes: 0

Related Questions