Producing a top-selling Android app requires more than great functionality. The app must look good too. Any professional Android developer will tell you that achieving this requires a great deal of time and effort. In this post, I want to look at just one apparently simple factor in creating the visuals which is the appearance of buttons. It’s easy to add a button to a view in the Android resource editor but the default appearance is uninspiring to say the least!
There are various approaches to creating visually appealing buttons perhaps the most obvious being to create an image as the background. Android provided a mechanism for creating stretchable background images known as 9-patch images. Called 9-patch as they break the image into 9 separate patches with the central area being allowed to stretch to accommodate content. This technique allows a single background image to be used for all buttons regardless of their size. I should say at this point though that you should provide images in at least three different resolutions in order to render correctly on different device sizes!
In many cases, the complexity of using 9-patch images is not required. One of the beauties of the Android resource system is that the background of any view (buttons are views), can be a Drawable. Drawables can be images, colors, transitions, shapes and a variety of other powerful sources. It is often possible to create a really nice looking background images just by using Shape Drawables. Shape Drawables are XML declarations of a graphical shape which is interpreted at run-time by the Android system and rendered as a graphic. From the developers perspective, they are a simple way to create clean, scalable backgrounds to any view component (including the buttons I set out to discuss).
Using shapes is really very simple. First, we define the shape drawable in res/drawables:
<corners android:radius="6dp" />
<gradient android:angle="270" android:endColor="#30196E" android:startColor=#2A7FCE" />
<stroke android:width="1px" android:color="#050875" />
Then in a layout (res/layout/mylayout.xml) include the button definition with the android:background attribute referencing the Drawable.
The result is shown below. You can of course use whatever combination of gradients, strokes and shapes you wish to produce the desired image. In the same project, I also used a Shape Drawable to create a background image for list items.
For me, as someone who normally sub-contracts graphics work, one of the wonderful things about using Shape Drawables is that I can experiment really quickly.