3

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>
Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61

2 Answers2

3

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"/>
RonTLV
  • 2,376
  • 2
  • 24
  • 38
2

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>
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
  • Thank you for your reply, @BirjuVachhani :). Where was the max spacing of the 20dp set in your example? – Augusto Carmo Aug 14 '19 at 11:04
  • Thanks a bunch for the edit. But now I have a second question: what if the screen is so small that a 20dp spacing is too much? With your solution, the spacing would be fixed to 20dp, not allowing it to be lower than that. – Augusto Carmo Aug 14 '19 at 11:15
  • Thanks for the another weird question. 1) I don't think that much small screen is in use nowadays(unless you're working on android wear!!). 2) I don't it is possible what you're asking. an image of what you're trying to achieve will be helpful though. – Birju Vachhani Aug 14 '19 at 11:19
  • @AugustoCarmo: If you don't want to hardcode 20dp. Best way is to keep in dimen's file and then create different dimen's as per resolution, screenwidth(sw) etc. Check this link: https://developer.android.com/training/multiscreen/screensizes – Akash Patra Aug 30 '19 at 04:29