Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout - Have element take necessary space up to what's available

I have a TextView and a Button organized horizontally, in sequence, in a ConstraintLayout:

Working case

I need the first element (TextView) to take only the necessary space when the text is short enough, but to expand as necessary when more text needs to be displayed while still leaving enough space for the second element (Button) to be completely rendered inside the view, with its end aligned to the end of the parent.

This is what the XML currently looks like:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/element1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Short enough text"/>

    <Button
        android:id="@+id/element2"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:drawableLeft="@drawable/element2ButtonDrawable"
        android:drawablePadding="0dp"
        android:drawableStart="@drawable/element2ButtonDrawable"
        android:text="Action"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/element1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"/>

 </android.support.constraint.ConstraintLayout>

This is how the tree renders when switching from "Short enough text" to "A longer text that will cause most of the bottom to get pushed outside of the parent view bounds":

Bad case

Is it possible to achieve what I'm trying to do by using the ConstraintLayout?

(at time of writing, latest version is 1.0.2)

Thanks!

like image 246
droid256 Avatar asked Mar 20 '17 21:03

droid256


Video Answer


2 Answers

You should use packed horizontal chain with your TextView having width that matches constraints and horizontal bias equals 0.0

Here's solution:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:text="Short enough text"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0.0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:drawablePadding="0dp"
        android:drawableStart="@drawable/element2buttondrawable"
        android:text="Action"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

Here's how this layout looks on the device with different text and orientations:

Result view

You can read more about using chains in following posts:

  • Building interfaces with ConstraintLayout
  • Build a Responsive UI with ConstraintLayout
like image 87
Eugene Brusov Avatar answered Oct 10 '22 14:10

Eugene Brusov


Try something like this:

<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="wrap_content"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/element1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/element2"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="A longer text that will cause most of the bottom to get pushed outside of the parent view bounds" />

    <Button
        android:id="@+id/element2"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="Action"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
like image 44
Rapunzel Van Winkle Avatar answered Oct 10 '22 16:10

Rapunzel Van Winkle