Color Changing ActionBar - Xamarin

I have a little question about ActionBar in Android. I am using Xamarin Studio to create an application, and I would like to change the color of the ActionBar. I looked far and wide to change the color, but this is the only thing I could find.

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

I would succeed, but now I get this exception, and I cannot understand why this does not work for me.

This is an exception:

 Object reference not set to an instance of an object 

Can someone explain to me what I'm doing wrong and maybe show me how I can fix this.

Edit

After a few comments, I 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 to add a custom color and how to assign it to your action bar

+6
source share
4 answers

I have done so. 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 a color variable to strings.xml, add the desired color for the action bar

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

And mention the topic in the manifest file

 <application android:theme="@style/AppTheme" > 
+2
source
 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> 
+1
source

It 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> 
0
source

As an explanation of @SID's answer, to style your Actionbar you need to redefine the whole theme of your application.

If they do not exist, add the Styles.XML and Colors.XML files to the Resource>values folder and edit them as shown 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 go to Properties>AndroidManifest.xml and change the <application> tag to link to your customized theme:

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

UPDATE:

If you want to use AppCompat and ActionBarActivity , you will no longer need an ActionBarTheme in Styles.XML , this is what Styles.XML looks Styles.XML :

 <?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 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> 
0
source

Source: https://habr.com/ru/post/974979/


All Articles