From 4d5770904c6b1a20c46fc25ccb00bdd34d5e3386 Mon Sep 17 00:00:00 2001
From: Scott Main 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 collection of properties that
+specify the look and format for a {@link android.view.View} or window.
+A style can specify properties such as height, padding, font color, font size,
+background color, and much more. A style is defined in an XML resource that is
+separate from the XML that specifies the layout. 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. Styles in Android share a similar philosophy to cascading stylesheets in web
+design—they allow you to separate the design from the
+content. To create custom styles and themes: For example, by using a style, you can take this layout XML: And turn it into this: All of the attributes related to style have been removed from the layout XML and put into a
+style definition called {@code CodeFont}, which is then applied with the A theme is a style applied to an entire {@link android.app.Activity} or
+application, rather than an individual {@link android.view.View} (as in the example above). When a
+style is applied as a theme, every View in the Activity or application will apply each style
+property that it supports. For example, you can apply the same {@code CodeFont} style
+as a theme for an Activity and then all text inside that Activity will have green monospace
+font. Here's an example declaration of a style: To create a set of styles, save an XML file in the {@code res/values/}
+directory of your project. The name of the XML file is arbitrary, but it must use the
+{@code .xml} extension and be saved in the {@code res/values/} folder. The root node of the XML file must be {@code <resources>}. For each style you want to create, add a {@code <style>} element to the file
+with a {@code name} that uniquely identifies the style (this attribute is required).
+Then add an {@code <item>} element for each property of that style, with a
+{@code name} that declares the style property and a value to go with it (this attribute
+is required). The value for the {@code <item>} can
+be a keyword string, a hex color, a reference to another resource type, or other value
+depending on the style property.
+Here's an example file with a single style: As shown, you can use Each child of the {@code <resources>} element is converted into an application resource
+object at compile-time, which can be referenced by the value in the {@code <style>} element's
+{@code name} attribute. This example style can be referenced from an XML layout as
+{@code @style/CodeFont} (as demonstrated in the introduction above). Notice the The Here's how you would reference the custom style from an XML layout, in this case, for an EditText element: Remember, a style that you want to use as an Activity or application theme is defined in XML
+exactly the same as a style for a View. A style such as the one defined above can be applied as a
+style for a single View or as a theme for an entire Activity or application. How to apply a style
+for a single View or as an application theme is discussed later. The {@code parent} attribute in the {@code <style>} element lets you specify a style
+from which your style should inherit properties.
+You can use this to inherit properties from an existing style and
+then define only the properties that you want to change or add. You can
+inherit from styles that you've created yourself or from styles that are built into the
+platform. (See Using Platform Styles and Themes, below, for
+information about inheriting from styles defined by the Android platform.) For example, you can
+inherit the Android platform's default text appearance and then modify it: Now this EditText widget will be styled as defined by the XML example above. Just like styles, themes are also declared in XML Here's an example declaration of a theme: If you want to inherit from styles that you've defined yourself, you do not have to use
+the 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 Notice that there is no {@code parent} attribute in the {@code <style>} tag, but because
+the {@code name} attribute begins with the {@code CodeFont} style name (which
+is a style that you have created), this style inherits all style properties from that style. This
+style then overrides the {@code android:textColor} property to make the text red. You can
+reference this new style as {@code @style/CodeFont.Red}. To set this theme for all the activites of your application, open the AndroidManifest.xml file and
+ You can continue inheriting like
+this as many times as you'd like, by chaining names with periods. For example, you can
+extend {@code CodeFont.Red} to be bigger, with: This inherits from both {@code CodeFont} and {@code CodeFont.Red} styles, then adds the
+{@code android:textSize} property. Note: This technique for inheritance by chaining together
+names only works for styles defined by your own resources. You can't inherit Android built-in styles
+this way. To reference a built-in style, such as {@link android.R.style#TextAppearance}, you must
+use the {@code parent} attribute. Now that you understand how a style is defined, you need to learn what kind
+of style properties—defined by the {@code <item>} element—are available.
+You're probably familiar with some already, such as {@link android.R.attr#layout_width} and
+{@link android.R.attr#textColor}. Of course, there are many more style properties you can use. The best place to find properties that apply to a specific {@link android.view.View} is the
+corresponding class reference, which lists all of the supported XML attributes. For example, all of the
+attributes listed in the table of
+TextView XML
+attributes can be used in a style definition for a {@link android.widget.TextView} element (or one of
+its subclasses). One of the attributes listed in the reference is {@code
+android:inputType}, so where you might normally place the {@code
+android:inputType}
+attribute in an {@code <EditText>} element, like this: You can instead create a style for the {@link android.widget.EditText} element that includes this property: So your XML for the layout can now implement this style: This simple example may look like more work, but when you add more style properties and
+factor-in the ability to re-use the style in various places, the pay-off can be huge. For a reference of all available style properties, see the {@link android.R.attr}
+reference. Keep in mind that all View objects don't accept all the same style attributes, so you
+should normally refer to the specific {@link android.view.View} class for supported style
+properties. However, if you
+apply a style to a View that does not support all of the style properties, the View will
+apply only those properties that are supported and simply ignore the others. Some style properties, however, are not supported by any View element and can only be applied
+as a theme. These style properties apply to the entire window and not to any type of View.
+For example, style properties for a theme can hide the application title, hide the status bar,
+or change the window's background. These kind of style properties do not belong to any View object.
+To discover these theme-only style properties, look at the {@link android.R.attr} reference for
+attributes that begin with {@code window}. For instance, {@code windowNoTitle} and {@code
+windowBackground} are style properties that are effective only when the style is applied as
+a theme to an Activity or application. See the next section for information about applying a
+style as a theme. Note: Don't forget to prefix the property names in each
+{@code <item>} element with the There are two ways to set a style: When you apply a style to a single {@link android.view.View} in the layout, the properties
+defined by the style are applied only to that {@link android.view.View}. If a style is applied to a
+{@link android.view.ViewGroup}, the child {@link android.view.View} elements will
+not inherit the style properties—only the element to which you directly apply
+the style will apply its properties. However, you can apply a style so that it
+applies to all {@link android.view.View} elements—by applying the style as a theme. To apply a style definition as a theme, you must apply the style to an
+{@link android.app.Activity} or application in the Android manifest. When you do so,
+every {@link android.view.View} within the Activity or
+application will apply each property that it supports. For example, if you apply the {@code
+CodeFont} style from the previous examples to an Activity, then all View elements
+that support the text style properties will apply them. Any View that does not support
+the properties will ignore them. If a View supports only some of the properties, then
+it will apply only those properties. Here's how to set a style for a View in the XML layout: Now this TextView will be styled as defined by the style named {@code CodeFont}.
+(See the sample above, in Defining Styles.) Note: The To set a theme for all the activities of your application, open the {@code AndroidManifest.xml} file and
edit the In this document
-
See also
-
-
+
-
+styles.xml in the your application's res/values directory. Add a root <resources> node.<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.<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.
+<TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:textColor="#00FF00"
+ android:typeface="monospace"
+ android:text="@string/hello" />
+
+
+<TextView
+ style="@style/CodeFont"
+ android:text="@string/hello" />
+
+
+style
+attribute. You'll see the definition for this style in the following section.Styles
+Defining Styles
-
-<?xml version="1.0" encoding="utf-8"?>
+<?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 name="CodeFont" parent="@android:style/TextAppearance.Medium">
+ <item name="android:layout_width">fill_parent</item>
+ <item name="android:layout_height">wrap_content</item>
+ <item name="android:textColor">#00FF00</item>
+ <item name="android:typeface">monospace</item>
</style>
</resources>
-<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.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.parent attribute in the {@code <style>} element is optional and
+specifies the resource ID of another style from which this style should inherit
+properties. You can then override the inherited style properties if you want to.Inheritance
+
+
-<EditText id="@+id/text1"
- style="@style/SpecialText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Hello, World!" />
+ <style name="GreenText" parent="@android:style/TextAppearance">
+ <item name="android:textColor">#00FF00</item>
+ </style>
-Themes
-
-<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.parent attribute. Instead, just prefix the name of the style you want to
+inherit to the name of your new style, separated by a period. For example, to create a new style
+that inherits the CodeFont style defined above, but make the color red,
+you can author the new style like this:
-<?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>
+ <style name="CodeFont.Red">
+ <item name="android:textColor">#FF0000</item>
+ </style>
-<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
-
+ <style name="CodeFont.Red.Big">
+ <item name="android:textSize">30sp</item>
+ </style>
+
+Style Properties
+
+
+<EditText
+ android:inputType="number"
+ ... />
+
+
+
+<style name="Numbers">
+ <item name="android:inputType">number</item>
+ ...
+</style>
+
+
+<EditText
+ style="@style/Numbers"
+ ... />
+
+
+android: namespace. For example:
+{@code <item name="android:inputType">}.Applying Styles and Themes to the UI
+
+
+
+
+style attribute to a View
+ element in the XML for your layout.android:theme
+ attribute to the <activity> or <application> element
+ in the Android manifest.Apply a style to a View
+
+
+<TextView
+ style="@style/CodeFont"
+ android:text="@string/hello" />
+
+
+style attribute
+does not use the android: namespace prefix.Apply a theme to an Activity or application
+
+<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.
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 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:
+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 {@code Dialog} theme and make your Activity +appear like a dialog box:
<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:
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 dialog theme to use your own
+background image like this:
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog"> + <item name="android:windowBackground">@drawable/custom_dialog_background</item> +</style>-
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.
-You can also load a theme for an Activity programmatically, if needed. -To do so, use the {@link android.app.Activity#setTheme(int) setTheme()} -method. Note that, when doing so, you must be sure to set the theme before +
Now use {@code CustomDialogTheme} instead of {@code Theme.Dialog} inside the Android +Manifest:
+ ++<activity android:theme="@style/CustomDialogTheme"> ++ + + + + + +
The Android platform provides a large collection of styles and themes that you can +use in your applications. You can find a reference of all available styles in the +{@link android.R.style} class. To use the styles listed here, replace all underscores in +the style name with a period. For example, you can apply the +{@link android.R.style#Theme_NoTitleBar} theme with +{@code "@android:style/Theme.NoTitleBar"}.
+ +The {@link android.R.style} reference, however, is not well documented and does not +thoroughly describe the styles, so viewing the actual source code for these styles and +themes will give you a better understanding of what style properties each one provides. +For a better reference to the Android styles and themes, see the following source code:
+ + +These files will help you learn through example. For instance, in the Android themes source code,
+you'll find a declaration for <style name="Theme.Dialog">. In this definition,
+you'll see all of the properties that are used to style dialogs that are used by the Android
+framework.
For more information about the syntax used to create styles in XML, see Available Resource Types: Style and Themes.
-For information about default themes and styles available, see {@link android.R.style}.
- +For a reference of available style attributes that you can use to define a style or theme +(e.g., "windowBackground" or "textAppearance"), see {@link android.R.attr} or the respective +View class for which you are creating a style.