Android
ConstraintLayout
View Overlap
Android Development
Mobile UI Design

Android ConstraintLayout - Put one view on top of another view

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In ConstraintLayout, putting one view on top of another is mostly about two things: sharing the same constrained position and controlling drawing order. The layout engine handles placement, while elevation or XML order determines which view visually appears above the other.

Constrain Both Views to the Same Area

For a common overlay pattern, constrain the second view relative to the first view or to the same parent edges. A badge on top of an image is a good example.

xml
1<androidx.constraintlayout.widget.ConstraintLayout
2    xmlns:android="http://schemas.android.com/apk/res/android"
3    xmlns:app="http://schemas.android.com/apk/res-auto"
4    android:layout_width="match_parent"
5    android:layout_height="wrap_content">
6
7    <ImageView
8        android:id="@+id/avatarImage"
9        android:layout_width="96dp"
10        android:layout_height="96dp"
11        android:scaleType="centerCrop"
12        android:src="@drawable/avatar"
13        app:layout_constraintStart_toStartOf="parent"
14        app:layout_constraintTop_toTopOf="parent" />
15
16    <TextView
17        android:id="@+id/statusBadge"
18        android:layout_width="24dp"
19        android:layout_height="24dp"
20        android:background="@drawable/badge_circle"
21        android:gravity="center"
22        android:text="3"
23        android:textColor="@android:color/white"
24        android:elevation="8dp"
25        app:layout_constraintEnd_toEndOf="@id/avatarImage"
26        app:layout_constraintBottom_toBottomOf="@id/avatarImage" />
27
28</androidx.constraintlayout.widget.ConstraintLayout>

The badge is not "floating randomly". It is anchored to the bottom end of the image. Because it is declared later and has elevation, it appears on top.

Understand Z Order

In overlapping layouts, the visual stacking order matters as much as the constraints.

  • views declared later in XML are normally drawn later, so they appear above earlier siblings
  • 'android:elevation and translationZ can raise a view visually above others'

If two views overlap but the wrong one appears on top, the issue is often not with constraints at all. It is usually drawing order or elevation.

In code, you can raise a view dynamically:

kotlin
badgeView.translationZ = 8f

That is useful for state changes such as dragging, selection, or temporary overlays.

When ConstraintLayout Is Enough and When It Is Not

ConstraintLayout can handle overlays well when you still need relational positioning. If the design is simply "stack this icon on top of that image", it works fine.

If the layout is purely layered with little relational logic, a FrameLayout may be simpler. That is not a limitation of ConstraintLayout; it is just a question of choosing the clearest container for the job.

Use ConstraintLayout when the overlapping views also need to participate in a more complex responsive layout. Use FrameLayout when stacking is the main concern.

Avoid Interaction Problems

Overlapping one view on another changes touch behavior. The topmost clickable view usually receives the event, not the one underneath. If the overlay is decorative only, make sure it does not block the base view unnecessarily.

For example:

xml
android:clickable="false"
android:focusable="false"

on a decorative overlay can prevent accidental interception of taps.

Accessibility also matters. If the overlaid view conveys important information, expose that meaning through content descriptions or merged accessibility text rather than relying only on visual placement.

Debugging Overlap Issues

If the overlay is not appearing where you expect, inspect the problem in two passes. First verify the constraints, because a misplaced overlay is often anchored to the parent instead of the base view. Then verify z order, because a correctly positioned view can still be hidden under a sibling with higher elevation.

Android Studio's Layout Inspector is useful here. It helps confirm whether the problem is position, size, or draw order instead of forcing you to guess from XML alone.

Common Pitfalls

  • Trying to solve overlap with margins alone instead of proper constraints tied to another view.
  • Forgetting that draw order and elevation control which view appears above the other.
  • Using ConstraintLayout for simple stacking when a FrameLayout would be clearer.
  • Letting a decorative overlay intercept touches intended for the underlying view.
  • Overusing overlapping views and creating unnecessary overdraw in complex screens.

Summary

  • In ConstraintLayout, overlap comes from shared positioning plus correct z order.
  • Anchor the overlay view to the base view with constraints rather than guessing with margins.
  • Use XML order or elevation when the top view is not rendering above the bottom one.
  • Check touch and accessibility behavior after adding overlays.
  • Choose FrameLayout instead when the layout is mostly just stacking.

Course illustration
Course illustration

All Rights Reserved.