Xamarin Forms Android Theming: Unable to change actionBar primaryColor

pierre-louis deschamps 146 Reputation points
2021-09-27T16:41:04.503+00:00

After this try:
https://learn.microsoft.com/en-us/answers/questions/562442/unable-to-use-custom-theme-in-android-xamarinforms.html

And after resolving this error:
https://learn.microsoft.com/en-us/answers/questions/565478/androidviewsinflateexception-binary-xml-file-line.html

I could change the statusBarColor and the navigationBarColor but not the colorPrimary of the action bar.

This is MainActivity.cs:

using Android.App;  
using Android.Content.PM;  
using Android.Runtime;  
using Android.Views;  
  
using Android.OS;  
using Xamarin.Forms.Internals;  
  
namespace Almicantarat.Droid  
{  
	[Activity (Label = "Almicantarat",   
		Icon = "@drawable/almicantarat_launcher",   
		MainLauncher = true,  
		ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]  
	//Theme = "@style/MainTheme",   
	public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
	{  
		protected override void OnCreate (Bundle savedInstanceState)  
		{  
			base.OnCreate (savedInstanceState);  
			Xamarin.Forms.Forms.SetFlags("SwipeView_Experimental");  
			Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
  
  
			Registrar.Registered.Register(typeof(Xamarin.Forms.CheckBox), typeof(Xamarin.Forms.Platform.Android.CheckBoxRenderer));  
  
			global::Xamarin.Forms.Forms.Init (this, savedInstanceState);  
			Xamarin.FormsMaps.Init(this, savedInstanceState);  
			  
            LoadApplication(new AlmicantaratXF.Views.App (AlmicantaratXF.Views.FirstPage.HomePage));  
  
        }  
		public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
		{  
			Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  
			base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
		}  
	}  
}  

This is styles.xml:

<?xml version="1.0" encoding="utf-8" ?>  
<resources>  
  <style name="AlmicantaratTheme" parent="Theme.AppCompat.NoActionBar">  
      <item name="android:windowNoTitle">true</item>  
      <item name="android:windowActionBar">false</item>  
      <item name="android:colorPrimary">#15014E</item>  
      <item name="android:colorPrimaryDark">#15014E</item>  
      <item name="android:navigationBarColor">#15014E</item>  
      <item name="android:statusBarColor">#15014E</item>  
      <item name="android:colorAccent">#FF4081</item>  
      <!--<item name="android:windowActionModeOverlay">true</item>-->  
  </style>  
</resources>  

This is AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="almicantarat.almicantarat" android:versionCode="17" android:versionName="1.3.0">  
	<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />  
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
	<uses-permission android:name="android.permission.INTERNET" />  
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
	<application android:label="Almicantarat"  
               android:icon="@drawable/almicantarat_launcher"  
							 android:theme="@style/AlmicantaratTheme">  
		<meta-data android:name="com.google.android.geo.API_KEY" android:value="***" />  
		<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />  
		<uses-library android:name="org.apache.http.legacy" android:required="false" />  
	</application>  
</manifest>  

And this is the result:
135615-screenshot-20210927-173855.jpg

The actionBar is black instead of the primaryColor I set (#15014E)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,342 questions
0 comments No comments
{count} votes

Accepted answer
  1. pierre-louis deschamps 146 Reputation points
    2021-09-28T14:54:49.007+00:00

    I feel ashamed as I found the origin of the issue:

    The toolbar was modified by NavigationPage BarBackgroundColor in App.xaml:

    <?xml version="1.0" encoding="utf-8"?>  
    <Application xmlns="http://xamarin.com/schemas/2014/forms"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                 x:Class="AlmicantaratXF.Views.App">  
        <Application.Resources>  
            <ResourceDictionary Source="Themes/DarkTheme.xaml">  
            <!-- The following code was setting the toolbar background color -->  
      <!--      <Style TargetType="{x:Type NavigationPage}">  
                    <Setter Property="BarBackgroundColor"  
                            Value="Black" />  
                    <Setter Property="BarTextColor"  
                            Value="White" />  
                </Style> -->  
       
    

    @JarvanZhang , thank you for the time spent on this issue.

    Regards,


3 additional answers

Sort by: Most helpful
  1. JarvanZhang 23,956 Reputation points
    2021-09-28T05:57:58.633+00:00

    Hello pierrelouisdeschamps-1678,​

    Welcome to our Microsoft Q&A platform!

    It's unnecessary to add 'android:' prefix for some resource properties in styles.xml. Please change the code like below:

       <style name="AlmicantaratTheme" parent="Theme.AppCompat.NoActionBar">  
         <item name="windowNoTitle">true</item>  
         <item name="windowActionBar">false</item>  
         <item name="colorPrimary">#15014E</item>  
         <item name="colorPrimaryDark">#15014E</item>  
         <item name="android:navigationBarColor">#15014E</item>  
         <item name="android:statusBarColor">#15014E</item>  
         <item name="colorAccent">#FF4081</item>  
         <item name="windowActionModeOverlay">true</item>  
       </style>  
    

    Theme.AppCompat.NoActionBar is in dark mode, if you want to use the light mode, please change the parent to Theme.AppCompat.Light.NoActionBar.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. pierre-louis deschamps 146 Reputation points
    2021-09-28T07:31:32.78+00:00

    Hello @JarvanZhang ,

    I have changed the android:colorPrimary to colorPrimary. But the toolbar background is still black instead of #15014E.

    I use this page:
    https://learn.microsoft.com/en-us/xamarin/android/user-interface/material-theme

    And succeeded in converting it to AndroidX but cannot change the toolbar background.

    Regards,


  3. pierre-louis deschamps 146 Reputation points
    2021-09-28T09:39:09.267+00:00

    The issue I am facing is connected to Theming and to Custom Toolbar.

    Currently I am unable to use the default Action Bar: if I remove everything about custom toolbar in style.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
    
      <style name="AlmicantaratTheme" parent="Theme.AppCompat"> <!--parent="Theme.AppCompat.NoActionBar"-->
     <!-- <item name="windowNoTitle">true</item>
          <item name="windowActionBar">false</item> -->
          <item name="colorPrimary">#15014E</item>
          <item name="colorPrimaryDark">#15014E</item>
          <item name="android:navigationBarColor">#15014E</item>
          <item name="android:statusBarColor">#15014E</item>
          <item name="colorAccent">#3498db</item>
          <item name="android:windowActionModeOverlay">true</item>
      </style>
    </resources>
    

    and in MainActivity.cs:

                //ToolbarResource = Resource.Layout.Toolbar;
                //TabLayoutResource = Resource.Layout.Tabbar;
                //var toolbar = FindViewById<Android.Widget.Toolbar>(Resource.Id.toolbar);
                //SetActionBar(toolbar);
    

    I get the following exception in MainActivity.cs:

    Java.Lang.IllegalStateException: 'This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.'

    but If I use a Custom Toolbar, I cannot change the background...


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.