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 ActionBar 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 ActionBar 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.

Below basic Activity themes in Android can be used to specify the color of the ActionBar, they are:

  1. @android:style/Theme.Holo : This is a dark-colored theme.
    action bar theme holo
  2. @android:style/Theme.Holo.Light : This is a light-colored theme.
    action bar theme holo light
  3. @android:style/Theme.Holo.Light.DarkActionBar : The ActionBar will be dark-colored, the content view bill be light-colored.
    action bar theme holo light dark action bar

You can apply these themes to your entire application, or you can apply it to just one Activity.

Specify android:theme properties for the <application> or <activity> tags in AndroidManifest.xml file. For example:

<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>
<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>

Please Note :  The Activity class must extends android.app.Activity, it can not extends android.support.v7.app.AppCompatActivity or it’s sub class, otherwise you will meet below error when execute the application.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

android activity appcompat theme error

You can use below java code to set theme.

setTheme(android.R.style.Theme_Holo_Light_Dialog_NoActionBar);

2. Custom ActionBar Background.

If you want to modify the background of your ActionBar, you can implement it by creating a custom theme and rewriting the actionBarStyle property.

READ :   Android Define Activity Transition Animation In Xml Example

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.

  1. Edit app / res / values / styles.xml file.
    <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>
  2. Use the custom theme as application or activity’s android:theme property value.
    <activity android:name=".ActionBarCustomStyleActivity"
        android:theme="@style/CustomActionBarTheme" >

    custom action bar background color

(Visited 664 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

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