Forum Xamarin Xamarin.Forms

Access AppThemeBinding in App.xaml from Android

ganesh96ganesh96 Member ✭✭✭

I'm using AppThemeBinding to respond to the dark theme on a device. I put all the colors in App.xaml and then I using static resource to access the style:

Now I need to access these colors from Android and iOS in two different ways:

1) With xml:

2) With C#:

Is this possible? If yes, how? If now, how can I achieve the respond to dark/light theme in Android and iOS?

I hope someone can help.



  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    The App class has an event for theme changed that you can listen to in the Android or iOS layer, and then respond by pulling either the light or dark resource depending. That way you dont have to worry about the AppThemeBinding.

    You can't put it into the layer list, you will have to create that list in code and import it correctly or have a light and dark colors xml files in the native manner.

  • ganesh96ganesh96 Member ✭✭✭

    Hi @AdamMeaney

    Thanks for the quick response.

    If I create dark and light colors xml files, how can I respond to the correct file depending on system theme?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    edited September 29

    This is a reasonable example project that shows how to have dark resources and light resources.

    Then all you need is to have a style based on DayNight in your styles.xml (also in the example project).

    Here is the android blog it comes from.

Sign In or Register to comment.