The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
As noted in an earlier chapter, Android offers styles and themes, filling the same sort of role that CSS does in Web development. In that earlier chapter, we covered the basic roles of styles and themes, plus introduced the three classic theme families:
In this chapter, we will take a slightly “deeper dive” into styles and themes, exploring how you can create your own and apply them to your app’s UI.
The purpose of styles is to encapsulate a set of attributes that you intend to use repeatedly, conditionally, or otherwise wish to keep separate from your layouts proper. The primary use case is “don’t repeat yourself” (DRY) — if you have a bunch of widgets that look the same, use a style to use a single definition for “look the same”, rather than copying the look from widget to widget.
And that paragraph will make a bit more sense if we look at an example,
Styles/NowStyled sample project.
This is a trivial project,
with a full-screen button that shows the date and time of when the activity was
launched or when the button was pushed. This time, though, we want to change
the way the text on the face of the button appears, and we will do so using a
res/layout/main.xml file in this project has a
style attribute on
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button" android:text="" android:layout_width="match_parent" android:layout_height="match_parent" style="@style/bigred" />
Note that the
style attribute is part of stock XML and therefore is not in
android namespace, so it does not get the
@style/bigred, points to a style resource. Style resources are
values resources and can be found in the
res/values/ directory in your
project, or in other resource sets (e.g.,
res/values-v11/ for values
resources only to be used on API Level 11 or higher). The convention is for
style resources to be held in a
styles.xml file, such as the one from the
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="bigred"> <item name="android:textSize">30sp</item> <item name="android:textColor">#FFFF0000</item> </style> </resources>
<style> element supplies the name of the style, which is what we use when
referring to the style from a layout. The
<item> children of the
element represent values of attributes to be applied to whatever the style is
applied towards — in our example, our
Button widget. So, our
will have a comparatively large font (
android:textSize set to
have the text appear in red (
android:textColor set to
Just defining the style and applying it to the widget gives us the desired results:
Figure 171: The Styles/NowStyled sample application
The preview of this section was stepped on by Godzilla.
The preview of this section was abducted by space aliens.
The preview of this section is in the process of being translated from its native Klingon.
The preview of this section was whisked away by a shark-infested tornado.