rbaloo
rbaloo

Reputation: 47

Issue with ConstraintLayout and TextView wrap_content

Since I have updated to Android Studio 2.2 and start using ConstraintLayout with new UI Builder there is a problem, that I can't solve.

I have a simple layout with ImageView and TextView to the right of ImageView. Text in TextView updates dynamicaly from server (I don't know exactly length of this text). I want to TextView use all free space between ImageView and right side of screen. Earlier with RelativeLayout I used TextView android:layout_width="wrap_content" and it worked:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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="116dp"
        android:layout_height="178dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        tools:src="@drawable/pets"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_toEndOf="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."/>
</RelativeLayout>

RelativeLayout (ScreenShot)

But when I use ConstraintLayout, there is no textwrap and TextView is going out of screen.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="116dp"
        android:layout_height="178dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/pets"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:ellipsize="none"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."
        app:layout_constraintLeft_toRightOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

ConstraintLayout (ScreenShot)

Upvotes: 3

Views: 2210

Answers (3)

laaptu
laaptu

Reputation: 2973

Just go with match_constraints(0dp) which is similar to wrap_content. Meaning

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:ellipsize="none"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."
    app:layout_constraintLeft_toRightOf="@+id/imageView"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

This means that your layout will wrap it's content as per the constraints you have set.

Upvotes: 7

Nicolas Roard
Nicolas Roard

Reputation: 8449

Which version of ConstraintLayout are you using? alpha 8 had a regression related to that. Try updating to alpha 9 (don't forget to restart studio and/or invalidate caches if you see an exception), this should fix your problem.

Upvotes: 0

Mohit Trivedi
Mohit Trivedi

Reputation: 729

Try following

<?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <ImageView
    android:id="@+id/imageView"
    android:layout_width="116dp"
    android:layout_height="178dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    tools:src="@drawable/pets"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_toEndOf="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."/>
    </RelativeLayout>
    </android.support.constraint.ConstraintLayout>

Upvotes: -1

Related Questions