Added OnPreferenceChangeListener

This commit is contained in:
luca0N! 2021-03-17 18:19:17 -03:00
parent c7e81eb66b
commit 5ac835ce0f
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
4 changed files with 33 additions and 10 deletions

View File

@ -30,16 +30,19 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class SettingsActivity extends AppCompatActivity { public class SettingsActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
if (actionBar != null) if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
getFragmentManager() getSupportFragmentManager()
.beginTransaction() .beginTransaction()
.replace(android.R.id.content, new SettingsFragment()) .replace(android.R.id.content, new SettingsFragment())
.commit(); .commit();

View File

@ -24,15 +24,32 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.settings; package com.luca0n.joguitos.pluck.settings;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceFragment; import androidx.preference.PreferenceFragmentCompat;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.luca0n.joguitos.pluck.R; import androidx.preference.ListPreference;
import androidx.preference.Preference;
public class SettingsFragment extends PreferenceFragment { import com.luca0n.joguitos.pluck.R;
@Override import com.luca0n.joguitos.pluck.util.UiUtil;
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); public class SettingsFragment extends PreferenceFragmentCompat {
addPreferencesFromResource(R.xml.preferences); @Override
} public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey){
addPreferencesFromResource(R.xml.preferences);
// Listen for theme preference changes
ListPreference preferenceTheme = (ListPreference) findPreference(getString(R.string.pref_ui_theme));
preferenceTheme.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(Preference preference, Object newValue){
getActivity().recreate();
return true;
}
});
}
} }

View File

@ -38,7 +38,6 @@ public class UiUtil {
public static void setTheme(Context context){ public static void setTheme(Context context){
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String light = context.getString(R.string.pref_ui_theme_entryValues_light), String light = context.getString(R.string.pref_ui_theme_entryValues_light),
dark = context.getString(R.string.pref_ui_theme_entryValues_dark), dark = context.getString(R.string.pref_ui_theme_entryValues_dark),
themeKey = context.getString(R.string.pref_ui_theme), themeKey = context.getString(R.string.pref_ui_theme),

View File

@ -28,4 +28,8 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="pref_network_tor">pref_network_tor</string> <string name="pref_network_tor">pref_network_tor</string>
<string name="pref_network_server">pref_network_server</string> <string name="pref_network_server">pref_network_server</string>
<string name="pref_network_server_default">https://opentdb.com/api.php</string> <string name="pref_network_server_default">https://opentdb.com/api.php</string>
<string name="pref_ui_theme">pref_ui_theme</string>
<string name="pref_ui_theme_entryValues_light">pref_ui_theme_entryValues_light</string>
<string name="pref_ui_theme_entryValues_dark">pref_ui_theme_entryValues_dark</string>
<string name="pref_ui_theme_default">pref_ui_theme_entryValues_light</string>
</resources> </resources>