I added a full border around a view but I need to add just the corner as shown image below :

I mean the red corner only .
I tried to adjust the below border xml , but it didn’t work :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="10dp" android:height="10dp" android:color="#B22222" />
<solid android:color="#FCE6C9" />
<corners android:radius="20dp" />
</shape>
Any help will be appreciated
I don’t think it’s possible to do this using a
ShapeDrawable, as it would require you to use some sort of margin or padding on the drawable itself. There actually is apaddingattribute, but unfortunately that only has effect on the content of theView, and not the drawable itself.That being said, an easy solution would be to create a 9-patch in stead and apply that as background to the
TextView. Just for demonstration purposes: make the 9-patch look somewhat like this:Edit:
On second thought, there’s actually another option that relies on using a
LayerDrawableto create the desired effect. It’s a bit tedious to create and I have my doubts it’ll be more efficient than using a 9-patch, but at least you don’t have to render out images, which means that if you need to make e.g. a change in colours, it’s more straightforward.Some details:
@drawable/roundedis the code snippet you posted yourself. The following two items are simply white rectangles with an offset, to create the white edges. Now, since these will also overlay the pink surface, we need two more pink rectangles (again with specific offsets) to counter that. The result is a background that looks exactly like what you’re showing in your question.Note that you might want to see if you can optimise this a bit. At the least I’d recommend not hardcoding the offsets (like I did for simplicity), but store them in a
dimens.xmlfile so you can keep these values centralized and consistent by referencing them from both theShapeDrawableandLayerDrawable.Addendum: On pre-ICS (or perhaps pre-Honeycomb) devices, there appears to be an issue with directly referencing colours with the
android:drawableattribute. You can however easily get around this by setting up another drawable (be it either a 9-patch orShapeDrawable) to represent this colour. For example, in the snippet above, you would replaceandroid:drawable="@color/pink"withandroid:drawable="@drawable/color_pink", wherecolor_pinkcan simply be an xml file containing:Obviously you will need to do the same for all other colours referenced in the
LayerDrawable. Tested on Gingerbread 2.3.7.