How to define a circle shape in an Android XML drawable file?
Asked Answered
T

19

830

I have some problems finding the documentation of the definitions of shapes in XML for Android. I would like to define a simple circle filled with a solid color in an XML File to include it into my layout files.

Sadly the Documentation on android.com does not cover the XML attributes of the Shape classes. I think I should use an ArcShape to draw a circle but there is no explanation on how to set the size, the color, or the angle needed to make a circle out of an Arc.

Tabbitha answered 6/7, 2010 at 9:40 Comment(3)
Here you can read about how to create shape drawable: developer.android.com/guide/topics/resources/…Blithe
You can get the circle xml from here developer.android.com/samples/WearSpeakerSample/res/drawable/…Textuary
The Android Dev documentation has been improved since then. Now it covers android:shape element - Drawable resources.Cutoff
B
1790

This is a simple circle as a drawable in Android.

<?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="120dp"
        android:height="120dp"/>
</shape>
Breechblock answered 25/8, 2012 at 12:24 Comment(11)
And how to change the color dynamically?Lacombe
@AnkitGarg you can apply a color filter from Java code (see Drawable class)Calciferol
I tried dp and it was distorted into an oval shape. For me, using pt instead fixed it.Ayakoayala
In some phones if you use the shape as "oval", even setting width and height to the same value, it seems a bit weird. Using "ring" as shape solved the problem to me.Eloquence
No need for the size in the shape if you later define a square size for the view.Genethlialogy
Its shape is oval and not round. Can anyone confirm??Exsiccate
are u able to add a image in the middle of that circle too?Policlinic
@jonney Add a circular image in the same place with margins. Make a new question if you need further help.Merriment
how can i fill the half circle with 2 colorsDemimondaine
@Tarun just set width equal to height, it will show up as a circleWoodcraft
for changing color of drawable programmatically, you can get the grawable directly by calling getDrawable of layout it has been assigned and then change color from there.Polyptych
D
881

Set this as your view background

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#78d9ff"/>
</shape>

enter image description here

For solid circle use:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>

enter image description here

Solid with stroke:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#199fff"/>
    <stroke
        android:width="2dp"
        android:color="#444444"/>
</shape>

enter image description here

Note: To make the oval shape appear as a circle, in these examples, either your view that you are using this shape as its background should be a square or you have to set the height and width properties of the shape tag to an equal value.

Danaedanaher answered 11/1, 2016 at 15:8 Comment(5)
Nice solution. Of course, the oval appears as a circle if the width and height of the view are the same. I think there was a way to make it appear as a circle no matter the size of the view.Genethlialogy
@FerranMaylinch "I think there was a way to make it appear as a circle no matter the size of the view." I solved this using a RelativeLayout wrapping both an ImageView (with the circle as "src" drawable) with fixed width/height and a TextView that wraps a text (for instance).Ossified
I am doing the exact same thing but the circle is drawn as ovalAdler
What if we need it to create around a textview with different border colors at run time ?Flopeared
@AnshulTyagi I believe you can do that by calling yourView.getBackground() and setting the color manually. you need to cast it to a proper type like ShapeDrawable. There are related questions on SO about this.Danaedanaher
T
104

Code for Simple circle

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <solid android:color="#9F2200"/>
        <stroke android:width="2dp" android:color="#fff" />
        <size android:width="80dp" android:height="80dp"/>
</shape>
Tailband answered 25/11, 2014 at 10:1 Comment(1)
From https://mcmap.net/q/55038/-how-to-import-shape-class-in-xml, the file needs to go in res/drawable not res/layoutSingular
G
54

You can use VectorDrawable as below :

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="64"
    android:viewportWidth="64">

    <path
        android:fillColor="#ff00ff"
        android:pathData="M22,32
        A10,10 0 1,1 42,32
        A10,10 0 1,1 22,32 Z" />
</vector>

The above xml renders as :

enter image description here

Garrotte answered 29/7, 2016 at 16:17 Comment(3)
Hey, I am having a padding between circle and viewport. Can you help me out?Nolde
@Nolde you can change the size of viewport and you can put your path inside group and specify the translation and scale <group android:translateX="-10" android:translateY="15"><path ...Garb
@Riyas can you explain the pathData part? what those co-ordinates imply?Wingard
B
46

Look in the Android SDK samples. There are several examples in the ApiDemos project:

/ApiDemos/res/drawable/

  • black_box.xml
  • shape_5.xml
  • etc

It will look something like this for a circle with a gradient fill:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
            android:angle="270"/>
</shape>

Beerbohm answered 11/8, 2011 at 17:32 Comment(0)
D
25

If you want a circle like this

enter image description here

Try using the code below:

<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/white" />
    <stroke
        android:width="1dp"
        android:color="@android:color/darker_gray" />
</shape>
Duala answered 6/2, 2018 at 9:36 Comment(0)
A
23
<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <!-- fill color -->
    <solid android:color="@color/white" />

    <!-- radius -->
    <stroke
        android:width="1dp"
        android:color="@color/white" />

    <!-- corners -->
    <corners
        android:radius="2dp"/>
</shape>
Amygdaloid answered 29/1, 2016 at 10:0 Comment(1)
What do the <corners /> do in an oval (which to my thinking don't have corners)?Echidna
T
22

Here's a simple circle_background.xml for pre-material:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/color_accent_dark" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/color_accent" />
        </shape>
    </item>
</selector>

You can use with the attribute 'android:background="@drawable/circle_background" in your button's layout definition

Trimly answered 9/6, 2015 at 23:28 Comment(1)
+ add 'size' to the shape(s) (depending on usecase).Advertising
E
21

a circle shape in an Android XML drawable file

<?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/white" />
    <stroke
        android:width="1.5dp"
        android:color="@android:color/holo_red_light" />
    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

Screenshot

enter image description here

Edina answered 5/6, 2020 at 19:35 Comment(0)
S
18

res/drawble/circle_shape.xml

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

        <shape android:shape="oval">
            <solid android:color="#e42828"/>
            <stroke android:color="#3b91d7" android:width="5dp"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>

enter image description here

Spikes answered 6/7, 2019 at 0:36 Comment(0)
C
18

Try below code with dash:

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

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

<stroke
    android:width="@dimen/_1sdp"
    android:color="@color/white"
    android:dashWidth="@dimen/_1sdp"
    android:dashGap="@dimen/_1sdp" />

Try code without dash:

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

<size
    android:width="@dimen/_60sdp"
    android:height="@dimen/_60sdp" />

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

<stroke
    android:width="@dimen/_1sdp"
    android:color="@color/white" />
Without dash With dash
Output without dash Out put
Carlocarload answered 28/4, 2021 at 8:3 Comment(0)
S
10
<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke
        android:width="10dp"
        android:color="@color/white"/>

    <gradient
        android:startColor="@color/red"
        android:centerColor="@color/red"
        android:endColor="@color/red"
        android:angle="270"/>

    <size 
        android:width="250dp" 
        android:height="250dp"/>
</shape>
Sukin answered 10/4, 2017 at 12:5 Comment(0)
D
6

I couldn't draw a circle inside my ConstraintLayout for some reason, I just couldn't use any of the answers above.

What did work perfectly is a simple TextView with the text that comes out, when you press "Alt+Num7":

 <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0075bc"
            android:textSize="40dp"
            android:text="•"></TextView>
Diplomat answered 1/5, 2018 at 13:52 Comment(0)
T
6

You can try this -

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="700"
    android:thickness="100dp"
    android:useLevel="false">

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

</shape>

Also, you can adjust the radius of the circle by adjusting android:thickness.

enter image description here

Tropical answered 7/6, 2018 at 8:39 Comment(1)
This is really nice! It certainly takes some playing with as the attributes are not intuitive. I managed to get this to display a nice empty circle for a border every time. And you can use padding to make sure the entire circle displays.Echidna
D
6

You can try to use this

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

<item>
    <shape
        android:innerRadius="0dp"
        android:shape="ring"
        android:thicknessRatio="2"
        android:useLevel="false" >
        <solid android:color="@color/button_blue_two" />
    </shape>
</item>

and you don't have to bother the width and height aspect ratio if you are using this for a textview

Desinence answered 13/11, 2019 at 12:21 Comment(0)
F
6

First Create Drawable Resource file

enter image description here

Then Add this Code to the file

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

   <solid 
        // here define your color
       android:color="#666666"/>

   <size 
        // here define your shape size
       android:width="120dp"
        android:height="120dp"/>
</shape>
Foah answered 25/3, 2022 at 7:20 Comment(0)
E
3

You can create a custom drawable to change color and radius of the circle dynamically

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CircleDrawable extends Drawable {

    private Paint circlePaint;
    private int fillColor;
    private int strokeColor;
    private float radius;

    public CircleDrawable(int fillColor, int strokeColor, float radius) {
        this.fillColor = fillColor;
        this.strokeColor = strokeColor;
        this.radius = radius;
        circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int x=getBounds().centerX();
        int y=getBounds().centerY();
        //draw fill color circle
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setColor(fillColor);
        canvas.drawCircle(x,y,radius,circlePaint);
        // draw stroke circle
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setColor(strokeColor);
        circlePaint.setStrokeWidth(5);
        canvas.drawCircle(x,y,radius,circlePaint);
    }

    @Override
    public void setAlpha(int alpha) {
        circlePaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
         circlePaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

Set this from UI to get the circle shape

imageView.setImageDrawable(new CircleDrawable(Color.RED,Color.YELLOW,100));

The Output Will be like this

enter image description here

Ermines answered 10/3, 2021 at 6:29 Comment(0)
L
1
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/text_color_green"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>
Landgrave answered 10/10, 2018 at 11:36 Comment(0)
F
-21

Just use

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );
Furlong answered 6/7, 2010 at 10:5 Comment(3)
And how can I set that as the src of an ImageView in my XML layout file?Tabbitha
This is not directly applicable for an xml layout codeAngelaangele
this is actually works. Thanks :) val background = ShapeDrawable(OvalShape()) background.paint.color = ContextCompat.getColor(holder.getView().context, R.color.green) val layers = arrayOf(background, resource!!) greenRoundIcon.setImageDrawable(LayerDrawable(layers))Orsa

© 2022 - 2024 — McMap. All rights reserved.