Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw circle shape programmatically in android

What I want to do is to draw a circle and fill it by one color (for example orange) and want to make a border in another color (blue) programmatically. I didn't find any tutorial on how to do this.

This is what I want to get:

enter image description here

like image 781
Aslan Almaniyazov Avatar asked Oct 16 '25 09:10

Aslan Almaniyazov


1 Answers

To achieve the circle drawable programmatically you need to have a function like the following.

public static GradientDrawable drawCircle(int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.OVAL);
    shape.setCornerRadii(new float[]{0, 0, 0, 0, 0, 0, 0, 0});
    shape.setColor(backgroundColor);
    shape.setStroke(10, borderColor);
    return shape;
}

And set the drawable in your ImageView like the following.

imageView.setBackground(drawCircle(getResources().getColor(android.R.color.holo_blue_dark), getResources().getColor(android.R.color.holo_red_dark)));

This is giving something like this.

enter image description here

like image 168
Reaz Murshed Avatar answered Oct 18 '25 23:10

Reaz Murshed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!