I'm working with my PreferenceActivity where I have a SwitchPreference to which I didn't assing a title using android:title="Title" because I'd like to change it whenever the Switch is clicked.
I've tried this way but didn't work. I also searched on SO related topics without solving my problem.
public class PrefsFragment extends PreferenceFragment {
private SwitchPreference pref;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
// Load the preferences from an XML resource
pref = (SwitchPreference)findPreference("SWITCH");
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.toString().equals("SWITCH")) {
boolean test = (Boolean) newValue;
if (test) {
pref.setTitle("ChangedTitle");
} else {
pref.setTitle("DefaultTitle");
}
}
return true;
}
});
}
}
settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Settings">
<ListPreference
android:key="yourPref"
android:title="Title"
android:dialogTitle="Select"
android:summary="%s"
android:defaultValue="4"
android:entries="#array/titles"
android:entryValues="#array/values"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Title">
<SwitchPreference
android:key="SWITCH"/>
</PreferenceCategory>
</PreferenceScreen>
Could you help me please?
Try calling preference.setTitle("YOUR TEXT"); within the listener callback, rather than pref.setTitle"YOUR TEXT"); The listener is passing in the preference that was changed.
The problem is with your OnSharedPreferenceChangeListener.
Instead of this,
pref = (SwitchPreference)findPreference("SWITCH");
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.toString().equals("SWITCH")) {
boolean test = (Boolean) newValue;
if (test) {
pref.setTitle("ChangedTitle");
} else {
pref.setTitle("DefaultTitle");
}
}
return true;
}
});
Try this
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
if (s.equals("SWITCH")) {
SwitchPreference sp = (SwitchPreference) findPreference(s);
boolean test = sharedPreferences.getBoolean(s, false);
if (test) {
sp.setTitle("ChangedTitle");
} else {
sp.setTitle("DefaultTitle");
}
}
}
}
Related
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
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();
}
});
This is my XML code, the ListPreference show the edited content with %s, but with the EditTextPreference it doesn't work.
<PreferenceCategory android:title="Player 1">
<EditTextPreference android:title="Nickname"
android:key="nickname1"
android:defaultValue="Player1"
android:summary="%s"/>
<ListPreference
android:defaultValue="4"
android:title="Selezione Colore"
android:entries="#array/colorSelect"
android:entryValues="#array/listvalues"
android:key="list_preference_1"
android:summary="%s"/>
</PreferenceCategory>
In the PrefActivity I've tried something like this but it isn't correct
public class PrefFragment extends PreferenceFragment {
EditTextPreference pref1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
pref1 = (EditTextPreference)findPreference("nickname1");
pref1.setSummary(pref1.getText());
pref1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
pref1.setSummary(pref1.getText());
return true;
}
});
}
I have those preference:
<xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="pk1"
android:title="#string/pt1"
android:summary="#string/pt1s"
android:defaultValue="false" />
<CheckBoxPreference
android:key="pk2"
android:title="#string/pt2"
android:defaultValue="false" />
<ListPreference
android:key="pk3"
android:title="#string/pt3"
android:dialogTitle="#string/pt3"
android:entries="#array/fontsi"
android:entryValues="#array/fontsiv"
android:defaultValue="0" />
<Preference
android:key="pkb"
android:title="#string/ptb" />
</PreferenceScreen>
And the settings activity:
public class SettingsActivity extends PreferenceActivity {
SharedPreferences.OnSharedPreferenceChangeListener lst;
SharedPreferences prf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
prf = getPreferenceScreen().getSharedPreferences();
lst = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.i("SettingsActivity","!any preference changen!");
if (key.equals("pk1")) { Log.i("SettingsActivity","!pref PK1 called!"); } }
else if (key.equals("pkb")) { Log.i("SettingsActivity","!pref PKB called!"); }
prf.registerOnSharedPreferenceChangeListener(lst);
}
#Override
protected void onResume() {
super.onResume();
prf.registerOnSharedPreferenceChangeListener(lst); }
#Override
protected void onPause() {
super.onPause();
prf.unregisterOnSharedPreferenceChangeListener(lst); }
...
}
The listener works with all the preferences but e custom preference (the last one, pkb as key)!
That i want to use as a back button.
Anybody knows why ?
Obviously your preference doesn't change any preferences.
Since it doesn't do anything, OnSharedPreferenceChangeListener ignores it.
A possible solution would be to set a clickListener to it like this:
findPreference("pkb").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.i("SettingsActivity", "!pref PKB called!");
return false;
}
});
My trouble is that when I'm trying to select an item from listpreference, the summary changes to the selected array key from array settings_listofitems_keys.
But I want to set the summary to a user readable format from array settings_listofitems_values on layout and at the same time save that value from settings_listofitems_keys as a parameter in preferences.
Xml arrays file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_listofitems_values">
<item>Green</item>
<item>Orange</item>
</string-array>
<string-array name="settings_listofitems_keys">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="#string/settings_select_horo" >
<ListPreference
android:defaultValue="#string/settings_selectitem"
android:key="preferenceitem"
android:entries="#array/settings_listofitems_values"
android:summary="#string/settings_plsselect"
android:entryValues="#array/settings_listofitems_keys"
android:title="#string/settings_plsselect" />
</PreferenceCategory>
</PreferenceScreen>
Code:
public class UserSettingActivity extends PreferenceActivity implements
OnPreferenceChangeListener {
private static final String APP_PREFERENCES = "mypref";
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
SharedPreferences settings = getSharedPreferences(APP_PREFERENCES,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
ListPreference myid = (ListPreference)
findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
editor.commit();
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString());
return true;
}
}
Any help is highly appreciated. I can also be a good example for newbies =)
All this with editors and commits and preferences does nothing - change it !
public class UserSettingActivity extends PreferenceActivity implements
OnPreferenceChangeListener {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
ListPreference myid = (ListPreference)
findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString()); // that's ok
return true;
}
}
Here is your issue - you must delete the summary line:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="#string/settings_select_horo" >
<ListPreference
android:defaultValue="#string/settings_selectitem"
android:key="preferenceitem"
android:entries="#array/settings_listofitems_values"
<!-- android:summary="#string/settings_plsselect"-->
android:entryValues="#array/settings_listofitems_keys"
android:title="#string/settings_plsselect" />
</PreferenceCategory>
</PreferenceScreen>
Instead of
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener {
private static final String APP_PREFERENCES = "mypref";
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
SharedPreferences settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
ListPreference myid = (ListPreference) findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
editor.commit();
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString());
return true;
}
Try using this:
public class UserSettingActivity extends PreferenceActivity {
private static final String APP_PREFERENCES = "mypref";
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
ListPreference myid = (ListPreference) findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int index = ((ListPreference) preference).findIndexOfValue(newValue.toString());
preference.setSummary(index >= 0 ? ((ListPreference) preference).getEntries()[index] : null);
return true;
}
});
}
}
Hope this helps :)
EDIT: I tried this new code but it still seems they are reset. Can anyone tell me, and propose a solution to my settings being reset?
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener{
static SwitchPreference mucus_stamps;
static SwitchPreference fertile_infertil;
static SwitchPreference cervical_mucus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* Populate the activity with the top-level headers.
*/
#Override
public void onBuildHeaders(List<Header> target) {
Log.i("PROJECTCARUSO","onBuildHeaders");
loadHeadersFromResource(R.xml.preference_headers, target);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.notification_settings);
}
}
/**
* This fragment shows the preferences for the second header.
*/
public static class Prefs2Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.charting_settings);
//findPreference("cervical_mucus").setOnPreferenceChangeListener(
mucus_stamps = (SwitchPreference) findPreference("mucus_stamps");
fertile_infertil = (SwitchPreference) findPreference("fertile_infertil");
cervical_mucus = (SwitchPreference) findPreference("cervical_mucus");
cervical_mucus.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// string test w/o "==" usage.
if (newValue.equals(false)) {
Log.i("PROJECTCARUSO","false");
mucus_stamps.setChecked(false);
fertile_infertil.setChecked(false);
} else {
Log.i("PROJECTCARUSO","true");
mucus_stamps.setChecked(true);
fertile_infertil.setChecked(true);
}
// true instead of false so the new value gets kept
return true;
};
});
if(!cervical_mucus.isChecked()){
mucus_stamps.setSelectable(false);
mucus_stamps.setEnabled(false);
fertile_infertil.setSelectable(false);
fertile_infertil.setEnabled(false);
} else {
mucus_stamps.setSelectable(true);
mucus_stamps.setEnabled(true);
fertile_infertil.setSelectable(true);
fertile_infertil.setEnabled(true);
}
}
}
protected void onPause()
{
super.onPause();
}
protected void onResume()
{
super.onResume();
}
#Override
public boolean onPreferenceChange(Preference arg0, Object arg1) {
// TODO Auto-generated method stub
return true;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="#string/pref_chart_profile"
android:textSize="20px">
<SwitchPreference
android:title="#+string/pref_symptothermal"
android:summary="#+string/pref_symptothermal_summary"
android:key="symptothermal"
android:defaultValue="true"
android:layout="#layout/pref_layout"/>
<SwitchPreference
android:id="#+id/cervical_mucus"
android:title="#+string/pref_cervical_mucus"
android:summary="#+string/pref_cervical_mucus_summary"
android:key="cervical_mucus"
android:defaultValue="true"
android:layout="#layout/pref_layout" />
<SwitchPreference
android:id="#+id/mucus_stamps"
android:title="#+string/pref_mucus_stamps"
android:summary="#+string/pref_mucus_stamps_summary"
android:key="mucus_stamps"
android:defaultValue="true"
android:layout="#layout/pref_layout" />
<SwitchPreference
android:id="#+id/fertile_infertil"
android:title="#+string/pref_fertile_infertile"
android:summary="#+string/pref_fertile_infertile_summary"
android:key="fertile_infertil"
android:defaultValue="true"
android:layout="#layout/pref_layout" />
</PreferenceCategory>
</PreferenceScreen>
Depending on what you want.
The value is saved(tested), but "selectable" is not a preference value. Therefore you have to set it when the preferencescreen is launched.
Do this:
Before your oncreate:
SwitchPreference mucus_stamps;
Then above your findPreference("mucus_stamps")
mucu_stamps = (SwitchPreference) findPreference("mucus_stamps");
Now, outside the onclicklistener, you can set it.
if(!mucus_stamps.isChecked()){
mucus_stamps.setSelectable(false);
mucus_stamps.setEnabled(false);
}
(I added "setEnablet" to show you that you can disable it aswell, not just make it "non-selectable". Selectable only says that you can't click anything outside the "off/on" area to change it to on/off.)
Personally I prefer to do it like this:
mucus_stamps = (SwitchPreference) findPreference("mucus_stamps");
mucus_stamps.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
// string test w/o "==" usage.
if (newValue.equals(false)) {
mucus_stamps.setChecked(false);
mucus_stamps.setSelectable(false);
} else {
mucus_stamps.setChecked(true);
}
// true instead of false so the new value gets kept
return true;
};
});
if(!mucus_stamps.isChecked()){
mucus_stamps.setSelectable(false);
mucus_stamps.setEnabled(false);
}
And, you don't need to implement OnPreferenceChangeListener when you do it the way you or I do. Not that it really matters.
:)