Reputation: 1886
I created a Setting by using PreferenceFragmentCompat
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.settings_pref);
Preference pref = findPreference("theme_key");
SwitchPreference swPref = (SwitchPreference) pref;
pref.setOnPreferenceChangeListener((preference, o) -> {
boolean state = Boolean.valueOf(o.toString());
String summaryValue;
if(state){
summaryValue = swPref.getSwitchTextOn().toString();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
restartActivity();
}else {
summaryValue = swPref.getSwitchTextOff().toString();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
restartActivity();
}
Toast.makeText(getContext(), "Theme " + summaryValue, Toast.LENGTH_SHORT).show();
return true;
});
}
private void restartActivity() {
Intent i = new Intent(getActivity(), SettingsActivity.class);
startActivity(i);
getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
getActivity().finish();
} }
and my xml file (settings_pref) looks like below:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="theme_key"
android:defaultValue="false"
android:title="@string/title_theme_pref"
android:summaryOn="Dark"
android:summaryOff="Light"
android:textColor="?attr/textcolor"<!--this textColor not working, i have test with different color based on theme, still has no effect-->
android:textColorHint="?attr/textcolor" />
</PreferenceScreen>
as functionality it's work fine, but i still have not found literature to change Summary TextColor of my SwitchPreference which is follows current value.
Problem shows as Image like below
Upvotes: 2
Views: 498
Reputation: 11
I have encountered similar problem. I scoured the internet and finally found the problem
Change summary text color of dialogpreference with preference V7
To summarize as follows:
themes.xml
<style name="Theme.xxx.PreferenceSumary">
<item name="android:textColorSecondary">@color/gray</item>
</style>
AndroidManifest.xml
<activity
***
android:theme="@style/Theme.xxx.PreferenceSumary"
*** />
Upvotes: 1