Ad

SwitchPreference Not Working Properly - Why So?

I am using Switch Preference in my preference activity to change app theme but it is not working properly . I can't figure out what am i doing wrong here is my code

Here is my prefs.xml

    <SwitchPreference
        android:defaultValue="false"
        android:key="@string/pref_key_dark_mode"
        android:title="@string/pref_title_dark_mode"
        android:summary="@string/pref_summary_dark_mode"/>


</PreferenceCategory>

and here is my Settings activity

  public class SettingsActivity extends AppCompatPreferenceActivity {
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // load settings fragment
       getFragmentManager().beginTransaction().replace(android.R.id.content, new MainSettingsFragment()).commit();
}

public static class MainSettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs_main);

        // Stuff to do with night mode switch
        final SwitchPreference nightMode = (SwitchPreference) findPreference(getString(R.string.pref_key_dark_mode));
        nightMode.setDefaultValue(false);
        nightMode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                if (nightMode.isChecked()){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    getActivity().recreate();
                    nightMode.setChecked(false);

                }else {

                       AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    getActivity().recreate();
                    nightMode.setChecked(true);
                }

                return false;
            }
        });
    }
}
}

And here is what am i doing in my MainActivity.java

     private void setNightMode() {

    preferences = getPreferences(MODE_PRIVATE);
    if (preferences.getBoolean(getString(R.string.pref_key_dark_mode), true)) 
{

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

I am calling setNightMode(); before on create and onResume

Ad

Answer

Try This it should help

final SwitchPreference nightMode = (SwitchPreference) findPreference(getString(R.string.pref_key_dark_mode));
            nightMode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {

                if ((Boolean)newValue){
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    }else {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    }
                    getActivity().recreate();
            }
        });
Ad
source: stackoverflow.com
Ad