4

I have this xml;

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:drawable="@drawable/back" />
    <item>
        <shape>
            <solid/>
            <stroke android:width="1dip" android:color="#25786" />
            <corners android:radius="10dip"/>
            <padding android:left="0dip" android:top="0dip" 
                     android:right="0dip" android:bottom="0dip" />
        </shape>
    </item> 
</layer-list> 

Now i am doing it via code:

Drawable[] layers = new Drawable[2];

    ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
    sd1.getPaint().setColor(0xFFFFFFFF);
    sd1.getPaint().setStyle(Style.STROKE);
    sd1.getPaint().setStrokeWidth(1);

    layers[0] = sd1;

    LayerDrawable composite = new LayerDrawable(layers);

But i am not able to code it for <corners android:radius="10dip"/> and <item android:drawable="@drawable/back" />

How to do it?

EDIT 1

Drawable[] layers = new Drawable[2];

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(0xFFFFFFFF);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(1);
sd1.getPaint().setPathEffect(new CornerPathEffect(10));

layers[1] = sd1;
layers[0] = getResources().getDrawable(R.drawable.pie_chart_back);

LayerDrawable composite = new LayerDrawable(layers);
Goofy
  • 6,098
  • 17
  • 90
  • 156
  • To set corners from code see (Gradient Drawables): http://stackoverflow.com/questions/8709595/how-to-set-corner-radiuses-for-the-button-in-java-code – samus Nov 13 '13 at 21:07

2 Answers2

5

Just like in your XML, you'll need three Drawables, not one. One is a LayerDrawable containing the others. The other one missing could be a BitmapDrawable depending on @drawable/back.

The corner effect you're looking for is in the CornerPathEffect of your Drawable's Paint, see Paint.setPathEffect.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • @Goofy You're right, the `LayerDrawable` is already there, so only the `BitmapDrawable` is missing. -- What do you mean by "the CornerPathEffect _is not there_? – class stacker Apr 22 '13 at 08:59
  • I am sorry i didnt not see it properly, can you please chek my edit and let me know is it fine now? – Goofy Apr 22 '13 at 09:02
  • @Goofy Your SDK and AVD can tell you much better than I can whether or not it's now working. ;) – class stacker Apr 22 '13 at 09:04
0

try this...!

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#e1e1e1" />

    <stroke
        android:width="2dp"
        android:color="#808080" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>
jigar
  • 1,571
  • 6
  • 23
  • 46