ConstraintLayout
Android Development
Layout Design
UI Development
XML Layouts

How to make ConstraintLayout work with percentage values?

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

ConstraintLayout does not use percentage sizing the way older web-style layout systems do, but it does support percentage-based behavior through guidelines and percent width or height constraints. The key is to use the correct attributes and usually pair them with 0dp match-constraint sizing.

Use Percent Width or Height with Match Constraints

For percentage width or height, the view usually needs 0dp in that dimension.

xml
1<androidx.constraintlayout.widget.ConstraintLayout
2    android:layout_width="match_parent"
3    android:layout_height="match_parent">
4
5    <Button
6        android:id="@+id/button"
7        android:layout_width="0dp"
8        android:layout_height="wrap_content"
9        app:layout_constraintWidth_percent="0.5"
10        app:layout_constraintStart_toStartOf="parent"
11        app:layout_constraintEnd_toEndOf="parent"
12        app:layout_constraintTop_toTopOf="parent" />
13
14</androidx.constraintlayout.widget.ConstraintLayout>

Here the button uses 50 percent of the parent width.

Guidelines Are Another Common Percent Tool

Guidelines are especially useful when you want to position views at percentage-based boundaries.

xml
1<androidx.constraintlayout.widget.Guideline
2    android:id="@+id/guideline"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:orientation="vertical"
6    app:layout_constraintGuide_percent="0.3" />

Then constrain other views to that guideline. This is often clearer than trying to encode everything as percent width or height directly.

Remember That 0dp Means “Match Constraints” Here

A frequent mistake is setting width or height to wrap_content and expecting percent sizing to override it. In ConstraintLayout, 0dp usually signals that the size should be determined by constraints.

If the percentage is not taking effect, check that the relevant dimension is actually using match constraints.

Common Pitfalls

  • Using wrap_content when the percentage-based dimension needs 0dp.
  • Expecting percent sizing without enough constraints to define the layout.
  • Confusing guidelines for positioning with percent width or height for sizing.
  • Mixing several competing constraints and then blaming percentage attributes for the final layout.
  • Forgetting to use ConstraintLayout-specific attributes from the correct namespace.

Summary

  • ConstraintLayout supports percentage layouts through percent attributes and guidelines.
  • Use 0dp in the dimension that should be sized by constraints.
  • 'layout_constraintWidth_percent and layout_constraintHeight_percent handle percentage sizing.'
  • Guidelines are useful for percentage-based positioning.
  • Most percent-layout problems come from incorrect constraint setup rather than from the percent feature itself.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms