Skip to content Skip to sidebar Skip to footer

How To Create Ring Shape Drawable In Android?

with this code I get just a border:

Solution 1:

2dp outer ring with a 2dp gap:

<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:top="4dp"android:right="4dp"android:bottom="4dp"android:left="4dp"><shapeandroid:shape="oval"><solidandroid:color="#4d4d4d" /></shape></item><item><shapeandroid:shape="oval"><strokeandroid:width="2dp"android:color="#4d4d4d"/></shape></item></layer-list>

Solution 2:

You will need to create a drawable and set it as a background.

shape_primary_ring.xml

<shapexmlns:android="http://schemas.android.com/apk/res/android"android:innerRadiusRatio="2.5"android:shape="ring"android:thickness="4dp"android:useLevel="false"><solidandroid:color="@color/colorPrimary"/></shape>

Preview

Ring Drawable Preview

Solution 3:

Ring drawable is juxtaposition of elements, use layer-list

<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:right="6dip"android:left="6dip"><shapexmlns:android="http://schemas.android.com/apk/res/android"android:innerRadius="0dp"android:shape="ring"android:thicknessRatio="3"android:useLevel="false" ><solidandroid:color="@android:color/transparent" /><strokeandroid:width="5dp"android:color="@color/maroon" /></shape></item><itemandroid:right="20dip"android:left="20dip"android:bottom="0dip"android:top="34dip"><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:innerRadius="0dp"><solidandroid:color="@color/maroon" /><strokeandroid:width="1dip"android:color="@android:color/transparent" /></shape></item><itemandroid:right="20dip"android:left="20dip"android:bottom="34dip"android:top="0dip"><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:innerRadius="0dp"><solidandroid:color="@color/maroon" /><strokeandroid:width="1dip"android:color="@android:color/transparent" /></shape></item></layer-list>

enter image description here

Xml drawable by all means more useful than static images, they can be correctly scaled without need of .9 library or generating set of different sizes from Gimp, Photoshop

Solution 4:

I think using a shape in android is better than a shape in photoshop.

Correct, creating a drawable it's better because you can change the colour or shape with code instead of creating a new image resource, for example. Create a FrameLayout with 2 Views and a TextView. The first view background would be your outer ring (shape) and the second a filed circle (shape). Finally the last View (bigger z-index) your TextView:

<FrameLayout><View/><!-- (outer ring)--><View/><!-- (filed circle)--><TextView/><!-- (text)--></FrameLayout>

Solution 5:

<?xml version="1.0" encoding="UTF-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android"android:fromDegrees="120"android:pivotX="50%"android:pivotY="50%"android:toDegrees="140"><itemandroid:id="@android:id/background"><shapeandroid:angle="0"android:innerRadiusRatio="2.3"android:shape="ring"android:thicknessRatio="20.0"android:type="sweep"android:useLevel="false"><solidandroid:color="@color/red" /></shape></item></layer-list>

like This

Post a Comment for "How To Create Ring Shape Drawable In Android?"