Styles and Themes

Style resources let your applications maintain a consistent look and feel by enabling you to specify the attribute values used by Views. The most common use of themes and styles is to store the colors and fonts for an application.

You can easily change the appearance of your application by simply specifying a different style as the theme in your project manifest.

To create a style use a <style> tag that includes a name attribute and contains one or more item tags. Each item tag should include a name attribute used to specify the attribute (such as font size or color) being defined. The tag itself should then contain the value, as shown in the following skeleton code.

<?xml version="1.0" encoding="utf-8"?> <resources>

<style name="StyleName">

<item name="attributeName">value</item> </style> </resources>

Styles support inheritance using the parent attribute on the <style> tag, making it easy to create simple variations.

The following example shows two styles that can also be used as a theme: a base style that sets several text properties and a second style that modifies the first to specify a smaller font.

<?xml version="1.0" encoding="utf-8"?> <resources>

<style name="BaseText">

<item name="android:textSize">14sp</item> <item name="android:textColor">#111</item> </style>

<style name="SmallText" parent="BaseText">

<item name="android:textSize">8sp</item> </style> </resources>

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment