How to define Primary and Secondary Accent Colors for Android-Maui?

dg2k 1,386 Reputation points
2023-10-31T06:23:17.8033333+00:00

I understand that AppThemeBinding markup extension goes a long way to define Dark Theme and Light Theme for a MAUI app.

But how to define Dark and Light themes for android accent color such as the ones used for RadioButton and CheckBox selections and AlertDisplay buttons and so on. Is there a new MAUI way of doing this or is this still under Android theme resources?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,870 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,546 Reputation points Microsoft Vendor
    2023-11-02T07:13:31.07+00:00

    Hello,

    Ideally I want my app to follow the system theme setting. The best scenario is that when the user changes to Light or Dark theme from device setting, I want my app to adapt to this. I just found out (unless I am missing something) that MAUI is not doing this yet.

    This issue is a known issue in GitHub, see [regression/7.0.0] Application.Current.RequestedThemeChanged event only raises once #8236.

    Short of the ideal (following system theme), I don't mind providing a theme toggle in my app setting so that the user can choose as required. This is better than fixing the theme (because some users like a dark theme, say, for night vision, or to save battery if their device display technology allows it).

    You could manually switch between light or dark themes for the program, see the following code.

    private void ChangeThemeBtn_Clicked(object sender, EventArgs e)
    {
        if (Application.Current.UserAppTheme == AppTheme.Dark)
        {
            Application.Current.UserAppTheme = AppTheme.Light;
        }
        else
        {
            Application.Current.UserAppTheme = AppTheme.Dark;
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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