12

I'm trying to draw a rounded rectangle with a border around it using a class that extends ShapeDrawable (see here) and everything is working except that the shapedrawable seems to be cutting off some of the border because the shape itself doesn't extend outside those bounds.

Is there not some way to offset where the shapedrawable starts drawing so that there is some padding between the bounds of the shape itself and the canvas? I have tried both ShapeDrawable.setBounds to larger than the intrinsic size of the shape and ShapeDrawable.setPadding but don't seem to be getting anywhere. Should I be subclassing Drawable instead?

Community
  • 1
  • 1
Stev_k
  • 2,118
  • 3
  • 22
  • 36

2 Answers2

25

You can wrap you shape drawable with inset (inset_shape.xml):

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/your_shape_drawable" android:insetBottom="10dip"
     android:insetLeft="10dip" android:insetRight="10dip" android:insetTop="10dip"
     android:visible="true" />

Then just use the inset_shape as you need.

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • 2
    Thanks, I created an InsetDrawable in code: [new InsetDrawable(shape,x,x,x,x)] and passed that instead which solved it. InsetDrawable seemed a bit hidden in the docs - very helpful – Stev_k Sep 09 '10 at 20:38
  • 2
    I just found that instead of passing android:drawable, you can also nest the drawable, so you don't need two files. Don't know why I had it in my mind that this was not possible. – velis Jun 08 '16 at 07:31
0

Try this in your drawable...!

<?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