themes.jd revision 600a75c31f0bb0993b815a17b7e22263f9ffb3b3
1page.title=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="#DefiningStyles">Defining Styles</a>
11      <ol>
12        <li><a href="#Inheritance">Inheritance</a></li>
13        <li><a href="#Properties">Style Properties</a></li>
14      </ol>
15    </li>
16    <li><a href="#ApplyingStyles">Applying Styles and Themes to the UI</a>
17      <ol>
18        <li><a href="#ApplyAStyle">Apply a style to a View</a></li>
19        <li><a href="#ApplyATheme">Apply a theme to an Activity or application</a></li>
20        <li><a href="#SelectATheme">Select a theme based on platform version</a></li>
21      </ol>
22    </li>
23    <li><a href="#PlatformStyles">Using Platform Styles and Themes</a></li>
24  </ol>
25  <h2>See also</h2>
26  <ol>
27    <li><a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
28    and Theme Resources</a></li>
29    <li>{@link android.R.style} for Android styles and themes</li>
30    <li>{@link android.R.attr} for all style attributes</li>
31  </ol>
32</div>
33</div>
34
35
36<p>A <strong>style</strong> is a collection of properties that
37specify the look and format for a {@link android.view.View} or window.
38A style can specify properties such as height, padding, font color, font size,
39background color, and much more. A style is defined in an XML resource that is
40separate from the XML that specifies the layout.</p>
41
42<p>Styles in Android share a similar philosophy to cascading stylesheets in web
43design&mdash;they allow you to separate the design from the
44content.</p>
45
46<p>For example, by using a style, you can take this layout XML:</p>
47<pre>
48&lt;TextView
49    android:layout_width="fill_parent"
50    android:layout_height="wrap_content"
51    android:textColor="#00FF00"
52    android:typeface="monospace"
53    android:text="@string/hello" />
54</pre>
55<p>And turn it into this:</p>
56<pre>
57&lt;TextView
58    style="@style/CodeFont"
59    android:text="@string/hello" />
60</pre>
61
62<p>All of the attributes related to style have been removed from the layout XML and put into a
63style definition called {@code CodeFont}, which is then applied with the <code>style</code>
64attribute. You'll see the definition for this style in the following section.</p>
65
66<p>A <strong>theme</strong> is a style applied to an entire {@link android.app.Activity} or
67application, rather than an individual {@link android.view.View} (as in the example above). When a
68style is applied as a theme, every View in the Activity or application will apply each style
69property that it supports. For example, you can apply the same {@code CodeFont} style
70as a theme for an Activity and then all text inside that Activity will have green monospace
71font.</p>
72
73
74<h2 id="DefiningStyles">Defining Styles</h2>
75
76<p>To create a set of styles, save an XML file in the {@code res/values/}
77directory of your project. The name of the XML file is arbitrary, but it must use the
78{@code .xml} extension and be saved in the {@code res/values/} folder.</p>
79
80<p>The root node of the XML file must be {@code &lt;resources&gt;}.</p>
81
82<p>For each style you want to create, add a {@code &lt;style>} element to the file
83with a {@code name} that uniquely identifies the style (this attribute is required).
84Then add an {@code &lt;item>} element for each property of that style, with a
85{@code name} that declares the style property and a value to go with it (this attribute
86is required). The value for the {@code &lt;item>} can
87be a keyword string, a hex color, a reference to another resource type, or other value
88depending on the style property.
89Here's an example file with a single style:</p>
90
91<pre>
92&lt;?xml version="1.0" encoding="utf-8"?>
93&lt;resources&gt;
94    &lt;style name="CodeFont" parent="@android:style/TextAppearance.Medium"&gt;
95        &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt;
96        &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;
97        &lt;item name="android:textColor"&gt;#00FF00&lt;/item&gt;
98        &lt;item name="android:typeface"&gt;monospace&lt;/item&gt;
99    &lt;/style&gt;
100&lt;/resources&gt;
101</pre>
102
103<p>Each child of the {@code &lt;resources>} element is converted into an application resource
104object at compile-time, which can be referenced by the value in the {@code &lt;style>} element's
105{@code name} attribute. This example style can be referenced from an XML layout as
106{@code @style/CodeFont} (as demonstrated in the introduction above).</p>
107
108<p>The <code>parent</code> attribute in the {@code &lt;style>} element is optional and
109specifies the resource ID of another style from which this style should inherit
110properties. You can then override the inherited style properties if you want to.</p>
111
112<p>Remember, a style that you want to use as an Activity or application theme is defined in XML
113exactly the same as a style for a View. A style such as the one defined above can be applied as a
114style for a single View or as a theme for an entire Activity or application. How to apply a style
115for a single View or as an application theme is discussed later.</p>
116
117
118<h3 id="Inheritance">Inheritance</h3>
119
120<p>The {@code parent} attribute in the {@code &lt;style>} element lets you specify a style
121from which your style should inherit properties.
122You can use this to inherit properties from an existing style and
123then define only the properties that you want to change or add. You can
124inherit from styles that you've created yourself or from styles that are built into the
125platform. (See <a href="#PlatformStyles">Using Platform Styles and Themes</a>, below, for
126information about inheriting from styles defined by the Android platform.) For example, you can
127inherit the Android platform's default text appearance and then modify it:</p>
128
129<pre>
130    &lt;style name="GreenText" parent="@android:style/TextAppearance"&gt;
131        &lt;item name="android:textColor"&gt;#00FF00&lt;/item&gt;
132    &lt;/style&gt;
133</pre>
134
135<p>If you want to inherit from styles that you've defined yourself, you <em>do not</em> have to use
136the <code>parent</code> attribute. Instead, just prefix the name of the style you want to
137inherit to the name of your new style, separated by a period. For example, to create a new style
138that inherits the <code>CodeFont</code> style defined above, but make the color red,
139you can author the new style like this:</p>
140
141<pre>
142    &lt;style name="CodeFont.Red"&gt;
143        &lt;item name="android:textColor"&gt;#FF0000&lt;/item&gt;
144    &lt;/style&gt;
145</pre>
146
147<p>Notice that there is no {@code parent} attribute in the {@code &lt;style&gt;} tag, but because
148the {@code name} attribute begins with the {@code CodeFont} style name (which
149is a style that you have created), this style inherits all style properties from that style. This
150style then overrides the {@code android:textColor} property to make the text red. You can
151reference this new style as {@code @style/CodeFont.Red}.</p>
152
153<p>You can continue inheriting like
154this as many times as you'd like, by chaining names with periods. For example, you can
155extend {@code CodeFont.Red} to be bigger, with:</p>
156<pre>
157    &lt;style name="CodeFont.Red.Big"&gt;
158        &lt;item name="android:textSize"&gt;30sp&lt;/item&gt;
159    &lt;/style&gt;
160</pre>
161<p>This inherits from both {@code CodeFont} and {@code CodeFont.Red} styles, then adds the
162{@code android:textSize} property.</p>
163
164<p class="note"><strong>Note:</strong> This technique for inheritance by chaining together
165names only works for styles defined by your own resources. You can't inherit Android built-in styles
166this way. To reference a built-in style, such as {@link android.R.style#TextAppearance}, you must
167use the {@code parent} attribute.</p>
168
169
170<h3 id="Properties">Style Properties</h3>
171
172<p>Now that you understand how a style is defined, you need to learn what kind
173of style properties&mdash;defined by the {@code &lt;item>} element&mdash;are available.
174You're probably familiar with some already, such as {@link android.R.attr#layout_width} and
175{@link android.R.attr#textColor}. Of course, there are many more style properties you can use.</p>
176
177<p>The best place to find properties that apply to a specific {@link android.view.View} is the
178corresponding class reference, which lists all of the supported XML attributes. For example, all of the
179attributes listed in the table of
180<a href="{@docRoot}reference/android/widget/TextView.html#lattrs">TextView XML 
181attributes</a> can be used in a style definition for a {@link android.widget.TextView} element (or one of
182its subclasses). One of the attributes listed in the reference is <a
183href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
184android:inputType}</a>, so where you might normally place the <a
185href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
186android:inputType}</a>
187attribute in an {@code &lt;EditText>} element, like this:</p>
188<pre>
189&lt;EditText
190    android:inputType="number"
191    ... />
192</pre>
193
194<p>You can instead create a style for the {@link android.widget.EditText} element that includes this property:</p>
195<pre>
196&lt;style name="Numbers">
197  &lt;item name="android:inputType">number&lt;/item>
198  ...
199&lt;/style>
200</pre>
201<p>So your XML for the layout can now implement this style:</p>
202<pre>
203&lt;EditText
204    style="@style/Numbers"
205    ... />
206</pre>
207
208<p>This simple example may look like more work, but when you add more style properties and
209factor-in the ability to re-use the style in various places, the pay-off can be huge.</p>
210
211<p>For a reference of all available style properties, see the {@link android.R.attr}
212reference. Keep in mind that all View objects don't accept all the same style attributes, so you
213should normally refer to the specific {@link android.view.View} class for supported style
214properties. However, if you
215apply a style to a View that does not support all of the style properties, the View will
216apply only those properties that are supported and simply ignore the others.</p>
217
218<p>Some style properties, however, are not supported by any View element and can only be applied
219as a theme. These style properties apply to the entire window and not to any type of View.
220For example, style properties for a theme can hide the application title, hide the status bar,
221or change the window's background. These kind of style properties do not belong to any View object.
222To discover these theme-only style properties, look at the {@link android.R.attr} reference for
223attributes that begin with {@code window}. For instance, {@code windowNoTitle} and {@code
224windowBackground} are style properties that are effective only when the style is applied as
225a theme to an Activity or application. See the next section for information about applying a
226style as a theme.</p>
227
228<p class="note"><strong>Note:</strong> Don't forget to prefix the property names in each
229{@code &lt;item&gt;} element with the <code>android:</code> namespace. For example:
230{@code &lt;item name="android:inputType">}.</p>
231
232
233
234<h2 id="ApplyingStyles">Applying Styles and Themes to the UI</h2>
235
236<p>There are two ways to set a style:</p>
237<ul>
238  <li>To an individual View, by adding the <code>style</code> attribute to a View
239  element in the XML for your layout.</li>
240  <li>Or, to an entire Activity or application, by adding the <code>android:theme</code>
241  attribute to the <code>&lt;activity></code> or <code>&lt;application></code> element
242  in the Android manifest.</li>
243</ul>
244
245<p>When you apply a style to a single {@link android.view.View} in the layout, the properties
246defined by the style are applied only to that {@link android.view.View}. If a style is applied to a
247{@link android.view.ViewGroup}, the child {@link android.view.View} elements will
248<strong>not</strong> inherit the style properties&mdash;only the element to which you directly apply
249the style will apply its properties. However, you <em>can</em> apply a style so that it
250applies to all {@link android.view.View} elements&mdash;by applying the style as a theme.</p>
251
252<p>To apply a style definition as a theme, you must apply the style to an
253{@link android.app.Activity} or application in the Android manifest. When you do so,
254every {@link android.view.View} within the Activity or
255application will apply each property that it supports. For example, if you apply the {@code
256CodeFont} style from the previous examples to an Activity, then all View elements
257that support the text style properties will apply them. Any View that does not support
258the properties will ignore them. If a View supports only some of the properties, then
259it will apply only those properties.</p>
260
261
262<h3 id="ApplyAStyle">Apply a style to a View</h3>
263
264<p>Here's how to set a style for a View in the XML layout:</p>
265
266<pre>
267&lt;TextView
268    style="@style/CodeFont"
269    android:text="@string/hello" />
270</pre>
271
272<p>Now this TextView will be styled as defined by the style named {@code CodeFont}.
273(See the sample above, in <a href="#DefiningStyles">Defining Styles</a>.)</p>
274
275<p class="note"><strong>Note:</strong> The <code>style</code> attribute
276does <em>not</em> use the <code>android:</code> namespace prefix.</p>
277
278
279<h3 id="ApplyATheme">Apply a theme to an Activity or application</h3>
280
281<p>To set a theme for all the activities of your application, open the {@code AndroidManifest.xml} file and
282edit the <code>&lt;application></code> tag to include the <code>android:theme</code> attribute with the 
283style name. For example:</p>
284
285<pre>
286&lt;application android:theme="@style/CustomTheme">
287</pre>
288
289<p>If you want a theme applied to just one Activity in your application, then add the 
290<code>android:theme</code> attribute to the <code>&lt;activity></code> tag instead.</p>
291
292<p>Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid
293writing them yourself. For example, you can use the {@code Dialog} theme and make your Activity
294appear like a dialog box:</p>
295
296<pre>
297&lt;activity android:theme="@android:style/Theme.Dialog">
298</pre>
299
300<p>Or if you want the background to be transparent, use the Translucent theme:</p>
301
302<pre>
303&lt;activity android:theme="@android:style/Theme.Translucent">
304</pre>
305
306<p>If you like a theme, but want to tweak it, just add the theme as the <code>parent</code>
307of your custom theme. For example, you can modify the traditional light theme to use your own
308color like this:</p>
309<pre>
310&lt;color name="custom_theme_color">#b0b0ff&lt;/color>
311&lt;style name="CustomTheme" parent="android:Theme.Light">
312    &lt;item name="android:windowBackground">@color/custom_theme_color&lt;/item>
313    &lt;item name="android:colorBackground">@color/custom_theme_color&lt;/item>
314&lt;/style>
315</pre>
316
317<p>(Note that the color needs to supplied as a separate resource here because
318the <code>android:windowBackground</code> attribute only supports a reference to
319another resource; unlike <code>android:colorBackground</code>, it can not be given
320a color literal.)</p>
321
322<p>Now use {@code CustomTheme} instead of {@code Theme.Light} inside the Android
323Manifest:</p>
324
325<pre>
326&lt;activity android:theme="@style/CustomTheme">
327</pre>
328
329
330<h3 id="SelectATheme">Select a theme based on platform version</h3>
331
332<p>Newer versions of Android have additional themes available to applications,
333and you might want to use these while running on those platforms while still being
334compatible with older versions.  You can accomplish this through a custom theme
335that uses resource selection to switch between different parent themes, based on the platform
336version.</p>
337
338<p>For example, here is the declaration for a custom theme which is simply
339the standard platforms default light theme.  It would go in an XML file under
340<code>res/values</code> (typically <code>res/values/styles.xml</code>):
341<pre>
342&lt;style name="LightThemeSelector" parent="android:Theme.Light">
343    ...
344&lt;/style>
345</pre>
346
347<p>To have this theme use the newer holographic theme when the application is running
348on Android 3.0 (API Level 11) or higher, you can place an alternative
349declaration for the theme in an XML file in <code>res/values-v11</code>, but make the parent theme
350the holographic theme:</p>
351<pre>
352&lt;style name="LightThemeSelector" parent="android:Theme.Holo.Light">
353    ...
354&lt;/style>
355</pre>
356
357<p>Now use this theme like you would any other, and your application will
358automatically switch to the holographic theme if running on Android 3.0 or higher.</p>
359
360<p>A list of the standard attributes that you can use in themes can be
361found at {@link android.R.styleable#Theme R.styleable.Theme}.</p>
362
363<p>For more information about providing alternative resources, such as themes and layouts, based
364on the platform version or other device configurations, see the <a
365href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resources</a>
366document.</p>
367
368<!-- This currently has some bugs
369
370<h3 id="setThemeFromTheApp">Set the theme from the application</h3>
371
372<p>We recommend that you set your themes in you Android manifest, as described above, because it's simple and
373keeps your program code focused on application functionality, rather than style. But if it's necessary
374for you to change your theme programatically (perhaps based on a user preference), you can.</p>
375
376<p>To set the theme in your program code, use the {@link android.content.ContextWrapper#setTheme(int)}
377method and pass it the theme resource ID. Note that, when doing so, you must be sure to set the theme <em>before</em> 
378instantiating any Views in the context, for example, before calling 
379<code>setContentView(View)</code> or <code>inflate(int, ViewGroup)</code>. This ensures that 
380the system applies the same theme for all of your UI screens. Here's an example:</p>
381
382<pre>
383 protected void onCreate(Bundle savedInstanceState) {
384    super.onCreate(savedInstanceState);
385    ...
386    setTheme(android.R.style.Theme_Light);
387    setContentView(R.layout.linear_layout_3);
388}
389</pre>
390
391<p>If you are considering loading a theme programmatically for the main
392screen of your application, note that the theme would not be applied
393in any animations the system would use to start the activity, which
394would take place before your application opens. In most cases, if 
395you want to apply a theme to your main screen, doing so in XML
396 is a better approach. </p>
397
398-->
399
400
401
402<h2 id="PlatformStyles">Using Platform Styles and Themes</h2>
403
404<p>The Android platform provides a large collection of styles and themes that you can
405use in your applications. You can find a reference of all available styles in the
406{@link android.R.style} class. To use the styles listed here, replace all underscores in
407the style name with a period. For example, you can apply the
408{@link android.R.style#Theme_NoTitleBar} theme with
409{@code "@android:style/Theme.NoTitleBar"}.</p>
410
411<p>The {@link android.R.style} reference, however, is not well documented and does not
412thoroughly describe the styles, so viewing the actual source code for these styles and
413themes will give you a better understanding of what style properties each one provides.
414For a better reference to the Android styles and themes, see the following source code:</p>
415<ul>
416	<li><a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml">Android Styles (styles.xml)</a></li>
417	<li><a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml">Android Themes (themes.xml)</a></li>
418</ul>
419
420<p>These files will help you learn through example. For instance, in the Android themes source code,
421you'll find a declaration for <code>&lt;style name="Theme.Dialog"&gt;</code>. In this definition,
422you'll see all of the properties that are used to style dialogs that are used by the Android
423framework.</p>
424
425<p>For more information about the syntax used to create styles in XML, see
426<a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Available Resource Types:
427Style and Themes</a>.</p>
428
429<p>For a reference of available style attributes that you can use to define a style or theme
430(e.g., "windowBackground" or "textAppearance"), see {@link android.R.attr} or the respective
431View class for which you are creating a style.</p>
432
433
434
435
436
437