NullPointerException
NullPointerException

Reputation: 37635

How to add a percentage margin between two views in ConstrainLayout

In ConstraintLayout, for specifying a percentage margin, you can use a guideline. For example, with this guideline, you can specify that imageView view has 1% of the screen height margin from top:

<android.support.constraint.Guideline
    android:id="@+id/guideline7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.1" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:src="@drawable/gamelogo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.14"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/guideline7"/>

It works, but... what happens if you need to specify a percentage margin between two views? I tried to set constraints to the guideline but it ignores them and sets at top of screen. So it seems that it's not possible to specify a percentage margin between two views.

How to do it?

Upvotes: 0

Views: 1004

Answers (2)

Tjaart
Tjaart

Reputation: 496

The 2 views that are at the endpoints of the guideline should be outside of the ConstraintLayout.

For example, the Component Tree should look as follows:

LinearLayout
    ImageView
    ConstraintLayout
        Guideline (horizontal)
        ImageView ---> The one that makes use of the Guideline
    ImageView

The ConstraintLayout should use the following settings:

<ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_gravity="fill_horizontal">

Upvotes: 0

Ben P.
Ben P.

Reputation: 54204

You can use a <Space> element in your layout to achieve this. Let's say you want 10% left/right margin and a 20% gap between two views that fill the rest of the space:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/startMargin"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.1"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/endMargin"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9"/>

    <View
        android:id="@+id/first"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#fac"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/startMargin"
        app:layout_constraintEnd_toStartOf="@id/space"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/first"
        app:layout_constraintEnd_toStartOf="@id/second"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintWidth_percent="0.2"/>

    <View
        android:id="@+id/second"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#caf"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/space"
        app:layout_constraintEnd_toStartOf="@id/endMargin"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

At this point, if you want to change the gap between the two views, all you have to do is change the app:layout_constraintWidth_percent attribute.

Upvotes: 2

Related Questions