Tutorials Legacy ▶ Hands On

Android themes and stylesThe Android SDK comes complete with impressive options to customise the appearance of its standard UI components. This technique can give your applications a polished and professional look without having to rely on custom drawing code. Your app can be instantly skinned with a makeover which changes every part of its appearance. This isn't just for cosmetic reasons either - skinning can greatly assist internationalisation, or how about support for high-contrast UI's for the visually impaired.

All this is achieved using Androids Themes component - it's explained in detail by Google here. This article gives a quick feel for the powerful Themes techniques Android provides, with a few tips on real-world use along the way.

Why Bother with Themes?

Using themes allows a developer to concentrate on the raw functionality of the application being written using the standard components in the Android tool kit. The content of the themes and styles are nicely separated from the functional part of the application - this allows us to create a library of themes that can be easily applied to any part of an application. In the same way a theme created for one application can be used without much modification on another application, which enables a uniform look and style across a suite of applications.

One alternative to using themes would be to subclass views to modify the look of the components by substituting drawing code and attributes - an approach often used in other mobile development environments. Another approach would be to insert the attributes such as colours and images into the layouts directly. There are a good number of reasons why this solution begins to break down as the application becomes more complex. Using themes addresses all of these concerns.

  • Using themes means that wherever possible the UI is drawn using the built in components. This is good because then we don't need to worry about the forward compatibility issues that you would with custom drawing code. It reduces then amount of code required and the library code will invariably be better written and optimised.
  • Each time a new look and feel needs to be applied to an application themes allow you to do so by just supplying a new theme rather than branching the code (either layout or Java code)
  • If you're lucky enough to get input from a graphic designer, when she tells you to turn you layout on its head you can do so without touching any of your precious application code. Making changes to the style components is fast, safe and in the same language as the creative types speak in.
  • Localisation of style resources becomes much easier using themes, its possible to dynamically change images with language and orientation of shadows, rather than having to use separate builds for different countries

There is a little overhead in using themes in that it requires some thought as to the structure of the applications layouts to allow the theme to be applied and it can be a little difficult to work out where the resources for a particular component as there are a few layers of indirection to go through before you get to the content of the theme, but the advantages of using themes very quickly compensate for these small inconveniences.