Android Custom ActionBar Style Example

Although ActionBar provides users with a globally unified interface style and operation, it doesn’t mean that all applications’ ActionBars have to be exactly the same. If you need to modify the style of your Action Bar to better match your application, you can make it very simple by using Android styles and themes.

Several of Android’s built-in Activity themes already contain Action Bar styles such as “dark” or “light”, and you can also inherit these themes and make further customization.

1. Use Theme To Change Action Bar Style.

  1. Below basic Activity themes in Android can be used to specify the color of the ActionBar, they are:
  2. @android:style/Theme.Holo: This is a dark-colored theme.
  3. @android:style/Theme.Holo.Light: This is a light-colored theme.
  4. @android:style/Theme.Holo.Light.DarkActionBar: The ActionBar will be dark-colored, the content view bill be light-colored.
  5. You can apply these themes to your entire application, or you can apply them to just one Activity.
  6. Specify android:theme properties for the <application> or <activity> tags in AndroidManifest.xml file. example.
  7. XML snippet for <application> tag.
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Holo.Light">
    
    </application>
  8. XML snippet for <activity> tag.
    <activity android:name=".ActionBarCustomStyleActivity"
               android:theme="@android:style/Theme.Holo">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  9. Please Note:  The Activity class must extend the android.app.Activity class, it can not extends android.support.v7.app.AppCompatActivity or it’s subclasses, otherwise you will meet the below error when executing the application. The error message is java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
       at android.support.V7.app.AppCompatDelegateImp1V9.createSubDecor(AppCpmpatDelegateImplV9.java: 356)
       ......
       at android.app.Activity.performCreate(Activity.java:6679)
  10. You can use the below java code to set the theme.
    setTheme(android.R.style.Theme_Holo_Light_Dialog_NoActionBar);

2. Custom ActionBar Background.

  1. If you want to modify the background of your ActionBar, you can implement it by creating a custom theme and rewriting the actionBarStyle property.
  2. This property can point to another style, and then override background in that style to specify a drawable resource or color to implement the functionality of the custom background. Below are the steps.
  3. Edit app / res / values / styles.xml file with the below XML content.
    <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/NewActionBar</item>
    </style>
    
    <style name="NewActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#00ff00</item>
    </style>
  4. Use the custom theme as application or activity’s android:theme property value as below.
    <activity android:name=".ActionBarCustomStyleActivity"
        android:theme="@style/CustomActionBarTheme" >
  5. Below is the example result.
    custom-action-bar-background-color
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x