ConstraintLayout

Views, layouts and RecyclerView Junior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

A form has labels of different lengths, and inputs should start right after the longest label in any language. What's the best tool?

Options
Hint 1 of 1

Which helper moves with content size?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A A Barrier at the end of all labels, with inputs constrained to it

Correct. The barrier follows whichever label is widest.

Why not the others?
  • B A Guideline at 30%. A fixed percentage breaks with long translations.
  • C A nested LinearLayout per row with weights. Each row aligns on its own, not to the longest label.
  • D Fixed dp margins. Breaks across languages and font sizes.

30-second answer

ConstraintLayout positions views with constraints to the parent, siblings, guidelines or barriers, so complex screens stay flat instead of nested. Chains distribute a group of views along an axis (spread, spread inside, packed), guidelines give fixed or percentage anchors, and barriers move with the largest of several views. A flat tree means fewer measure passes.

Practise saying it · 30 s. No recording, just a timer

Explain ConstraintLayout and its helpers.

Say your answer out loud, then check which of these points you covered:

  • Flat layouts with constraints
  • Chains distribute views
  • Guidelines and barriers
  • 0dp means match constraints

Deep dive

Use 0dp (match constraints) to let a view fill the space between its constraints.

Barriers solve 'align to whichever label is longest', which nested layouts handle badly.

Use layout_constraintDimensionRatio for aspect-ratio images.

In Compose, Row, Column and Box are usually enough; ConstraintLayout for Compose exists for complex cases.

Common mistakes

  • Using match_parent inside ConstraintLayout instead of 0dp with constraints.
  • Forgetting a vertical constraint so the view jumps to the top.
  • Nesting ConstraintLayouts by habit.

Senior take & follow-ups

Mention barriers; it's the feature that shows you've built real screens with it.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does a barrier do?

    Show hint

    Creates a virtual line at the edge of the biggest referenced view.

  2. Is ConstraintLayout needed in Compose?

    Show hint

    Rarely; Rows, Columns and Boxes are cheap there.

Suggest a fix if something here is wrong or out of date.