Aswin Balaji

Aswin Balaji

186 Sadasivam Nagar,Madipakkam,Chennai, TN 600091
Aswin Balaji

Android – Styles and Themes

June 30, 2015, by Aswin Balaji, category ANDROID
A style resource defines the format and look for a UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file).

Defining Styles

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the .xml extension.
You can define multiple styles per file using <style> tag but each style will have its name that uniquely identifies the style.

Using a Custom Nine-Patch With Buttons

A nine-patch drawable is a special kind of image which can be scaled in width and height while maintaining its visual integrity. Nine-patches are the most common way to specify the appearance of Android buttons, though any drawable type can be used.
Nine Patch Button  a Sample of Nine-Patch button

Steps to create Nine-Patch Buttons

  • Save this bitmap as /res/drawable/my_nine_patch.9.png
  • Define a new style
  • Apply the new button style to the buttonStyle attribute of your custom theme

Android Themes

Hope you understood the concept of Style, so now let’s try to understand what is a Theme. A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View.
Thus, 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 CustomFontStyle style as a theme for an Activity and then all text inside that Activity will have green monospace font.
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example −

<application android:theme="@style/CustomFontStyle">

But if you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag only. For example −

<activity android:theme="@style/CustomFontStyle">

There are number of default themes defined by Android which you can use directly or inherit them using parent attribute as follows −

<style name="CustomTheme" parent="android:Theme.Light">
...........  </style>

To understand the concept related to Android Theme, you can check Theme Demo Example.

Styling the colour palette

The layout design can implementable based on them based colours, for example as following design is designed based on them colour(blue)
Above layout has designed based on style.xml file,Which has placed at res/values/

<resource>
 <style name="AppTheme" parent="android:Theme.Material">
 <item name ="android:color/primary">@color/primary</item>
 <item name ="android:color/primaryDark">@color/primary_dark</item>
 <item name ="android:colorAccent/primary">@color/accent</item>
 </style>
 <resource>

Default Styles & Themes

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 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 Theme_NoTitleBar theme with “@android:style/Theme.NoTitleBar”. You can see the following source code for Android styles and themes −

  • Android Styles (styles.xml)
  • Android Themes (themes.xml)