Transparent circle with border
Asked Answered
A

11

101

I am trying to create a circle with only a border using XML in android:

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

<stroke android:width="1dp"
    android:color="#000000"/>

</shape>

The code I've used is posted above. However, I get a solid disk and a not a ring. I would like to get the output using just XML and not canvas. What am i doing wrong?

Thanks.

EDIT: Got it to work thanks to the answer below. Heres my final code:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9"
    android:useLevel="false" >

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

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

    <stroke android:width="1dp"
    android:color="#FFFFFF"/>

</shape>
Arrearage answered 19/4, 2013 at 6:27 Comment(0)
P
203

Try something like this

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="2dp"
        android:color="@android:color/darker_gray" />
</shape>

Update: made android:thicknessRatio="2" to give full circle (using Nexus 5 - Lollipop)

Place answered 19/4, 2013 at 6:30 Comment(3)
I made it android:thicknessRatio="2" to be a closed ring (Nexus 5, Lollipop)Scale
The area outside the circle is not showing up as transparent for me.Nellynelms
@Nellynelms try imageview instead of buttonGeneralize
P
35

use this it will work

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

<gradient
    android:centerX=".6"
    android:centerY=".40"
    android:endColor="@android:color/transparent"
    android:gradientRadius="20"
    android:startColor="@android:color/transparent"
    android:type="radial" />

<stroke
    android:width="1dp"
    android:color="#FFFFFF" />

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

</shape>
Pentathlon answered 7/7, 2014 at 14:59 Comment(0)
V
15

Hollow

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <stroke
            android:width="1dp"
            android:color="@color/indicator_unselected" />
    </shape>

Full

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="100dp" />
    <solid android:color="@android:color/white" />
</shape>
Violative answered 9/7, 2016 at 16:11 Comment(0)
R
9

The stroke effect can be achieved drawing a transparent oval with the stroke of a required color (#000 in the example):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="#000" />
    <size
        android:width="40dp"
        android:height="40dp" />
</shape>
Rexfourd answered 19/2, 2019 at 10:37 Comment(0)
S
3

If you can use Vector drawables try this

<vector android:height="24dp" android:viewportHeight="512.0"
    android:viewportWidth="512.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FFFFFF" android:fillType="evenOdd"
        android:pathData="M0,0L512,0L512,512L0,512L0,0ZM256,511C396.8,511 511,396.8 511,256C511,115.2 396.8,1 256,1C115.2,1 1,115.2 1,256C1,396.8 115.2,511 256,511Z"
        android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
Stephanstephana answered 6/10, 2017 at 11:59 Comment(0)
M
2

You can use android inbuilt value for transparent as @android:color/transparent or use #0000 or #00000000

for above starting for 4 digit first was for alpha and in 8 digit value first two digit was for same as alpha.

When you just give 3 digit or 6 digit in color than default alpha value was ff you by passing in 4 digit or 8 digit value set that alpha of that color value

Maxima answered 19/4, 2013 at 6:43 Comment(2)
Hi Pratik, I didnt understand the alpha part of your answer. I added the line <solid android:color="#0000"/> to the XML file. But still got the same result.Arrearage
this alpha was used to set the opacity of color. If you set the 4 0's or 8 0's than it fully transparent, with the increase of first 2 digit in 8 0's value of that only increase the value of opacityMaxima
W
2
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval" >

        <gradient
            android:endColor="@android:color/transparent"
            android:gradientRadius="20"
            android:startColor="@android:color/transparent" />

        <stroke
            android:width="1dp"
            android:color="#d9d9d9" />

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

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="oval" >

            <gradient
                android:endColor="@android:color/transparent"
                android:gradientRadius="20"
                android:startColor="@android:color/transparent" />

            <stroke
                android:width="1dp"
                android:color="#b3b3b3" />

            <size
                android:height="100dp"
                android:width="100dp" />
        </shape>
    </item>
</layer-list>
Woolley answered 7/8, 2018 at 17:48 Comment(0)
V
1
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval" >

            <stroke
                android:width="4dp"
                android:color="@color/colorPrimaryDark" />
            <corners android:radius="0dp" />
        </shape>
    </item>
    <item
        android:top="1dp"
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">

        <shape android:shape="oval">
            <solid android:color="@color/colorRed" />
            <size android:height="@dimen/_100sdp"
                android:width="@dimen/_100sdp"></size>
        </shape>

    </item>

</layer-list>
Valletta answered 19/4, 2018 at 10:51 Comment(0)
X
0

If you set the color to #00000000, the result will be transparent. You would want to do it this way if you wanted to change it in the future of development. If you wanted it to be red and partially transparent for example, it would be #ff000088 The last two numbers are the opacity. I do it this way to make future changes easier.

Xylol answered 19/2, 2019 at 10:49 Comment(0)
L
0
<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:innerRadiusRatio="2"
     android:shape="ring"
     android:thicknessRatio="1"
     android:useLevel="false">

    <gradient
        android:type="radial"
        android:gradientRadius="8dp"
        android:endColor="@color/colorDarkPurple"
        android:elevation="5dp"
    />

    <stroke
        android:width="2dp"
        android:color="@color/colorLilac"/>
</shape>
Labonte answered 30/6, 2020 at 10:25 Comment(0)
D
0

You can try setting background of image view with this drawable :

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

            <stroke
                android:width="4dp"
                android:color="@android:color/black" />
            <corners android:radius="0dp" />
        </shape>
    </item>
    <item
        android:top="8dp"
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp">

        <shape android:shape="oval">
            <solid android:color="@android:color/black" />
            <size
                android:height="100dp"
                android:width="100dp" />
        </shape>

    </item>

</layer-list>

Drastic answered 13/5, 2022 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.