An online markdown blog and knowledge repository.
Android Developer Styles and Themes
Android Styles and Themes from Medium.com
Separate details of app design from UI structure and behavior.
Think web stylesheets.
Style: Collection of attributes for a single View => font color, font size, background color, etc.
Theme: Collection of attributes applied to:
Status Bar and Window background are "non-view elements" and Themes can apply to those.
Both Styles and Themes use same <style>
syntax.
Think of Styles and Themes as KPVs: Keys are attributes; Values are resources.
Can apply to specified items like Buttons.
View Attributes that apply to a single type of widget. [medium.com article, linked in resources]
Are lists can be set in the Layout file.
Extract all attributes from Style to use them across multiple widgets.
Can consider Styles to be a Map of type view attributes and resource values.
Defines a collection of named resoures that can be referenced by Styles, Layouts, Widgets, etc.
Themes apply semantic names to Android resources.
Semantic names e.g. "colorPrimary".
Styles and Themes meant to work together.
Style definitions are within the Theme.
Themes can be switched e.g. "Night Mode" theme vs "light" theme.
Themes use same semantic style names, so light/dark switching does not require changing values in resource names.
To create a style, follow these steps, repeat for each style to create:
res/values/styles.xml
<item>
element for each style attribute to definename
in each item specifies an attribute name; value
in each item specifies the value to apply to that attributeparent=
to identify the parent element for the style name to extend (see below)Apply a style to a View:
<TextView style="@style/GreenText" ...>
Note: A View might not accept a style and will ignore the attribute.
Always better to extend from the framework or support library to maintain compatibility.
To extend a style:
parent=...
<style name="Foo" parent="@android:style/TextAppearance">
<item name="android:textColor">#0FA12E</item>
</style>
Styles in the Android Support Library: Compatibility with API 14 and higher.
AppCompat
is used to indicate Android Support Library implementation for API 14 through (current), which apply to Elements with similar names through the version history.
To inherit from a Library or your own project, do not use @android:style/
prefix to element names.
<style name="Foo" parent="TextAppearance.AppCompat">
<item name="android:textdColor">#0F1E2D</item>
</style>
Inherit styles by exsting style's existing name and use dot notation instead of parent attribute.
Prefix name of style to inherit, add a dot, then append the name of your custom style.
Note: Only do it this way when extending your own styles and not from other Libraries.
Increase text size:
<style name="Foo.Large">
<item name="android:textSize">20dp</item>
</style>
Inherit (through chaining) to create more.
Using parent
attribute will override chained custom styles.
Similar to creating Styles.
Apply a theme with android:thme
attribute.
Apply theme attribute to <application>
or <activity>
tag in AndroidManifest.xml
.
<manifest ...>
<application android:theme="@style/Theme.AppCompat" ...>
</application>
</manifest>
Apply a "light" theme to a single Activity:
<manifest ...>
<application ...>
<activity android:theme="@style/Theme.AppCompat.Light" ...>
</activity>
</application>
</manifest>
If a view supports only some of the attributes declared in the style only those are applied.
To modify theme for specific view and its child views at API Level 21 through v22.1: Specify android:theme
attribute to view in Layout file.
Ways to set attributes:
Advice:
Hierarchical Precedence:
TextView
-derived classesTextAppearance
on a TextView
For example:
A View can have but one single style applied to it.
TextAppearance
attribute act similarly to Style
<TextView
...
android:textAppearance="@android:style/TextAppearance.Material.Headline"
android:text="Foo Bar!" />
Using TextAppearance
leaves a View's style available for use.
Applying text attributes directly to the View (or via a Style) the TextAppearance
values will be overridden.
THere are subsets of styling attributes offered through TextView
. See TextAppearance subsection for details.
Material design theme is applied to App by default upon project creation.
Material design theme is defined in styles.xml
file.
Material design theme extends a suport library theme and overrides appbar
and floating action button
elements.
Color Resources are found in the project's res/values/colors.xml
file.
Use the Material Color Tool
to preview colors prior to applying them.
After previesing the colors, update values in res/cavlues/colors.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- color for the app bar and other primary UI elements -->
<color name="colorPrimary">#3F51B5</color>
<!-- a darker variant of the primary color, used for
the status bar (on Android 5.0+) and contextual app bars -->
<color name="colorPrimaryDark">#303F9F</color>
<!-- a secondary color for controls like checkboxes and text fields -->
<color name="colorAccent">#FF4081</color>
</resources>
Once this is updated, override whatever other styles needed.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:windowBackground">@color/activityBackground</item>
</style>
Look at R.styleable theme table for a lookup of standard attributes that make up a complete theme.
Remember: All Views support XML attributes from the base View
class.
This is possible by adding another styles.xml
file in the values
directory that includes the resource version qualifier:
res/values/styles.xml
: Themes for all versions
res/values-v21/styles.xml
: Themes for API level 21+ only
Themes in values-v21/styles.xml
can inherit from values/styles.xml
extending the base theme and adding version-specific styles.
See the subsection Add Version Specific Styles.
Button is an example of a Widget.
Widget.AppCompat.Button
is how it can be styled.
Apply a 'Borderless" style to all Button widgets:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="buttonsStyle">@style/Widget.AppCompat.Button.Borderless</item>
...
</style>
This animates the transition between Views.
Transitions:
Enter and Exit Transitions:
Transitions that support Visibility
class can be used as enter or exit transitions.
Transition class API
Shared Element Transitions:
Requires Android 5.0 or higher.
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Apply activity transition
} else {
// Swap without transition
}
android:windowActivityTransitions
attribute when defining a style that inherits from 'material theme'.See sample code in Specify Custom Transitions sub-section.
This is covered in Start An Activity Using Transitions sub-section.
Back to Root README