Tanay Neotia
Tanay Neotia

Reputation: 515

How can I get dark mode to work on my app?

I have set up a ListPreference with different values for the user's dark mode preference. Right now, I just want the app to work with the new dark mode implementation introduced in Android Q. If I set the system dark mode, the app changes because I have enabled isforcedarkallowed in styles.xml.

SettingsFragment:

public static class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
        PreferenceScreen screen = getPreferenceScreen();
        final ListPreference listPreference = (ListPreference) findPreference("theme");
        Preference preference = (Preference) findPreference("notq");
        int api = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
        if (api > 28) {
            screen.removePreference(preference);
        }
        if (api < 29) {
            screen.removePreference(listPreference);
        }
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                listPreference.setValue(newValue.toString());
                theme = String.valueOf(listPreference.getEntry());
                Log.d("debug", theme);
                if (theme.equals("Light")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
                }
                if (theme.equals("Dark")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
                }
                if (theme.equals("System default")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
                }
                return true;
            }
        });
    }
}

Arrays.xml (values for dark mode preference)

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Reply Preference -->
<string-array name="theme_entries">
    <item>Light</item>
    <item>Dark</item>
    <item>System default</item>

</string-array>

<string-array name="theme_values">
    <item>light</item>
    <item>dark</item>
    <item>default1</item>
</string-array>
</resources>

My listpreference:

 <ListPreference
    android:id="@+id/list"
    app:entries="@array/theme_entries"
    app:entryValues="@array/theme_values"
    app:key="theme"
    app:title="@string/theme_title"
    app:useSimpleSummaryProvider="true" />

And the app theme I'm using (styles.xml):

 <style name="AppTheme2" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:forceDarkAllowed" tools:targetApi="q">true</item>
    <item name="android:isLightTheme" tools:targetApi="q">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#FFFFFF</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Any suggestions?

Upvotes: 0

Views: 606

Answers (2)

Jack
Jack

Reputation: 1

String theme = String.valueOf(listPreference.getEntry()); 

Should work fine, you just didn't declare theme as a string

:)

Upvotes: 0

Rscalover
Rscalover

Reputation: 23

In your styles.xml you should not take Theme.AppCompat.DayNight.DarkActionBar as a parent while you enabled forceDarkAllowed. Change that to Theme.AppCompat or Theme.MaterialComponents. You cannot inherit from a daynight theme while forceDarkAllowed is set to true. You can read about that here https://developer.android.com/guide/topics/ui/look-and-feel/darktheme section forcedark.

Upvotes: 1

Related Questions