Aslan Almaniyazov
Aslan Almaniyazov

Reputation: 93

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

Upvotes: 4

Views: 9045

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

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

Upvotes: 11

Related Questions