percentage margin with constraintlayout?

user1865027 picture user1865027 · Dec 18, 2017 · Viewed 13.6k times · Source

I'm learning how to use ConstraintLayout and have found a few good tutorials to have views width and height in percentage. I know I can probably add an empty view to 'create' margin, but it doesn't seem right. Is there a way to things like marginEnd='10%'?

Answer

Eugene Brusov picture Eugene Brusov · Dec 27, 2017

There're two ways to add percentage margin using ConstraintLayout.

#1 Guideline

Simply add vertical guideline to your layout and constraint view to that guideline:

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

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9" />

</android.support.constraint.ConstraintLayout>

#2 Constraint weight

Add Space, place it on the very left side of parent, group your view and Space into "spread" chain and set app:layout_constraintHorizontal_weight="90" to your view and app:layout_constraintHorizontal_weight="10" to Space. It works very similar to LinearLayout's weight.

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

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="90"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/space"
        app:layout_constraintBottom_toBottomOf="parent" />

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_weight="10"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

As a result, your view has end margin with value of 10% of parent width:

Result view