Creating Consistency with Android Styles

When first developing an Android application, nearly everyone ends up with a rather dull looking application with a black background and white text. Whilst this is great as proof of concept, it’s not going to keep the guys from marketing happy! A quick look at the apps on Google Play shows that there must be a good way of changing the visual appearance of applications.

It is indeed fairly straightforward to set the style for any Android view component. The example below shows a TextView being styled to set the text size, colour and font face.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:textSize="20sp"
android:typeface="sans"

android:text="@string/some text" />

The problem with this approach is that every single control on every single view must be styled in the same way to create a consistent look and feel for your application. That is a lot of code to maintain and a potential nightmare when someone from marketing decides to change the look and feel of the app. The solution to the problem lies in defining styles and then using them control the appearance of controls throughout the application.

Declaring styles is easy with the Android resource system: just create a new resource file ./res/values/styles.xml and declare the styles you need. As you can see below, you simply declare a named style and create declarations of each style property as required.


<resources>
<style name="myTextStyle">
<item name="android:textColor">#00FF00</item>
<item name="android:textSize">20sp</item>
<item name="android:typeface">sans</item>
</style>
</resources>

All you then need to do is to modify the declaration of the view to use the new style.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:style="@style/myTextStyle"
android:text="@string/some text" />

I could have included the layout_width and layout_height attributes in the style declaration as well but generally I like to keep the styling attributes separate from those defining the layout structure.

At this stage, we have defined a reusable style in such a way that any change to the style declaration will automatically be reflected in any view which uses the android:style attribute to reference it.

It’s a powerful technique but still requires that we reference the style in the declaration of every single widget in our application. The solution to that  problem is to apply the style to an Activity or even the entire application via an entry in the manifest. To do that properly requires an understanding of Android’s themes and how to extend them. I’ll come back to that another time.

Learn more about Android in Learning Tree’s course, Developing Applications for Android™ Devices. Check it out!

Mike Way

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.