unable to use custom theme in android xamarin.forms app

pierre-louis deschamps 146 Reputation points
2021-09-22T16:27:09.403+00:00

Hello, I am struggling for many hours.

I followed every step described here:
https://learn.microsoft.com/en-us/xamarin/android/user-interface/material-theme
and here:
https://www.serkanseker.com/xamarin-material-design-theme/

But I am unable to change navigationBarColor nor statusBarColor.

This is styles.xml:

<?xml version="1.0" encoding="utf-8" ?>  
<resources>  
  <style name="AlmicantaratTheme" parent="@android:Theme.Material">  
      <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>  

styles.xml is in Almicantarat.Droid/Resources/values-v21
I tried to put it in Almicantarat.Droid/Resources/values too.

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>  

Java jdk is jdk1.8.0_271
Android 5.0 - Lollipop Android SDK Platform 21 is installed

And this is the result:
134616-screenshot-20210922-181641-25.png

What is going wrong? Why does navigationBarColor remain white on the screenshot? and why does colorPrimary remain black?

Regards,

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2021-09-23T09:48:57.007+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I test it, I get the same result, If you want to change the color of navigationBarColor, you can add following code to OnCreate method of MainActivity.cs

       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
           {  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
         
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
         
       // add here  
                   if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)  
                   {  
                       Window.SetStatusBarColor(Android.Graphics.Color.Orange);  
                       Window.SetNavigationBarColor(Android.Graphics.Color.Orange);  
                   }  
         
         
         
                   LoadApplication(new App());  
               }  
    

    134682-image.png

    Best Regards,

    Leon Lu


    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.


1 additional answer

Sort by: Most helpful
  1. pierre-louis deschamps 146 Reputation points
    2021-09-24T14:45:41.797+00:00

    Hello @Leon Lu (Shanghai Wicresoft Co,.Ltd.)

    I moved my style.xml to values file: Same exception.

    I start a new thread as it is no more a custom theme issue but it seems to be more a toolbar issue.

    Regards

    0 comments No comments