I have a PreferenceScreen defined in an XML-file like this:
<PreferenceCategory android:title="Choose Days">
<PreferenceScreen android:title="Days of Week" android:key="daysOfWeek">
<CheckBoxPreference android:title="Mondays" android:key="chkMonday"></CheckBoxPreference>
<CheckBoxPreference android:title="Tuesdays" android:key="chkTuesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Wednesdays" android:key="chkWednesday"></CheckBoxPreference>
<CheckBoxPreference android:title="Thursdays" android:key="chkThursday"></CheckBoxPreference>
<CheckBoxPreference android:title="Fridays" android:key="chkFriday"></CheckBoxPreference>
<CheckBoxPreference android:title="Saturdays" android:key="chkSaturday"></CheckBoxPreference>
<CheckBoxPreference android:title="Sundays" android:key="chkSunday"></CheckBoxPreference>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings">
<CheckBoxPreference android:title="Enable" android:defaultValue="true" android:key="enable"></CheckBoxPreference>
</PreferenceCategory>
When I click on the PreferenceScreen:daysOfWeek the checkboxes appear, og when I check or uncheck a box, the onSharedPreferenceChanged is triggered.
This is because of this:
public class Muteny extends PreferenceActivity implements OnSharedPreferenceChangeListener {
and register it in onResume and unregister it in onPause in my .java file.
A simplified onSharedPreferenceChanged looks like this:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
PreferenceScreen daysOfWeek = ((PreferenceScreen)findPreference("daysOfWeek"));
daysOfWeek.setSummary("text for summary");
Toast.makeText(this.getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
}
The problem is that the summary of "daysOfWeek" is never updated. If I then switch the CheckBoxPreference "enable" (which does nothing at the moment, but trigger the change), the summary is updated as I wanted it to do when switching the "day-boxes".
The message in Toast IS displayed though...
How do I update the summary of my PreferenceScreen when checking the "day boxes"?
This issue comes up frequently.
Here are a couple of other answers you can look at for possible solutions. I've never settled on a solution that makes me 100% happy though.
PreferenceScreen android:summary update !
Update existing Preference-item in a PreferenceActivity upon returning from a (sub)PreferenceScreen
Of all of the "solutions" I've seen the one that seems to work best is to call onContentChanged() after your setSummary(...) call. There is some debate as to whether this is safe to do.
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.startsWith("chk")) {
PreferenceScreen prefscreen = ((PreferenceScreen)findPreference("daysOfWeek"));
prefscreen.setSummary(key);
onContentChanged();
}
}
Related
I currently have the problem of a disabled checkbox preference which is enabled in the preference.xml. This happened after the migration to Metarial design. I tried to enable the view programmatically but it didn't help.
(I'd like to show a screenshot but my rep is not high enough yet :/)
my preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:enabled="true"
android:key="#string/communication_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="false"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:disableDependentsState="false"
android:enabled="true"
android:dependency="#string/communication_option"
android:key="#string/wifi_sync_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
</PreferenceScreen>
my preferenceFragment
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// added to make sure it really is enabled
CheckBoxPreference cbp = (CheckBoxPreference) getPreferenceManager()
.findPreference(getString(R.string.communication_option));
cbp.setEnabled(true);
cbp.setShouldDisableView(false);
}
I am using the Theme.AppCompat.Light.NoActionBar theme to be able to add the new toolbar.
An other thing i noticed is, that the preference is shown as enabled it the screen orientation changes to landscape and even though the preference is shown as disabled the preference can still be clicked/checked/unchecked.
After a few trial and error I found out that I just overloaded the Preferences.
The slimmer and much cleaner version that works looks like this:
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:key="#string/communication_option"
android:defaultValue="true"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:dependency="communication_option"
android:key="#string/wifi_sync_option"
android:defaultValue="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
and
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
You have to set the default values in you your main activity's onCreate.
PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);
I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="#string/summary_category"
android:title="#string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="#string/summary_main"
android:title="#string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="#string/summary_firstDependent"
android:title="#string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="#string/summary_secondDependent"
android:title="#string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="#string/summary_first"
android:title="#string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="#string/summary_second"
android:title="#string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="#string/summary_third"
android:title="#string/title_third" />
</PreferenceCategory>
I have some PreferenceActivity and there are some CheckBoxes.
<PreferenceScreen
android:key="prefGraphValues"
android:title="#string/pref_graph_values"
android:persistent="false" >
<CheckBoxPreference
android:defaultValue="true"
android:key="Temp.Dry"
android:summary="#string/pref_graph_value_temp_dry_summary"
android:title="#string/pref_graph_value_temp_dry" >
</CheckBoxPreference>
</PreferenceScreen>
Somwhere in code I have something like this:
checkBox.setEnabled(false);
But when I close this activity and later launch it, checkBox is enabled. Can I save this state?
Thanks
Change your persistent property to true.
android:persistent="true"
Actual, if you assign it to false it means that preference won't store value(s) to the SharedPreferences and this is reason why your state is not saved in "next launch".
You have to apply the change to sharedpreference and next time the checkbox will retrieve the value by the key from sharedpreference.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.edit().putInt(your_key, your_value).apply();
I have a Preference Activity
But, whenever I call it, the preferences are empty. No title or summaries are visible. You can click on the preference and it will highlight but there is no text. I have tried adding an EditTextPreference and in that case an edit text will pop up on click but there is no text.
Activity:
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="SomePref"
android:summary="Somethingsomething"
/>
</PreferenceScreen>
Thanks
You may try to add this inside PreferenceScreen and see what happens:
<PreferenceCategory
android:title="Preference Category">
<CheckBoxPreference
android:defaultValue="True"
android:key="checkbox_key"
android:title="CheckBox Title"
android:summaryOn="Checkbox is ticked"
android:summaryOff="Checkbox is not ticked" />
</PreferenceCategory>
I figured it out. I'm using the ActionBarSherlock library and I was extending the regular PreferencesActivity when it should have been Sherlock*PreferencesActivity
Everything works great now.
Always, throughout the app save your preferces through getApplicationContext(), if you use this as context it will save with activity context and cannot retreived all the time
I have write a multi-level Preferences in program with xml File. and i want to know how to return to the previous level without press the back button.
how to write some code to implement an item in preference to return previous Preferences level.
I had the same problem and solve it thanks to Xuelong and getDialog() but without needing to manage onPreferenceTreeClick().
You need to keep an instance (myPreferenceScreen) of the PreferenceScreen you want to return from
You have to give him a key in XML
Retrieve the instance with findPreference("MyPreferenceScreenKey");
Once you have to return , use this method : myPreferenceScreen.getDialog().dismiss()
You will then return from where you came from.
Here is a epurated example :
Xml file :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="contactList"/>
<PreferenceScreen android:title="My Sub Preference Screen ..."
android:key="mySubScreenKey">
<EditTextPreference android:key="foo1"/>
</PreferenceScreen>
</PreferenceScreen>
Java file :
public class ParanoidPreferenceManager extends PreferenceActivity {
ListPreference contactList;
EditTextPreference foo1;
PreferenceScreen mySubScreenKey;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load XML preference file
addPreferencesFromResource(R.xml.preferences);
contactList = (ListPreference) findPreference("contactList");
foo1= (EditTextPreference) findPreference("foo1");
screenContact = (PreferenceScreen) findPreference("screenAddContact");
foo1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mySubScreenKey.getDialog().dismiss();
return false;
}
});
}
}
That's it
Sorry for presentation, this is my first post on this site.
Bye dudes
Building on Patrice's and Xuelong's answers, I have made the following functions to easily do the job:
// Refresh the content of a PreferenceScreen by simulating a click (thus it will call the refresh code if it's contained inside the click callback)
private void refreshPreferenceScreenByClick(String prefScreenName) {
// Refresh preference screen (given by its name) by calling the click callback
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
Preference.OnPreferenceClickListener click_callback = prefScreen.getOnPreferenceClickListener();
click_callback.onPreferenceClick(prefScreen);
}
// Close the current PreferenceScreen (or any given the name)
// Useful to go back to the previous PreferenceScreen when constructing dynamically nested submenus.
private void closePreferenceScreen(String prefScreenName) {
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
prefScreen.getDialog().dismiss();
}
So first if what you do in your submenu define what is shown on the parent menu (eg in a music app: submenu allows to add a new instrument, and the parent menu shows the list of added instruments), you first have to call refreshPreferenceScreenByClick("parentPreferenceScreen"), and then to go back to the parent menu you can just close the current submenu by calling closePreferenceScreen("currentPreferenceScreen").
Alternatively, if you want to just open a submenu programmatically, you can use the openPreference function found here (tested and it works well): https://stackoverflow.com/a/4869034/1121352
NOTE: do NOT use the openPreference() function to go back to parent PreferenceScreen, because after a few iterations you will get a stack overflow and your app will crash (because Android will keep in memory the history of all previous menus, including the ones you open via openPreference()!).
You can simulate the press of the back button:
this.onBackPressed();
or
getActivity().onBackPressed();
if you are in a fragment.
I think you are looking for something like this
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="NotifSettings" android:title="Notification Settings">
<PreferenceCategory android:title="Notification Settings">
<CheckBoxPreference android:key="NotifyOption"
android:title="Notification Status" android:defaultValue="true"
android:summaryOn="Notifications are Enabled." android:summaryOff="Notifications are Disabled."
android:persistent="true" />
<Preference android:title="XXXX"
android:dependency="NotifyOption" android:summary="xxxxxx"
android:key="Xxx" />
</PreferenceCategory>
<PreferenceCategory android:title="YYYYY">
<PreferenceScreen android:title="Configure Notifications"
android:summary="yyyyy">
<CheckBoxPreference android:key="aa"
android:title="aaaaa" android:defaultValue="true"
android:summary="aaaa" />
<CheckBoxPreference android:key="bb"
android:title="bbbbb" android:defaultValue="true"
android:summary="bbbb." />
...
<ListPreference android:title="zzz"
android:summary="zzzz" android:key="zz"
android:defaultValue="0" android:entries="#array/OverDesc"
android:entryValues="#array/OverValues" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Alert Type">
<CheckBoxPreference android:key="AlertVibr"
android:title="Vibrate" android:defaultValue="true"
android:summaryOn="Vibration Turned ON" android:summaryOff="Vibration Turned OFF" />
<CheckBoxPreference android:key="AlertSound"
android:title="Sound" android:defaultValue="true" android:summaryOn="Sound Turned ON"
android:summaryOff="MUTE" />
</PreferenceCategory>
But what I didnt get is that your want to return to the previous preference screen, without pressing back button. Can you shed some more light on that??