themes.jd revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1page.title=Applying Styles and Themes
2parent.title=User Interface
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8  <h2>In this document</h2>
9  <ol>
10    <li><a href="#styles">Styles</a></li>
11    <li><a href="#themes">Themes</a>
12      <ol>
13        <li><a href="#inTheManifest">Set the theme in the manifest</a></li>
14        <li><a href="#fromTheApp">Set the theme from the application</a></li>
15      </ol>
16    </li>
17  </ol>
18  <h2>See also</h2>
19  <ol>
20    <li><a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style and Theme Resources</a></li>
21  </ol>
22</div>
23</div>
24
25<p>When designing your application, you can use <em>styles</em> and <em>themes</em> to apply uniform formatting to its various screens and UI elements.
26
27<ul>
28  <li>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.</li>
29  <li>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.</li>
30</ul>
31
32<p>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.</p>
33
34<p>To create custom styles and themes:</p>
35<ol>
36  <li>Create a file named <code>styles.xml</code> in the your application's <code>res/values</code> directory. Add a root <code>&lt;resources></code> node.</li>
37  <li>For each style or theme, add a <code>&lt;style&gt;</code> element with a unique <code>name</code> and, optionally, a <code>parent</code> attribute.
38    The name is used for referencing these styles later, and the parent indicates what style resource to inherit from.</li>
39  <li>Inside the <code>&lt;style></code> element, declare format values in one or more <code>&lt;item&gt;</code> element(s). 
40    Each <code>&lt;item&gt;</code> identifies its style property with a <code>name</code> attribute and defines its style value inside the element.</li>
41  <li>You can then reference the custom resources from other XML resources, your manifest or application code.</li>
42</ol>
43
44
45<h2 id="styles">Styles</h2>
46
47<p>Here's an example declaration of a style: </p>
48
49<pre>
50&lt;?xml version="1.0" encoding="utf-8"?&gt;
51&lt;resources&gt;
52    &lt;style name="SpecialText" parent="@style/Text"&gt;
53        &lt;item name="android:textSize"&gt;18sp&lt;/item&gt;
54        &lt;item name="android:textColor"&gt;#008&lt;/item&gt;
55    &lt;/style&gt;
56&lt;/resources&gt;
57</pre>
58
59<p>As shown, you can use <code>&lt;item&gt;</code> elements to set specific formatting values for the style. 
60The <code>name</code> attribute in the <code>item</code> can refer to a standard string, a hex color value, 
61or a reference to any other resource type.</p>
62
63<p>Notice the <code>parent</code> attribute in the <code>&lt;style></code> 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.</p>
64
65<p>Here's how you would reference the custom style from an XML layout, in this case, for an EditText element:</p>
66
67<pre>
68&lt;EditText id="@+id/text1"
69          style="@style/SpecialText"
70          android:layout_width="fill_parent"
71          android:layout_height="wrap_content"
72          android:text="Hello, World!" /&gt;
73</pre>
74
75<p>Now this EditText widget will be styled as defined by the XML example above.</p>
76
77
78<h2 id="themes">Themes</h2>
79
80<p>Just like styles, themes are also declared in XML <code>&lt;style&gt;</code> elements, and are referenced in the same manner.
81The difference is that you add a theme to an entire application or activity, via the <code>&lt;application&gt;</code> 
82and <code>&lt;activity&gt;</code> elements in the Android Manifest &mdash;
83themes cannot be applied to individual Views.</p>
84
85<p>Here's an example declaration of a theme:</p>
86
87<pre>
88&lt;?xml version="1.0" encoding="utf-8"?&gt;
89&lt;resources&gt;
90  &lt;style name="CustomTheme"&gt;        
91    &lt;item name="android:windowNoTitle">true&lt;/item>
92    &lt;item name="windowFrame"&gt;@drawable/screen_frame&lt;/item&gt;
93    &lt;item name="windowBackground"&gt;@drawable/screen_background_white&lt;/item&gt;
94    &lt;item name="panelForegroundColor"&gt;#FF000000&lt;/item&gt;
95    &lt;item name="panelBackgroundColor"&gt;#FFFFFFFF&lt;/item&gt;
96    &lt;item name="panelTextColor"&gt;?panelForegroundColor&lt;/item&gt;
97    &lt;item name="panelTextSize"&gt;14&lt;/item&gt;
98    &lt;item name="menuItemTextColor"&gt;?panelTextColor&lt;/item&gt;
99    &lt;item name="menuItemTextSize"&gt;?panelTextSize&lt;/item&gt;
100  &lt;/style&gt;
101&lt;/resources>
102</pre>
103
104<p>Notice the use of the at-symbol (@) and the question-mark (?) to reference resources.
105The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from
106this project or from the Android framework).
107The question-mark indicates that we're referencing a resource value in the currently loaded theme. This
108is done by referring to a specific <code>&lt;item></code> by its <code>name</code> value. (E.g., 
109<em>panelTextColor</em> uses the same color assigned to <em>panelForegroundColor</em>, defined beforehand.)
110This technique can be used only in XML resources.
111</p>
112
113<h3 id="inTheManifest">Set the theme in the manifest</h3>
114<p>To set this theme for all the activites of your application, open the AndroidManifest.xml file and 
115edit the <code>&lt;application></code> tag to include the <code>android:theme</code> attribute with the 
116theme name:</p>
117
118<pre>
119&lt;application android:theme="@style/CustomTheme">
120</pre>
121
122<p>If you want the theme applied to just one Activity in your application, then add the theme
123attribute to the <code>&lt;activity></code> tag, instead.</p>
124
125<p>Just as Android provides other built-in resources, there are several themes that you swap in
126without having to write one yourself. For example, you can use the Dialog theme to make your Activity 
127appear like a dialog box. In the manifest, reference an Android theme like so:</p>
128
129<pre>
130&lt;activity android:theme="@android:style/Theme.Dialog">
131</pre>
132
133<p>If you like a theme, but want to slightly tweak it, just add the theme as the <code>parent</code> of your custom theme.
134For example, we'll modify the <code>Theme.Dialog</code> theme. To do so, create a style
135with <code>Theme.Dialog</code> as the parent:</p>
136<pre>
137&lt;style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
138</pre>
139<p>There it is. We've inherited the Android Dialog theme so we can adjust its styles as we like. 
140So, for each item in the Dialog theme that we want to change, we re-define the value here and 
141use <var>CustomDialogTheme</var> instead of <var>Theme.Dialog</var> inside the Android Manifest.</p>
142
143<h3 id="fromTheApp">Set the theme from the application</h3>
144<p>You can also load a theme for an Activity programmatically, if needed. 
145To do so, use the {@link android.app.Activity#setTheme(int) setTheme()} 
146method. Note that, when doing so, you must be sure to set the theme <em>before</em> 
147instantiating any Views in the context, for example, before calling 
148<code>setContentView(View)</code> or <code>inflate(int, ViewGroup)</code>. This ensures that 
149the system applies the same theme for all of your UI screens. Here's an example:</p>
150
151<pre>
152 protected void onCreate(Bundle savedInstanceState) {
153    super.onCreate(savedInstanceState);
154    ...
155    setTheme(android.R.style.Theme_Light);
156    setContentView(R.layout.linear_layout_3);
157}
158</pre>
159
160<p>If you are considering loading a theme programmatically for the main
161screen of your application, note that the theme would not be applied
162in any animations the system would use to start the activity, which
163would take place before your application opens. In most cases, if 
164you want to apply a theme to your main screen, doing so in XML
165 is a better approach. </p>
166
167<p>For detailed information about custom styles and themes and referencing them from your application, see
168<a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Available Resource Types:
169Style and Themes</a>.</p>
170
171<p>For information about default themes and styles available, see {@link android.R.style}.</p>
172
173
174
175
176
177
178