34

I have the following xml in drawable folder (circle_status.xml) to create a ring:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="15dp"
    android:thickness="10dp"
    android:useLevel="false">
    <solid android:color="#ababf2" />
</shape>

And I insert the drawable as a background for a RelativeLayout, as next:

<RelativeLayout
    android:id="@+id/RelativeLayout_Status"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/circle_status" />

The problem is that a circle appears in the RelativeLayout, not a ring.

bighugedev
  • 379
  • 1
  • 3
  • 11
suanido
  • 751
  • 1
  • 7
  • 12

4 Answers4

34

Note that a ring is an oval without a fill. Just with a stroke. And the view holding it, should be a perfect square.

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

<stroke
    android:width="1dp"
    android:color="@color/blue" />
</shape>

And the view holding it

<ImageView
     android:layout_width="10dp"
     android:layout_height="10dp"
     android:src="@drawable/ring" />
Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • 6
    "A ring is an oval without a fill, just with a stroke" is just what I needed to know (and what wasn't clearly stated in developer docs). – Jonik Jun 13 '16 at 09:37
  • That's not true. There's a notion of an inner radius. – dcow Apr 29 '17 at 02:15
  • 1
    "Note that a ring is an oval without a fill !!" good sentence. never thought of it :D. it works better than ring shape :D – Amir Ziarati Sep 18 '17 at 07:22
  • 1
    Just a note, if you want to tint it, set a transparent solid color to prevent a full oval from appearing: `` – alexbchr Apr 17 '18 at 14:10
22

I answer myself.

It seems the problem is in the Graphical Layout Editor of Eclipse, the code works fine in a real device.

suanido
  • 751
  • 1
  • 7
  • 12
  • I couldn't find an existing issue for this with Google, so I raised it here: https://code.google.com/p/android/issues/detail?id=92345 If you star that issue it is a tiny bit more likely to get fixed. :-) – Dan J Jan 01 '15 at 02:13
12

This hack shows a ring on both device and Android Studio:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring" android:innerRadius="23dp" android:thickness="0dp">
    <stroke android:width="2dp" android:color="#ababf2" />
</shape>
snodnipper
  • 2,580
  • 1
  • 29
  • 19
5

You must use <stroke> tag instead of <solid> tag for ring in a <shape> tag. Using <solid> tag in a <shape> tag results a circle not a ring.

<solid> tag can be used for ring background color and <stroke> for ring body color.

ygngy
  • 3,630
  • 2
  • 18
  • 29