How to implement a transition from light mode to dark mode on a fragment

Viewed 2461

I have attempted to add a dark mode to my application. When I activate dark mode on my settings fragment, the screen flashes, and the page stays in light mode. However when I transition to a different fragment, whether this be through the backstack or opening a new fragment, that fragment is in dark mode. Only upon going back to the settings fragment will it then be in dark mode. But when I disable dark mode on the settings page, it immediately goes back to light mode. Why isn't it transitioning from light mode to dark mode, yet it'll transition from dark mode to light mode. According to my previous research, I will have to reload the fragment onto the activity, I have attempted to do this, but with no success.

enter image description here

XML layout file for setting fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsFragment"
    android:background="?attr/backgroundColor">

styles.xml:

<resources>
    <!-- Light/Day Mode theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#424242</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundColor">#fcfcfc</item>
        <item name="cardBackgroundColor">#ffffff</item>
        <item name="android:textColor">#808080</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="boxBackgroundColor">#fcfcfc</item>

        <item name="android:windowContentTransitions">true</item>
    </style>

    <!-- Night / dark Mode theme -->
    <style name="darktheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#212121</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundColor">#000000</item>
        <item name="cardBackgroundColor">#424242</item>
        <item name="android:textColor">#ffffff</item>
        <item name="editTextColor">#ffffff</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="alertDialogTheme">@style/AlertDialogCustom</item>
        <item name="boxBackgroundColor">#303030</item>
        <!-- <item name="backgroundColor">#303030</item> -->

        <item name="android:windowContentTransitions">true</item>
    </style>

Settings fragment java code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);

        if(AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES)
        {
            getActivity().setTheme(R.style.darktheme);
        } else {
            getActivity().setTheme(R.style.AppTheme);
        }
        myswitch = view.findViewById(R.id.myswitch);
        if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES)
        {
            myswitch.setChecked(true);
        }

        myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    //Objects.requireNonNull(getActivity()).recreate();
                    //getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new SettingsFragment()).commit(); //<== both these two lines not working

                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                }
            }
        });
1 Answers

maybe because in this line <style name="darktheme" parent="Theme.AppCompat.Light.NoActionBar"> you were adding the parent as Light not DayNight so change this line to <style name="darktheme" parent="Theme.AppCompat.DayNight.NoActionBar"> and I hope it will work

Related