Here I am using layer list to draw nested Circle by using XML
<item>
<shape android:shape="oval" >
<stroke
android:width="1dp"
android:color="@android:color/holo_orange_light" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
</shape>
</item>
<item>
<shape android:shape="oval" >
<solid android:color="@color/welcome_bg" />
</shape>
</item>
No I want same nested circle by using programmatically because I want to change color dynamically or is there any way to change color dynamically in xml provided above
Here is my custom View
public class MyView extends EditText {
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(50, 50, 300, 300);
canvas.drawOval(oval1, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(55, 55, 295, 295);
canvas.drawOval(oval2, paint);
}
}
Thanks
If you want to change the drawable color while keeping using the xml, you could add an id to the item you want to modify:
And then in your code get the layer list drawable and search for relevant id and change the color:
Note that I'm casting to GradientDrawable, because that's what
<shape>
tag is a pointer to (shape tag documentation)When you use xml, specified dimensions are in dp - density independent pixels. But in your code drawing functions take actual pixels as parameters and you have to take that into account and calculate proper values yourself.
Depending on your device declared screen density 1dp will be translated to:
formula for calculating real pixels is
px = dp * (dpi / 160)
You can read more:
Here I provided XML and set their ids as well and changing color programmatically The way color changing of XML Items
Thanks to all, Its works for me and please close it