0

I am trying to implement a dark theme into my android app using the android daynight theme. It currently changes the theme but i do not think it recreates the activity like i want it to. In java they have AppCompatDelegate.setDefaultNightMode() which apparently now automatically recreates the activity but i cannot find the c# way to do this in xamarin android?

My current implementation is:

switch (selectedSpinnerItem)
{
    case "Light":
        ((AppCompatActivity)Activity).Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightNo);
        break;
    case "Dark":
        ((AppCompatActivity)Activity).Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightYes);
        break;
    case "System Preference":
        ((AppCompatActivity)Activity).Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightFollowSystem);
         break;
 }

I do not think this is the correct way to do it.

euandeas
  • 87
  • 1
  • 9
  • You can create serveral styles in the `style.xml`, Then set the theme by `AppSettings `, you can refer to this link.https://stackoverflow.com/questions/30174042/how-to-switch-themes-night-mode-without-restarting-the-activity – Leon Apr 01 '20 at 07:42
  • @LeonLu-MSFT I have worked on a version that implements this however when it came to detecting what theme the system was using it seemed better to use the daynight theme (https://developer.android.com/guide/topics/ui/look-and-feel/darktheme) however its been very difficult to find any c# copy of similar documentation although i have found bits here and their as seen above. – euandeas Apr 01 '20 at 08:07

1 Answers1

1

First of all, Your code just worked in the Android Q. https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force_dark

If you want it worked, you can add <item name="android:forceDarkAllowed">true</item> in your styles.xml like following code as well.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:forceDarkAllowed">true</item>
    </style>

</resources>

I set it use ((AppCompatActivity)this).Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightYes); in OnCreate method. Here is running sceenshot.

enter image description here

Update

When I change the theme, Activity will be created. enter image description here

Here is my demo. https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/App16.zip

Leon
  • 8,404
  • 2
  • 9
  • 52
  • I know that this works an i have got it sort of working, however it does not recrete the activity and destroy the old one like the method described at developer.android.com/guide/topics/ui/look-and-feel/darktheme. I am going to try experimenting a bit more but seems like its not implemented in xamarin so i will need to find a workaround. – euandeas Apr 04 '20 at 15:49
  • Does switch Light and the Dark theme that Activity will destroy, and then recreate? I test it in Android Q, it worked as normal. Please see my update answer. – Leon Apr 08 '20 at 07:24
  • @euandeas Are there any update for this issue? If the answer it helpful, Please don’t forget to mark this answer, it will help others who have similar issues. – Leon Apr 10 '20 at 08:39
  • Although this does help i have found a different easier solution and realised i was doing something wrong. Thanks for your help though. – euandeas Apr 10 '20 at 13:27
  • one question i have is for some reason when i switch to follow the system it does not change the the current OS theme if the previous theme was different from the OS theme...any fix for this? – euandeas Apr 14 '20 at 12:09
  • You can get the Current night mode first,then set it directly like Leo‘s answer: https://stackoverflow.com/questions/61229441/setting-android-theme-to-follow-system-does-not-change-to-current-system-theme – Leon Apr 22 '20 at 09:50