Sans Pareil Technologies, Inc.

Key To Your Business

Lesson 4


Introduction



A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font colour, font size, background colour, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

A theme is a style applied to an entire Activity or application, rather than an individual View. When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont style as a theme for an Activity and then all text inside that Activity will have green monospace font.

Styles



To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ directory.

The root node of the XML file must be <resources>

For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the <item> can be a keyword string, a hex colour, a reference to another resource type, or other value depending on the style property. 

Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute.

The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.

Remember, a style that you want to use as an Activity or application theme is defined in XML exactly the same as a style for a View. A style can be applied as a style for a single View or as a theme for an entire Activity or application.

Inheritance



The parent attribute in the <style> element lets you specify a style from which your style should inherit properties. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform. For example, you can inherit the Android platform's default text appearance and then modify it:



If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example if you have a style named CodeFont as defined below, and wish to create a variant that uses red colour text, you can achieve that as follows:



Notice that there is no parent attribute in the <style> tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red.

You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:



This inherits from both CodeFont and CodeFont.Red styles, then adds the android:textSize property.

Note: This technique for inheritance by chaining together names only works for styles defined by your own resources. You cannot inherit Android built-in styles this way. To reference a built-in style, such as TextAppearance, you must use the parent attribute.

Style Properties



The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributes can be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an <EditText> element, you can instead create a style for the EditText element that includes this property.



Your XML for the layout can now implement this style:



For a reference of all available style properties, see the R.attr reference. Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specific View class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.

Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View. For example, style properties for a theme can hide the application title, hide the status bar, or change the window's background. These kind of style properties do not belong to any View object. To discover these theme-only style properties, look at the R.attr reference for attributes that begin with window. For instance, windowNoTitle and windowBackground are style properties that are effective only when the style is applied as a theme to an Activity or application.

Applying Styles and Themes



There are two ways to set a style:
  • To an individual View, by adding the style attribute to a View element in the XML for your layout.
  • Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.

When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.

To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every Viewwithin the Activity or application will apply each property that it supports.

Apply a style to a View



Here's how to set a style for a View in the XML layout:


Now this TextView will be styled as defined by the style named CodeFont.

Note: The style attribute does not use the android: namespace prefix.

Apply a theme to an Activity or application



To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name.



If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead.

Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:



Or if you want the background to be transparent, use the Translucent theme:



If you want to customise an existing theme, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own colour

#b0b0ff
Note: The colour needs to supplied as a separate resource here because the android:windowBackground attribute only supports a reference to another resource; unlike android:colorBackground, it can not be given a colour literal.
Now use CustomTheme instead of Theme.Light inside the Android Manifest:


Select a theme based on platform version



Newer versions of Android have additional themes available to applications, and you might want to use these while running on those platforms while still being compatible with older versions. You can accomplish this through a custom theme that uses resource selection to switch between different parent themes, based on the platform version.

For example, here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under res/values (typically res/values/styles.xml):



To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file in res/values-v11, but make the parent theme the holographic theme:



Now use this theme like you would any other, and your application will automatically switch to the holographic theme if running on Android 3.0 or higher.

Using Platform Styles and Themes



The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "@android:style/Theme.NoTitleBar".

The R.style reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code: