Android supports all sort of drawable (
Bitmap like PNG or JPG being only one of them). The one we are going to talk about today is so-called Shape drawable.
They can be defined with a simple XML syntax and support a number of basic shapes like rectangles (with rounded corners or not) or ovals which can be filled with a solid color or a gradient (borders can also be defined).
You define those in the
drawable folder of your solution as normal. The Android designer in MonoDevelop is actually smart enough to pick up these ones so you can change them and see the result live while designing your application(!).
As a standalone drawable, it’s probably not that useful but used in conjunction with your
Views, it allows you to simply theme your widgets.
In the following image reused from a previous post, we actually have 3 different shape drawables in action:
First, the circle around the coffee cup is an oval drawable filled with a semi-transparent color to darken the main header background color. The shape source is following:
1 2 3 4 5 6
The layout XML references it in the following snippet (
1 2 3 4 5 6 7 8 9 10 11
To force the oval shape to be square, you have to force the size of the
FrameLayout. The value depends on your application, you can even do it dynamically but it has to be in code.
Second, the bottom bar where the location and date appear is a rectangle shape filled with a gradient to give it volume.
The bar shape XML is given below:
1 2 3 4 5 6 7 8 9
Here I’m combining the
<solid/> element to paint my base color and the
<gradient/> element to do the shading on top of it.
Again, to apply the effect, simply use the
android:background attribute on your element:
1 2 3 4
Finally, the overall round-corner border around the whole UI widget is also given by a shape drawable:
1 2 3 4 5 6
In that case, since we only care about borders, we set the filling color of the shape to be transparent. The
<stroke/> element define your borders while the
<corners/> element make them rounded.
As a final treat, if you combine things, you can also make fancy header using shape:
Shape XML (notice the padding left value for indenting):
1 2 3 4 5 6 7 8 9 10
TextView XML (as always, shape registered in
1 2 3 4 5 6
Now go add a bit of “woowoo” to your app!