2

I want to draw a shape like my icon.xml programmatically, but I can't embed the second circle into the first circle this is my code :

icon.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/ok">
<shape
    android:shape="oval">
    <solid android:color="#ffffff" />
    <size android:width="33px" android:height="33px"/>
    <stroke android:width="4px"
        android:color="#ff0000"/>
    <padding android:bottom="8px"
        android:left="8px"
        android:right="8px"
        android:top="8px"/>
</shape>
</item>
<item>
<shape android:shape="oval">
    <solid android:color="#ccff0000"/>
</shape>
</item>

</layer-list>

MainActivity.java

    GradientDrawable layer1 = new GradientDrawable();
    layer1.setShape(GradientDrawable.OVAL);
    layer1.setSize(33,33);
    layer1.setColor(Color.WHITE);
    layer1.setStroke(4,Color.RED);

    GradientDrawable layer2 = new GradientDrawable();
    layer2.setShape(GradientDrawable.OVAL);
    layer2.setColor(Color.BLUE);

    InsetDrawable insetLayer2 = new InsetDrawable(layer1, 8, 8, 8, 8);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]
    {insetLayer2,layer2});
    button.setBackground(layerDrawable);

What's wrong ?

thank you in advance

1 Answer 1

1

Finally I got the answer. My problem was how to set padding for out Circle, however we can set padding programmatically using setLayerInset(int index, int l, int t, int r, int b) for two gradients separately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.