Search code examples
c#androidxamarin.android

Change ActionBar color - Xamarin


i've got a small question about the ActionBar in Android. I am using Xamarin Studio to make an application and i would like to change the color of the ActionBar. I've looked far and wide for a way to change the color but this is the only thing that i could find.

ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#ff0f62ae"));
            this.ActionBar.SetBackgroundDrawable(colorDrawable); 

I tought this would work but now i get this exception and i cannot figure out why this isnt working for me.

This is the exception:

Object reference not set to an instance of an object

Can someone explain to me what i am doeing wrong and maybe show me how i can fix this.

Edit

After an couple of comments i've tried this:

<resources>
    <style name="AppTheme"
           parent="android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/</item>
        <item name="andriod:titleTextStyle">@style/MyActionBarTextAppearance</item>
    </style>

    <style name="MyActionBarTextAppearance" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
       <item name="android:textColor">@color/default_white</item>
    </style>
</resources>

How can i add a custom color, and how do i assign this to my actionbar


Solution

  • I did in this way. Try this and let me know if it works please,

    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/ActionBarTheme</item>
    </style>
    
    <style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/color_action_bar</item>
    </style> "
    

    And in colors.xml or add color variable in strings.xml, please add your desired color for the action bar

    <color name="color_action_bar">#3450A3</color>
    

    And mention the theme in the manifest file

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