Search code examples
androidandroid-layoutandroid-buttonandroid-image

Android : How to write text to specific area in an image


I am creating an Imagebutton which displays notification number. Here is my sample image:

Icon Image

My goal is to write a number (1,2..) on specific black circle in an image. I tried RelativeLayout and FrameLayout but don't think it is a reliable way to achieve the result. The reason being Margins, Padding will vary on different device sizes.

Any suggestions to get started?


Solution

  • Here you go:

    <FrameLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pencil">
    
            <TextView
                android:id="@+id/circle"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:background="@drawable/circle"
                android:gravity="center"
                android:text="2"
                android:layout_gravity="bottom|right"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:textColor="#FFFFFF"
                android:textSize="12sp" />
    
        </FrameLayout>
    

    Images: enter image description here enter image description here

    Output:

    enter image description here