user3996249
user3996249

Reputation:

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

Upvotes: 7

Views: 14599

Answers (6)

user14187680
user14187680

Reputation:

Your problem is my solution, i used your code with some slight modifications and it worked, attached is a picture sample of the result code and below is your code plus my modified verenter image description heresion of your code

//Define ActionBar
       actionbar=SupportActionBar;
//Define Color Drawable
            ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#ff0f62ae"));
//Set drawable color to ActionBar
    actionbar.SetBackgroundDrawable(colorDrawable);

Upvotes: 0

Led Machine
Led Machine

Reputation: 7552

A very simple solution, go to your "styles.xml" file on your Xamarin.Android project, and just replace or add this properties to your style:

<!-- colorPrimary is used for the default action bar background -->    
<item name="colorPrimary">#002500</item>
<!-- colorPrimaryDark is used for the status bar #4FAB5B-->
<item name="colorPrimaryDark">#002500</item>

Upvotes: -1

AmiNadimi
AmiNadimi

Reputation: 5755

As an explanation upon @SID's answer , For styling your Actionbar you have to override the whole theme of your app.

If they don't exist add Styles.XML and Colors.XML files under Resource>values folder and edit them as below :

Styles.XML:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <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/DarkBlue</item>
  </style>
</resources>

Colors.XML:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="DarkBlue">#007acc</color> 
</resources>

Now navigate to Properties>AndroidManifest.xml and change the <application> tag to reference your customized theme :

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

  </application>

UPDATE :

If you are willing to use AppCompat and ActionBarActivity then you will no longer need the ActionBarTheme in Styles.XML so here is what Styles.XML will look like :

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/LightBlue</item>
    <item name="colorPrimaryDark">@color/DarkBlue</item>
    <item name="colorAccent">@color/Pink</item>
  </style>

</resources>

And the Colors.XML :

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <color name="DarkBlue">#0052cc</color>
  <color name="Pink">#ff1ac6</color>
  <color name="LightBlue">#1a75ff</color>

</resources>

Upvotes: 0

alin0509
alin0509

Reputation: 551

This worked for me

   <color name="actionBarBackgroundColor">#2C3E50</color>
    <color name="actionBarTextColor">#FFFFFF</color>
  <style name="MyAppTheme"/>
    <style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light">
      <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>         
    </style>
    <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.DeviceDefault.Light.ActionBar">
      <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
      <item name="android:background">@color/actionBarBackgroundColor</item>
    </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
      <item name="android:textColor">@color/actionBarTextColor</item>
    </style>

Upvotes: 0

user3579236
user3579236

Reputation:

Try this:


<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#f00</item>
        <item name="android:titleTextStyle">@style/MyTextAppearance</item>
    </style>

    <style name="MyTextAppearance" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#ffffff</item>
    </style>

Upvotes: 1

Sid
Sid

Reputation: 711

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

Upvotes: 3

Related Questions