In Android, how do I position an image on top of another to look like a badge?

android, android-layout

Solution

RelativeLayout is a great option.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:scaleType="centerCrop"
    android:src="@drawable/iconImage" />

<ImageView
    android:id="@+id/badge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/icon"
    android:layout_alignTop="@+id/icon"
    android:src="@drawable/badge" />

If you actually want a badge with a dynamic number/text, then you can make the second `ImageView` a `TextView` (or a `ViewGroup` such as `LinearLayout` or `RelativeLayout`) and give it a background drawable and set the text to what you want.

Problem

Please see below: I tried using Absolute layout, but that's deprecated. I appreciate your help, thanks.

Original source