How to create a ring (round) ShapeDrawable

72 Views Asked by At

I need to set a round shape on top of an ImageView

Here is what I've tried so far:

float[] outerR = new float[]{8, 8, 8, 8, 8, 8, 8, 8};
 RoundRectShape roundrect = new RoundRectShape(outerR, null, null);
 ShapeDrawable drawable = new ShapeDrawable(roundrect);

This provides an oval shape. I need a round shape. How to fix this? Thx

enter image description here

2

There are 2 best solutions below

0
A S M Sayem On

First create a xml drawable file into your drawable folder into the res directory. Then into that xml file paste this code:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

   <solid 
       android:color="#666666"/>

   <size 
       android:width="50dp"
        android:height="50dp"/>
</shape>

and set this drawable as the background of your textView. Yeah, that will solve your issue.

2
Rajnish suryavanshi On

You can achieve by doing this way.

     <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_email_black_24dp"/>

        <TextView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="end"
            android:gravity="center"
            android:background="@drawable/bg_circle"
            android:text="5"
            android:textColor="@android:color/white"/>
    </FrameLayout>

bg_circle

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/holo_red_dark"/>

    <size android:height="10dp"
        android:width="10dp"/>

    <corners android:radius="10dp"/>

</shape>

By this you will get the same output.