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%'
?
Asked
Active
Viewed 2.2k times
34

Phantômaxx
- 37,901
- 21
- 84
- 115

user1865027
- 3,505
- 6
- 33
- 71
-
Either use weights in LinearLayouts or use PercentRelativeLayouts. – Phantômaxx Dec 18 '17 at 18:07
-
try guideline for divide screens or add view – Tara Dec 18 '17 at 18:09
-
`PercentRelativeLayouts` is deprecated, so I wanted to try if `ConstraintLayout` can do the job – user1865027 Dec 18 '17 at 18:16
-
yes, guideline is what I'm using to set height and width for the view, but how can i apply that to margin? – user1865027 Dec 18 '17 at 18:16
3 Answers
57
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:

Eugene Brusov
- 17,146
- 6
- 52
- 68
9
Trick #3:
for the percentage from both sides
app:layout_constraintWidth_percent=".80"

amorenew
- 10,760
- 10
- 47
- 69
-
3Note: This is relative to parent size, not relative to arbitrary sides. – Enselic Apr 02 '20 at 12:15
9
You might want nested constraints with
app:layout_constraintWidth_percent="0.9"
app:layout_constraintHorizontal_bias="0.25"
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.9">
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Xml Example With bias
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.9"
app:layout_constraintHorizontal_bias="0.25">
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#Bias

CrandellWS
- 2,708
- 5
- 49
- 111
-
2This should be the right answer. The bias between the start to the end of the parent – Oz Shabat May 14 '20 at 16:01