Introduction

As in my previous article, this article describes various aspects and basics of an Action Bar. While learning the Action Bars basics we saw implementations of public boolean onOptionsItemSelected(MenuItem item) of MainActivity.java for setting up an Action Bar.
However, when it comes to styling or customizing the Action Bar theme we should know various theme resources. Basically, the Action Bar provides users a native and predictable way to perform actions and navigate the application. It is not obligatory for the developer to develop the Action Bars as other applications already have. If the developer wants to add a style to the Action Bar then that would be better of the fit product's brand, it could be easily achievable using Android's style and theme resources.

Use an Android Theme

Android has themes that specify the color. Android lacks basic themes however there are two themes basically as in the following: We can apply these themes to our entire application or to an individual activity by declaring them in the manifest file with the android.theme attribute for the <application> element or individual <activity> elements.
For example
  1. <application android: theme ="@android:style/Theme.Holo.light.../>

When Using Support Library

When the support library is used it is necessary to use the Themes as written below. However, use these libraries according to the contrast and icons matching the background of the respective themes. Styles and Themes
A style is a collection of properties that specify the look and format for a 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.
Likewise, CSS in web design and styles in Android plays same role. However, both are used for design purposes but the syntax is different.
  1. <TextView
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:textColor="#00FF00"
  5. android:typeface="monospace"
  6. android:text="@string/hello" />
It can be rewritten as,
  1. <TextView
  2. style="@style/CodeFont"
  3. android:text="@string/hello" />
    CodeFont is the attribute related to the style and have been removed from the layout.xml file and put into the style definition.

    Defining the Styles

    For each style creation add an <style> element to the file with a unique name.
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3. <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    4. <item name="android:layout_width">fill_parent</item>
    5. <item name="android:layout_height">wrap_content</item>
    6. <item name="android:textColor">#00FF00</item>
    7. <item name="android:typeface">monospace</item>
    8. </style>
    9. </resources>

    Style Properties

    In this section, we now understand what the properties are of the style element but some of these are under the <item> element like layout_width and text color.
      1. <Edit Text
      2. android: inputType="number"
      3. ....../>
      We can instead create a style for the EditText element that includes this property:
      1. <style name="Numbers">
      2. <item name="android:inputType">number</item>
      3. ...
      4. </style>

      Customizing the Background

      To change the Action Bar background, we must create a custom theme for our activity that will override the actionBarStyle property quite similarly. This property points to another style in which you can override the background property to specify a drawable resource for the Action Bar background.
      If an application uses navigation tabs or the split Action Bar, then we must specify the background for these bars types using the backgroundStacked and backgroundSplit properties, respectively.
      Note: It is the highest and first priority to set the appropriate parent theme from which the custom theme and default theme inherit their style.

      For Android 3.0 and higher only

      When supporting Android API level 11 or higher we can define an Action Bar background like this:
      1. <?xml version="1.0" encoding="utf-8"?>
      2. <resources>
      3. <!-- the theme applied to the application or activity -->
      4. <style name="CustomActionBarTheme"
      5. parent="@android:style/Theme.Holo.Light.DarkActionBar">
      6. <item name="android:actionBarStyle">@style/MyActionBar</item>
      7. </style>
      8. <!-- ActionBar styles -->
      9. <style name="MyActionBar"
      10. parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
      11. <item name="android:background">@drawable/actionbar_background</item>
      12. </style>
      13. </resources>
      Then apply the theme to the entire application or individual activities as in the following:
      1. <application android:theme="@style/CustomActionBarTheme" ... />

      For Android 2.1 and higher

      For the version of Android using the support library the changes that must exist in the code above provides compatiblity with the version support. Consider the code given below,
      1. <?xml version="1.0" encoding="utf-8"?>
      2. <resources>
      3. <!-- the theme applied to the application or activity -->
      4. <style name="CustomActionBarTheme"
      5. parent="@style/Theme.AppCompat.Light.DarkActionBar">
      6. <item name="android:actionBarStyle">@style/MyActionBar</item>
      7. <!-- Support library compatibility -->
      8. <item name="actionBarStyle">@style/MyActionBar</item>
      9. </style>
      10. <!-- ActionBar styles -->
      11. <style name="MyActionBar"
      12. parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
      13. <item name="android:background">@drawable/actionbar_background</item>
      14. <!-- Support library compatibility -->
      15. <item name="background">@drawable/actionbar_background</item>
      16. </style>
      17. </resources>
      Then apply this theme to the entire app or individual activities:
      1. <application android:theme="@style/CustomActionBarTheme" ... />
      Note: If we look at the code for both 3.0 and higher and for 2.1 that uses the support library it is just the lines of code that support library attributes as shown below:
      1. <item name="actionBarStyle">@style/MyActionBar</item>
      We must add this line of code for version 2.1 or higher just for compatibility with the support library.
      1. <item name="background">@drawable/actionbar_background</item>
        This line is for the background setter in the 2.1 or higher version just for compatibility as mentioned above.

        Summary

        This article is based on the themes and styles in Android for application to the Action Bar and individual activities. Basically the themes and styles are for the version higher than "Honeycomb" or API level 11 but it can be done in the 2.1 version using "Support libraries". See the code snippet with the minor changes in both of the versions.