I want to create a drawable that contains a circle with a background color that comes from an external file. As such I unfortunately can't simply load the drawable from an Xml file but have to create it dynamically in Java. How do I create my circle directly in Java?
            Asked
            
        
        
            Active
            
        
            Viewed 92 times
        
    -3
            
            
        - 
                    You can create your custom view by extending view class – Vivek Mishra Oct 15 '18 at 08:33
 - 
                    You can create it by using ShapeDrawable – anhtuannd Oct 15 '18 at 08:35
 - 
                    Why there is need to create dynamically, We can programatically the change the color of of image inside imageView and change imageview background . To change image color refer https://stackoverflow.com/questions/14208367/how-to-change-image-color-dynamically-in-android – Jinson Paul Oct 15 '18 at 08:37
 
2 Answers
1
            
            
        You can use ShapeDrawables:
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.setIntrinsicHeight(height);
shapeDrawable.setIntrinsicWidth(width);
For circle, just use the same height and width.
        Pang
        
- 9,564
 - 146
 - 81
 - 122
 
        Mohammed Junaid
        
- 1,362
 - 14
 - 18
 
0
            I finally found a nonhacky way to create an oval shaped drawable:
    GradientDrawable gd = new GradientDrawable();
    int fillColor = Color.parseColor("FF0000");
    gd.setColor(fillColor);
    int strokeWidth = 2; // px not dp
    int strokeColor = Color.parseColor("#000000");
    gd.setStroke(strokeWidth, strokeColor);
    gd.setShape(GradientDrawable.OVAL);
        Christian
        
- 25,249
 - 40
 - 134
 - 225