When designing your application, you can use styles and themes to apply uniform formatting to its various screens and UI elements.
- A style is a set of one or more formatting attributes that you can apply as a unit to single elements in your layout XML file(s). For example, you could define a style that specifies a certain text size and color, then apply it to instances of a certain type of View element.
- A theme is a set of one or more formatting attributes that you can apply as a unit to all activities in an application or just a single activity. For example, you could define a theme that sets specific colors for the window frame and the panel foreground and background, and sets text sizes and colors for menus, then apply it to the activities of your application.
Styles and themes are resources. Android provides some default style and theme resources that you can use, or you can declare your own custom style and theme resources.
To create custom styles and themes:
- Create a file named
styles.xml
in the your application's res/values
directory. Add a root <resources>
node.
- For each style or theme, add a
<style>
element with a unique name
and, optionally, a parent
attribute.
The name is used for referencing these styles later, and the parent indicates what style resource to inherit from.
- Inside the
<style>
element, declare format values in one or more <item>
element(s).
Each <item>
identifies its style property with a name
attribute and defines its style value inside the element.
- You can then reference the custom resources from other XML resources, your manifest or application code.
Styles
Here's an example declaration of a style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SpecialText" parent="@style/Text">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
As shown, you can use <item>
elements to set specific formatting values for the style.
The name
attribute in the item
can refer to a standard string, a hex color value,
or a reference to any other resource type.
Notice the parent
attribute in the <style>
element. This attribute lets you specify a resource from which the current style will inherit values. The style can inherit from any type of resource that contains the style(s) you want. In general, your styles should always inherit (directly or indirectly) from a standard Android style resource. This way, you only have to define the values that you want to change.
Here's how you would reference the custom style from an XML layout, in this case, for an EditText element:
<EditText id="@+id/text1"
style="@style/SpecialText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
Now this EditText widget will be styled as defined by the XML example above.
Themes
Just like styles, themes are also declared in XML <style>
elements, and are referenced in the same manner.
The difference is that you add a theme to an entire application or activity, via the <application>
and <activity>
elements in the Android Manifest —
themes cannot be applied to individual Views.
Here's an example declaration of a theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowFrame">@drawable/screen_frame</item>
<item name="windowBackground">@drawable/screen_background_white</item>
<item name="panelForegroundColor">#FF000000</item>
<item name="panelBackgroundColor">#FFFFFFFF</item>
<item name="panelTextColor">?panelForegroundColor</item>
<item name="panelTextSize">14</item>
<item name="menuItemTextColor">?panelTextColor</item>
<item name="menuItemTextSize">?panelTextSize</item>
</style>
</resources>
Notice the use of the at-symbol (@) and the question-mark (?) to reference resources.
The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from
this project or from the Android framework).
The question-mark indicates that we're referencing a resource value in the currently loaded theme. This
is done by referring to a specific <item>
by its name
value. (E.g.,
panelTextColor uses the same color assigned to panelForegroundColor, defined beforehand.)
This technique can be used only in XML resources.
Set the theme in the manifest
To set this theme for all the activites of your application, open the AndroidManifest.xml file and
edit the <application>
tag to include the android:theme
attribute with the
theme name:
<application android:theme="@style/CustomTheme">
If you want the theme applied to just one Activity in your application, then add the theme
attribute to the <activity>
tag, instead.
Just as Android provides other built-in resources, there are several themes that you swap in
without having to write one yourself. For example, you can use the Dialog theme to make your Activity
appear like a dialog box. In the manifest, reference an Android theme like so:
<activity android:theme="@android:style/Theme.Dialog">
If you like a theme, but want to slightly tweak it, just add the theme as the parent
of your custom theme.
For example, we'll modify the Theme.Dialog
theme. To do so, create a style
with Theme.Dialog
as the parent:
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
There it is. We've inherited the Android Dialog theme so we can adjust its styles as we like.
So, for each item in the Dialog theme that we want to change, we re-define the value here and
use CustomDialogTheme instead of Theme.Dialog inside the Android Manifest.
Set the theme from the application
You can also load a theme for an Activity programmatically, if needed.
To do so, use the setTheme()
method. Note that, when doing so, you must be sure to set the theme before
instantiating any Views in the context, for example, before calling
setContentView(View)
or inflate(int, ViewGroup)
. This ensures that
the system applies the same theme for all of your UI screens. Here's an example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.linear_layout_3);
}
If you are considering loading a theme programmatically for the main
screen of your application, note that the theme would not be applied
in any animations the system would use to start the activity, which
would take place before your application opens. In most cases, if
you want to apply a theme to your main screen, doing so in XML
is a better approach.
For detailed information about custom styles and themes and referencing them from your application, see
Available Resource Types:
Style and Themes.
For information about default themes and styles available, see R.style.