Pin Me

Guide to Custom Themes for Android Apps

written by: hardiks•edited by: Simon Hill•updated: 7/5/2011

A quick guide to learn how to create custom themes and apply them to your Google Android applications.

  • slide 1 of 1

    A theme can be used to apply uniform formatting and styles to your whole application, or to certain activities in your application. Creating themes is very similar to creating styles for your Android application; the only difference is that you can apply styles to view elements, while you can apply themes to your whole application or a set of activities. It is analogous to the CSS styles you apply to your web pages. Using custom themes can give your application a great look and help to differentiate it from the scores of other applications out there.

    To create a custom theme, first of all, create a file named styles.xml and copy it to your application's res/values directory.

    Edit the styles.xml file and add a root node - <resources>. All your theme elements will be nested in the <resources> </resources> nodes.

    Now to specify the style of the theme, just add a <style> element inside which you can specify all your item styles. The <style> element has a single attribute -

    name - which specifies the name of the theme

    Example:

    <resources>

    <style name=" MyTheme ">

    <item name=" ... "> ... </item>

    <item name=" ... "> ... </item>

    </style>

    </resources>

    This is the basic structure of your theme. In the style element, you can use many item elements which define the look of each item.

    The item tags have an attribute name which identifies the item to which the style is applied. The values are written inside the tags. You can also refer to previous item values and copy them by writing ?itemname instead of the value.

    Example:

    <item name="panelForegroundColor"> #FFFFFFFF </item>

    <item name="panelBackgroundColor"> ?panelForegroundColor </item>

    <item name="panelTextSize"> 14 </item>

    Here's an example of the whole theme file:

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

    <resources>

    <style name=" MyTheme ">

    <item name="panelForegroundColor"> #FFFFFFFF </item>

    <item name="panelBackgroundColor"> ?panelForegroundColor </item>

    <item name="panelTextSize"> 14 </item>

    <item name="menuItemTextColor"> ?panelTextColor </item>

    <item name="menuItemTextSize"> ?panelTextSize </item>

    </style>

    </resources>

    Now that the theme is created, you can apply it to your application by editing the AndroidManifest.xml file of your application and adding the android:theme attribute to the application tag with its attribute as the theme's name you specified earlier.

    Example:

    <application android:theme="@style/MyTheme">

    If you want to apply it to a single activity and not the whole application, edit the activity tag to add its attribute.

    <activity android:theme="@style/MyTheme">