Augusto Carmo
Augusto Carmo

Reputation: 4964

Set a max spacing between two views using Android's ConstraintLayout

I would like to know if there is any way to, in Android, using ConstraintLayout, to set a max spacing between two views when using Chains. I know that using the margin attributes works like a minimum spacing between two views, but I could not figure out how to set a max spacing.

For example, how could I achieve that through the following layout xml (max spacing = 20dp)?

<?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">

    <View
        android:id="@+id/left_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FF0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/right_view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread" />

    <View
        android:id="@+id/right_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/left_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

Upvotes: 3

Views: 4047

Answers (3)

Farid Z
Farid Z

Reputation: 1018

Use chain style "spread_inside"

app:layout_constraintHorizontal_chainStyle="spread_inside"

and set maxWidth on the parent constraint layout, example for three views:

android:maxWidth="150dp"

Upvotes: 1

RonTLV
RonTLV

Reputation: 2556

Set vertical or horizontal spacing with <Space>. The key is to set width or height to 0dp to max space between elements, or any value for fixed spacing (20dp):

<Space
    android:layout_width="0dp"
    android:layout_height="1dp"/>

Upvotes: 3

Birju Vachhani
Birju Vachhani

Reputation: 6353

If you want to set custom spacing (max 20dp) then you should use this:

app:layout_constraintHorizontal_chainStyle="packed"

Then you can add spacing between the views using margin so that it will be the max spacing between them.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <View
        android:id="@+id/left_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="10dp"
        android:background="#FF0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/right_view"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/right_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="10dp"
        android:background="#00FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/left_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 4

Related Questions