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.
- Below basic Activity themes in Android can be used to specify the color of the ActionBar, they are:
- @android:style/Theme.Holo: This is a dark-colored theme.
- @android:style/Theme.Holo.Light: This is a light-colored theme.
- @android:style/Theme.Holo.Light.DarkActionBar: The ActionBar will be dark-colored, the content view bill be light-colored.
- You can apply these themes to your entire application, or you can apply them to just one Activity.
- Specify android:theme properties for the <application> or <activity> tags in AndroidManifest.xml file. example.
- 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>
- 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>
- 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)
- You can use the below java code to set the 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.
- 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.
- 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>
- Use the custom theme as application or activity’s android:theme property value as below.
<activity android:name=".ActionBarCustomStyleActivity" android:theme="@style/CustomActionBarTheme" >
- Below is the example result.