3

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)

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
rbaloo
  • 47
  • 6

3 Answers3

7

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.

laaptu
  • 2,963
  • 5
  • 30
  • 49
0

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.

Nicolas Roard
  • 8,339
  • 1
  • 28
  • 30
  • Its not working. I have install alpha 9 & ConstraintLayout 1.0.2 but its not working. Content goes out of screen – Amit Mar 17 '17 at 07:50
-1

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>
Mohit Trivedi
  • 699
  • 3
  • 13
  • You should never use ConstraintLayout in conjunction with RelativeLayout, you can check this post https://stackoverflow.com/questions/37321448/differences-between-constraintlayout-and-relativelayout – Eugene Brusov Oct 10 '17 at 13:38