Allow switching theme

This commit is contained in:
Sylvia van Os
2019-12-02 16:24:24 +01:00
parent 9b5a731d48
commit 8d3d9a21f7
8 changed files with 80 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.IntegerRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatDelegate;
import protect.card_locker.R;

View File

@@ -1,9 +1,13 @@
package protect.card_locker.preferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.MenuItem;
import protect.card_locker.R;
@@ -44,12 +48,34 @@ public class SettingsActivity extends AppCompatActivity
public static class SettingsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
findPreference(getResources().getString(R.string.settings_key_theme)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if(o.toString().equals(getResources().getString(R.string.settings_key_light_theme)))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getActivity().recreate();
return true;
}
else if(o.toString().equals(getResources().getString(R.string.settings_key_dark_theme)))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getActivity().recreate();
return true;
}
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
getActivity().recreate();
return true;
}
});
}
}
}