Android styling tips

One area that has always been a bit lacking for Android developers is the online documentation at developer.android.com. It’s getting better with each new release but is still far from perfect – one area which has left many developers lost is styling. There are now plenty of guides on the internet discussing styling and theming but below are the two tips I wish someone had told me when I’d started Android development:

1, ‘?’
The question mark in a style refers to a dynamically defined theme value. In the following example, taken directly from the Android styles.xml file, the value of ‘android:textColor’ refers to the value of the dynamically defined “textColorPrimary” value:

<style name="TextAppearance">
<item name="android:textColor">?textColorPrimary</item>
<item name="android:textColorHighlight">?textColorHighlight</item>
<item name="android:textColorHint">?textColorHint</item>
<item name="android:textColorLink">?textColorLink</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
</style>

As can be seen from the following excerpt from the android-16 themes.xml file, the exact value of the ‘textColorPrimary’ within ‘TextAppearance’, and hence the default text colour throughout the app, will vary depending on the theme applied on your application or activity in the AndroidManifest.xml file:

<style name="Theme.Light">
...
<item name="textColorPrimary">@android:color/primary_text_light</item>
...
</style>
<style name="Theme.Holo">
...
<item name="textColorPrimary">@android:color/primary_text_holo_dark</item>
...
</style>

So if we were to define our own theme and wanted to change the default text colour – we can easily override the above value in order to achieve that throughout all parts of our application which used our custom theme:

<style name="CustomTheme">
...
<item name="android:textColorPrimary">@color/custom_primary_colour</item>
...
</style>

2, Read the source code
This is a general tip really, but enough emphasis can’t be placed on how much you will learn by reading the Android source code, which can be downloaded via the SDK Manager. To find the style and theme code, find where you installed the Android SDK and have a look at styles.xml and themes.xml in the <android-sdk>/platforms/android-XX/data/res/values/ directory.

Really! You’ll learn more by looking at, studying, and playing about with the Android style and theme source code than any guide can ever teach you! For example, there’s no comprehensive documentation of what styles are available to change, but have a look at the default theme (search for ‘<style name=”Theme”>’ in the theme.xml file) and you’ll find out how to style every single widget in Android and lots more. This is the best form of guide and documentation rolled in to one – it’s always going to accurately reflect the live code and every single bit of style information is in there. Only the experience of using it will help you get a deeper knowledge of how it works. So…what are you waiting for?

Leave a Reply