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. For example:
<application android:theme="@style/CustomTheme">
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:
<activity android:theme="@android:style/Theme.Dialog">
Or if you want the background to be transparent, use the Translucent theme:
<activity android:theme="@android:style/Theme.Translucent">
If you like a theme, but want to tweak it, just add the theme as the parent
of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color> <style name="CustomTheme" parent="android:Theme.Light"> <item name="android:windowBackground">@color/custom_theme_color</item> <item name="android:colorBackground">@color/custom_theme_color</item> </style>
(Note that the color 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 color literal.)
Now use CustomTheme
instead of Theme.Light
inside the Android Manifest:
<activity android:theme="@style/CustomTheme">
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
):
<style name="LightThemeSelector" parent="android:Theme.Light"> ... </style>
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:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light"> ... </style>
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.
A list of the standard attributes that you can use in themes can be found at R.styleable.Theme
.
For more information about providing alternative resources, such as themes and layouts, based on the platform version or other device configurations, see the Providing Resources document.