Login     Sign Up
Cyril Sermon (@admin)
10 months ago

Themes are an excellent way to ensure consistency for your application’s UI. Rather than fully define each style, Android provides a shortcut to let you use styles from the currently applied theme.

To do this, you use ?android: rather than @ as a prefix to the resource you want to use. The following example shows a snippet of the above code but uses the current theme’s text color rather than an exter-nal resource:








This technique lets you create styles that will change if the current theme changes, without having to modify each individual style resource.

To-Do List Resources Example

In this example, you’ll create new external resources in preparation for adding functionality to the To-Do List example you started in Chapter 2. The string and image resources you create here will be used in Chapter 4 when you implement a menu system for the To-Do List application.

The following steps will show you how to create text and icon resources to use for the add and remove menu items, and how to create a theme to apply to the application:

Create two new PNG images to represent adding, and removing, a to-do list item. Each image should have dimensions of approximately 16 × 16 pixels, like those illustrated in Figure 3-5.

Copy the images into your project’s res/drawable folder, and refresh your project. Your proj-ect hierarchy should appear as shown in Figure 3-6.

Open the strings.xml resource from the res/values folder, and add values for the “add_new,” “remove,” and “cancel” menu items. (You can remove the default “hello” string value while you’re there.)

<?xml version=”1.0” encoding=”utf-8”?>


<string name=”app_name”>To Do List</string>

<string name=”add_new”>Add New Item</string>

<string name=”remove”>Remove Item</string>

<string name=”cancel”>Cancel</string>


Create a new theme for the application by creating a new styles.xml resource in the res/values folder. Base your theme on the standard Android theme, but set values for a default text size.

<?xml version=”1.0” encoding=”utf-8”?>


<style name=”ToDoTheme” parent=”@android:style/Theme.Black”> <item name=”android:textSize”>12sp</item>



Apply the theme to your project in the manifest:

<activity android:name=”.ToDoList”