Android App Development: Using Themes and Styles in Android
Themes are used to group a set of styles to be applied on the whole web application. Themes (or sometimes skins) define the look of all control within the application.
Android introduces similar concepts by using Styles and Themes. A Style can be applied to views individually while a Theme is applied to a whole activity.
Styles:
Styles are defined as xml resources files in res/values directory of your project.
consider this definition of a TextView:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:typeface="monospace"
android:text="First Text View"
android:background="#00F"
/>
this defines a text view with width and height equal to wrap_content, white font color, font type “monospace” and blue back ground.
if we want to have the same results using a style: we first create a xml file (call it styles.xml) inres/values directory and it would be like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="BlueLabel">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:typeface">monospace</item>
<item name="android:background">#00F</item>
<item name="android:textColor">#FFF</item>
</style>
</resources>
then redefine the TextView like this:
<TextView android:text="First Text View"
style="@style/BlueLabel"
/>
and you will receive the same results.the attributes in the - tag can be any layout property.
Inheriting Styles:
Styles in Android has an interesting feature which is the ability to inherit styles in a fashion similar to that in CSS. consider this example:
we have a style for a button like this:
<style name="ButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15px</item>
<item name="android:typeface">serif</item>
</style>
the button will appear like this:we can make this style inherit from BlueLabel defined previously by adding the parent attribute to the