11

I'm trying to change Navigation Bar and Status Bar background color in my .Net MAUI Android App.

I found how to do that when the app has initialized an loaded (Status Bar: Using MauiCommunityToolkit StatusBarBehavior StatusBarColor feature. Navigation Bar: Using Window.SetNavigationBarColor).

But I have not found how to do that when the app starts and shows the Splash Screen. I need black background color for both, Status and Navigation bar but when the App starts, NavBar is white and Status Bar is Purple (default .Net MAUI color). I attached an image of how it looks when starts.

I tried changing the Primary Color in the Resources/colors.xml, and it didn't work.

My App is a MAUI Single project template (Android and iOS).

Thanks in advance for taking a look at this issue.enter image description here

UPDATE: I was able to change just Status Bar, changing defautl colorPrimaryDark value in Platforms/Android/Resources/colors.xml enter image description here

But still looking at how to change Navigation Bar color. enter image description here

4 Answers 4

15

You can create a new .xml file and then put the style in or add the style to colors.xml directly:

<resources>
    <color name="colorPrimary">#512BD4</color>
    <color name="colorPrimaryDark">#2B0B98</color>
    <color name="colorAccent">#2B0B98</color>
    
    <style name="SplashTheme" parent="@style/Maui.SplashTheme">
        <item name="android:statusBarColor">@android:color/black</item>
        <item name="android:navigationBarColor">@android:color/black</item>
    </style>
</resources>

In the MainActivity.cs file (Platforms/Android):

[Activity(
    //Theme = "@style/Maui.SplashTheme",
    Theme = "@style/SplashTheme",
    MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : MauiAppCompatActivity

It works well and can achieve your need.

Sign up to request clarification or add additional context in comments.

2 Comments

@maguilleng You can also see the source code about styles.xaml
I copy and pasted both your colors.xml and the MainActivity.cs and I am getting an error that the resource style/SplashTheme is not found. Did anyone else have this issue?
2

Maybe it will be useful to someone

MainPage.xaml:

xmlns:droid="clr-namespace:CommunityToolkit.Maui.PlatformConfiguration.AndroidSpecific;assembly=CommunityToolkit.Maui"
droid:NavigationBar.Color="{Binding BarColor}">

MainPageViewModel.cs:

 private Color? _statusBarColor = Colors.Black;

 public Color? StatusBarColor { get => _statusBarColor; set => SetProperty(ref _statusBarColor, value); }

This approach allows you to dynamically change the color of the navigation bar.

Comments

1

From the reference: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/statusbar-behavior?tabs=android

1- Get CommunityToolkit (YourApp.csproj)

 <ItemGroup>
     <PackageReference Include="CommunityToolkit.Maui" Version="7.0.0" />
     <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
     <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
     <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
 </ItemGroup>

2- Update App.xaml

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:YourMauiApp"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="YourMauiApp.App">

3- Update MauiProgram.cs registration

 MauiAppBuilder builder = MauiApp.CreateBuilder();
 builder
     .UseMauiApp<App>()
     .UseMauiCommunityToolkit()

4- I am using AppShell.xaml but wherever you want ot change color:

add namespace

 xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Then:

 <Shell.Behaviors>
     <toolkit:StatusBarBehavior StatusBarColor="White" StatusBarStyle="DarkContent" />
 </Shell.Behaviors>

And result: enter image description here

1 Comment

Any idea why StatusBarColor="{DynamicResource PrimaryBlue}" wouldn't work, but StatusBarColor="{StaticResource PrimaryBlue}" does work? It looks to be a BindableProperty.
0
xmlns:droid="clr-namespace:CommunityToolkit.Maui.PlatformConfiguration.AndroidSpecific;assembly=CommunityToolkit.Maui"
droid:NavigationBar.Color="#000000"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.