I have two preference xml files. The first one (pref2.xml) contains 2 preferences:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
<CheckBoxPreference
android:key="key2"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
and the other one (pref1.xml) contains 1 preference:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
in my preference activity I am trying to change them:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref1);
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref2);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences"+adapter.getCount()+"(should be 2)");
actually here i get the right output and everything is displayed correctly. But I want to change the displayed preferences concerning one preference. Therefore I implemented the OnSharedPreferenceChangeListener in the preference activity:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
super.onSharedPreferenceChanged(sharedPreferences, key);
//switch the widget type
if (key.equals("key")){
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref1);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences "+adapter.getCount()+" (should be 1)");
}
}
Well, the pref1 preferences are displayed correctly, but the output of
"size of sharedPreferences 2"
indicated that in the background there are still the old preferences applied. If i iterate over the listAdapter i get also the old preferences.
Any idea how I could solve this?
I found out that the listadapter is some how outdated. If I get the count of items via getPreferenceScreen().getPreferenceCount() I get the right amount. But how do I access these preferences ?
I'm fairly sure that problem lies with your having two preferences with a key of "key" even though they are on different screens. A preference is identified by its key and this needs to be unique to avoid conflicts. The listener only knows which preference is being changed from the key. I would change the keys to have unique values.
EDIT: Well, wasn't right :S
...
Okey, I finally found the solution. After changing the PreferenceScreen I need to bind manually the listView again:
getPreferenceScreen().bind((ListView)findViewById(android.R.id.list));
Related
I am now creating a PreferenceFragment with nested PreferenceScreen.
The preferences_layout.xml looks like below
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/preference_setting_title" >
<SwitchPreference
android:key="#string/pref_key_switch"
android:title="#string/title__preference"
android:summary="#string/summary_title_preference"
android:defaultValue="true" />
<CheckBoxPreference
android:key="#string/pref_key_auto_enable"
android:title="#string/title_auto_enable_preference"
android:summary="#string/summary_auto_enable_preference"
android:dependency="#string/pref_key_switch"
android:defaultValue="true" />
<PreferenceScreen
android:key="#string/pref_key_enabled_items"
android:title="#string/title_enabled_items"
android:summary="#string/summary_enabled_items"
android:dependency="#string/pref_key_switch" >
</PreferenceScreen>
</PreferenceScreen>
The nested PreferenceScreen perference items will be dynamically added on that PreferenceScreen onClick.
Due to need to check the items already exist before adding a new one, so i use the following ways to check:
int count = enabledPreferenceScreen.getPreferenceCount();
for (int i = 0 ; i < count ; ++i) {
//Do the check with new one
}
All works ok. But the problem is that when this setting activity is destroyed and relaunch again, enabledPreferenceScreen.getPreferenceCount() will become 0.
So i check with the app/shared_prefs/xxxx.xml, the values are saved, but the problem is that there is no nested screen hierarchy information in that store value xml.
How to solve this issue? Rebuild the PreferenceScreen from the preference_layout.xml cannot rebuild the daynamic add items preferences.
Is it possible to save the nested PreferenceScreen data into another /shared_prefs/xxx2.xml? How to achieve it?
Thanks a lot.
I have two roughly connected issues in my new Android app regarding the Settings. The former is the need to retrieve the information from the preferences without displaying the specific setting activity by performing some:
addPreferencesFromResource(R.xml.preferences);
in another activity.
Is it possible to do it or how else may I retrieve the data without displaying the activity, or is it possible to start the activity without displaying it?
The latter more distressing one is that even in the SettingActivity, when I call that function I recently started getting a crash complaining:
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String
Like if something internal were set somehow expecting something. What may I do and in particular how may I reset this sort of hidden structure?
This is my xml file: if I just keep the entries in the former category it does not crash, if I add any field in the latter it does:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/switches"
android:key="switches">
<EditTextPreference
android:key="night_switch_start"
android:summary="#string/ext_night_switch_start"
android:defaultValue="22"
android:title="#string/night_switch_start" />
<EditTextPreference
android:key="day_switch_start"
android:summary="#string/ext_day_switch_start"
android:defaultValue="6"
android:title="#string/day_switch_start"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/tokens"
android:key="tokens">
<EditTextPreference
android:key="token_day"
android:summary="#string/ext_token_day"
android:defaultValue="3"
android:title="#string/token_day" />
</PreferenceCategory>
</PreferenceScreen>
Thanks, Fabrizio
For you first question, you can retrieve a preference saved in a PreferenceActivity by calling PreferenceManager#getDefaultSharedPreferences(Context). This is a static method, so you can have in your Activity something like:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
About your second question, I'm not sure what is happening. Try cleaning the project before you run it again.
I answer here to be able to show my code better: this is the first summarizing part of the xml file I suspect might have problems:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/switches"
android:key="switches">
<EditTextPreference
android:key="night_switch_start"
android:summary="#string/ext_night_switch_start"
android:defaultValue="22"
android:inputType="numberDecimal"
android:title="#string/night_switch_start" />
<EditTextPreference
android:key="day_switch_start"
android:summary="#string/ext_day_switch_start"
android:defaultValue="6"
android:inputType="numberDecimal"
android:title="#string/day_switch_start"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/tokens"
android:key="switches">
<EditTextPreference
android:key="token_day"
android:summary="#string/ext_token_day"
android:defaultValue="3"
android:inputType="numberDecimal"
android:title="#string/token_day" />
</PreferenceCategory>
</PreferenceScreen>
My code is very simple: the crashing setting part is the following.
public class SettingsActivity extends PreferenceActivity {
private static final String LOG_TAG = "preferences";
public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
While the code loading the string and finding "mostly" empty strings is:
sharedPref= PreferenceManager.getDefaultSharedPreferences(Dashboard.dashboard);
startDayTime=Integer.parseInt(sharedPref.getString("day_switch_start", "")); //correct value
startNightTime=Integer.parseInt(sharedPref.getString("night_switch_start", "")); //empty string thereafter rising an exception.
Also, when I tried leaving just the first two EditText, so not to have the crash, the Setting activity had the correct value for day_switch_start and again an empty string in the night_switch_start field.
When I do getAll() I get the correct values:
{start_night_time=6, day_switch_start=6, notifications_new_message_ringtone=content://settings/system/notification_sound, notifications_new_message=true, second_rate_limit=24.0, third_rate_fare=1.6, token_day=6.0, token_night=6.0, night_switch_start=22, token_holiday=6.0, start_day_time=6, start_night_switch=John Smith, example_checkbox=true, example_text=John Smith, notifications_new_message_vibrate=false, example_list=-1, first_rate_fare=1.1, first_rate_limit=11.0, max_speed_for_time=20.0, sync_frequency=180, second_rate_fare=1.3, timed_fares=27.0, cooperative=}
Yet, when I try to get the value of them either with:
startDayTime=Integer.parseInt(sharedPref.getString("night_switch_start", "")); or
startNightTime=sharedPref.getInt("night_switch_start", 0);
I retrieve an empty string in both cases.
Half of the answer: clearing the cache of the phone removes all the funny fields and does not crash any longer the setting activity, nor the loading of its values by:
startNightTime=Integer.parseInt(sharedPref.getString("night_switch_start", ""));
The old form:
startNightTime=sharedPref.getInt("night_switch_start", 0);
still crashes, notwithstanding the values seem numeral. Not a big deal, though.
The unanswered problem is how to load the settings from the xml file without entering the SettingsActivity performing:
addPreferencesFromResource(R.xml.preferences);
Is it possible to run the function from outside that activity or is it possible to start the activity without visualizing it to the user?
I have searched here and looked at samples but haven't yet found an answer to what is essentially a simple problem. Depending on the choice made in a preceding ListPreference, I want to build a preference screen of CheckBoxPreferences dynamically in code, which is then shown when I click on a simple preference with a click listener. The list and number of check boxes will be different in each case.
This is where I have got to so far - just a simple bit of code to test the concept in the onClick listener, but how to get the check box preference screen to appear? There must be a simple explanation why it doesn't. What am I doing wrong?
Part of my xml code:
<PreferenceCategory android:title="Filters">
<PreferenceScreen android:key="FilterScreen"
android:title="Filters" android:summary="Click to change filter settings">
<ListPreference android:title="Filter type"
android:summary="Set to: Gliding"
android:key="filterType"
android:defaultValue="0"
android:entries="#array/filterTypeOptions"
android:entryValues="#array/filterTypeValues" />
<CheckBoxPreference android:title=""
android:summary="Include Aerodrome Notams"
android:defaultValue="false" android:key="filterIncludeAerodrome" />
<CheckBoxPreference android:title=""
android:summary="Delete night-time Notams"
android:defaultValue="true" android:key="filterDeleteNighttime" />
<ListPreference android:title="Select category to change"
android:summary="Set to: Airspace organisation"
android:key="filterCategory"
android:defaultValue="0"
android:entries="#array/filterCategoryOptions"
android:entryValues="#array/filterCategoryValues" />
<Preference android:title="Show filters for category"
android:summary="Click to choose subjects to delete"
android:key="filterShow" />
</PreferenceScreen>
</PreferenceCategory>
The contents of "Show filters for category" will depend on the "Filter type" and "Select category to change" settings.
This is the simple test code I have for the "Show filters" click listener (cut down just to show essentials):
public class Settings extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
------
public static final String KEY_FILTER_SHOW = "filterShow";
------
private Preference mFilterShow;
------
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.settings);
// Get a reference to the preferences
------
mFilterShow = (Preference)findPreference(KEY_FILTER_SHOW);
------
// Set the click listener for Show Filter options
mFilterShow.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference pref)
{
Context ctx = pref.getContext();
PreferenceScreen screen =
pref.getPreferenceManager().createPreferenceScreen(ctx);
CheckBoxPreference cb1 = new CheckBoxPreference(ctx);
cb1.setTitle("This is cb1");
cb1.setKey("cb1_key");
cb1.setDefaultValue(false);
screen.addPreference(cb1);
return true;
}
});
OK, I have solved the problem myself, through a process of iteration! Others might find this useful.
Just create an empty PreferenceScreen in the xml:
<PreferenceScreen android:title="Show filters for category"
android:summary="Click to choose subjects to delete"
android:key="filterShow">
</PreferenceScreen>
Then in the code there is no need for the onClick listener - the contents of the screen are created in the onCreate function. Actually, since the contents of the screen need to change when the choice made in the Category list preference (see original code) changes, this needs to go in a separate function which is called both from onCreate and onSharedPreferenceChanged:
public static final String KEY_FILTER_SHOW = "filterShow";
...
private PreferenceScreen mFilterShow;
...
// In onCreate:
// Get a reference to the PreferenceScreen
mFilterShow =
(PreferenceScreen)getPreferenceScreen().findPreference(KEY_FILTER_SHOW);
// Now the code to create the contents of the screen
mFilterShow.removeAll();
CheckBoxPreference cb1 = new CheckBoxPreference(this);
cb1.setTitle("This is cb1");
cb1.setKey("cb1_key");
cb1.setDefaultValue(true);
mFilterShow.addPreference(cb1);
The above is just "proof of concept". It works exactly as you would expect. In my final version, I will create an array of CheckBoxPreferences with 'new' initially, then re-use them (changing title and default) when setting up the contents of the screen for each Category choice as it changes. The number of check boxes required may be different for each category - I will create an array for the maximum number required, then add as many as I need in each case.
I feel I'm missing something obvious, but search took me to several different hits, all of which don't directly access my odd issue.
Have an app with a main activity and a preference activity. Add to that a 'preference' class, which simplifies reading and setting preferences. The main activity has an option menu to get to the preference activity:
Preferences class (included for relevance, same thing happens if I don't use this class to read settings).
public class Preferences
{
public static SharedPreferences getPrefs(Context context)
{
SharedPreferences retCont = PreferenceManager.getDefaultSharedPreferences(context);
return retCont;
}
/* Map Page: Show Satellite */
public static boolean getMapShowSatellite(Context context)
{
return Preferences.getPrefs(context).getBoolean(Preferences.getString(context, R.string.option_showSatellite), false);
}
public static void setMapShowSatellite(Context context, boolean newValue)
{
Editor prefsEditor = Preferences.getPrefs(context).edit();
prefsEditor.putBoolean(Preferences.getString(context, R.string.option_showSatellite), newValue);
prefsEditor.commit();
}
}
PreferencesActivity:
public class AppSettings extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preferences);
ListPreference stationType = (ListPreference)this.findPreference(this.getString(R.string.option_filterStationType));
stationType.setOnPreferenceChangeListener(this.stationOrderEnable());
}
[...]
}
The last two lines hook up an event to enable/disable other preferences based on one's selection. That works, as expected.
The simple main activity, and related functions:
public class MainMapScreen extends MapActivity
{
private void launchSettings()
{
Intent prefsIntent = new Intent(this.getApplicationContext(), AppSettings.class);
this.startActivity(prefsIntent);
}
#Override
protected void onResume()
{
super.onResume();
Preferences.getMapShowSatellite(); // <-- Returns previous value.
// Re-start the MyLocation Layer from tracking.
this._mapView.requestLayout();
}
[...]
}
Okay, so what happens is, let's say we run the app. At app load, the getMapShowSatellite() returns True. Go into the PreferenceActivity, and change that option to False. Exit the PreferenceActivity by hitting the Back button. At this time, the main activity's onResume() is called. Getting the getMapShowSatellite() at this point returns the previous setting of True. Exiting and relaunching the app will then finally return the False expected.
I'm not calling .commit() manually - and don't think I need to, sicne the setting IS saving, I'm just not getting update values.
What'm I missing? :)
--Fox.
Edit 2: Small update. I thought the issue may be the static calls - so temporarily I changed over my Preferences class (above) to be a instantiated class, no more static. I also added the following code to my onResume() call in the main activity:
//Try reloading preferences?
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String test = sp.getString(Preferences.OPTION_FILTERSTATIONTYPE, "---");
Log.e("BMMaps", test);
What is logged at this point, from leaving the PreferenceActivity, is the old setting. Manually reading the preferences file shows me that the .xml file is getting updated with the user's new setting.
Since it's not obvious, I am hooked into Google's Maps API. Because of this, I had to specify two ifferent processes - one for the Main activity (this one) and another for an activity not related to this issue. All other activities, including the PreferencesActivity have no specified android:process="" in their definition.
Edit 3:
As requested, here's the data preferences file:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="com.tsqmadness.bmmaps.filterStationType">V-?</string>
<boolean name="com.tsqmadness.bmmaps.deviceHasLocation" value="false" />
</map>
And here is the Preference storage XML file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Map Options">
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowSatellite" android:order="1" android:summary="Whether or not to show satellite imagery on the map." android:summaryOff="Standard road map will be shown." android:summaryOn="Satellite imagery will be show." android:title="Show Satellite Layer?" />
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowScale" android:order="2" android:summary="Whether or not to show the distance bar on the map." android:summaryOff="The distance bar will not be shown on the map." android:summaryOn="The distance bar will be shown on the map." android:title="Show Map Scale?" />
<CheckBoxPreference android:defaultValue="false" android:key="com.tsqmadness.bmmaps.useMetric" android:order="3" android:summary="Whether to use Metric os SI values." android:summaryOff="SI units (mi/ft) will be shown." android:summaryOn="Metric units (km/m) will be shown." android:title="Use Metric?" />
<ListPreference android:dialogTitle="Station Load Delay" android:entries="#array/static_listDelayDisplay" android:entryValues="#array/static_listDelayValues" android:key="com.tsqmadness.bmmaps.mapBMDelay" android:negativeButtonText="Cancel" android:order="4" android:positiveButtonText="Save" android:summary="The delay after map panning before staions are loaded." android:title="Delay Before Loading" />
</PreferenceCategory>
<PreferenceCategory android:title="Control Station Filter">
<ListPreference android:dialogTitle="Station Type" android:entries="#array/static_listStationTypeDisplay" android:entryValues="#array/static_listStationTypeValues" android:key="com.tsqmadness.bmmaps.filterStationType" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="The station type to filter on." android:title="Station Type" android:order="1" />
<ListPreference android:dialogTitle="Select Station Order" android:entries="#array/static_listStationHOrderDisplay" android:entryValues="#array/static_listStationHOrderValues" android:key="com.tsqmadness.bmmaps.filterStationOrder" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station Order to filter by." android:title="Station Order" android:order="2" />
<ListPreference android:dialogTitle="Select Station Stability" android:entries="#array/static_listStationStabilityDisplay" android:entryValues="#array/static_listStationStabilityValues" android:key="com.tsqmadness.bmmaps.filterStationStability" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station stability to filter by." android:title="Station Stability" android:order="3" />
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.filterNonPub" android:summaryOff="Non-Publishable stations will not be shown." android:defaultValue="false" android:summaryOn="Non-Publishable stations will be shown on the map." android:order="4" android:title="Show Non-Publishable" />
</PreferenceCategory>
<Preference android:key="temp" android:title="Test" android:summary="Test Item">
<intent android:targetClass="com.tsqmadness.bmmaps.activities.MainMapScreen" android:targetPackage="com.tsqmadness.bmmaps" />
</Preference>
</PreferenceScreen>
When changing the filterStationType parameter, and hitting the back button out of PreferenceActivity changes the preferences file from the above from V-? to H-?, as it should. However, reading the value from the SharedPreferences on the main activity still gives the V-?, until app restart. Ah, also, I have a OnPreferenceChangeListnener() in the PreferenceActivity, and that is called when the value changes.
Final Edit: Apparently, it's the use of named android:process for the given activity. This is needed for Google maps API to allow two separate MapActivitys in the same app use different settings. If the PreferenceActivity is moved to the same named-process, then the code above, reading the setting in the onResume() returns the correct value.
Since everything checks out, my guess is that you have a typo, or incorrect or undefined, key in your PreferenceActivity's app_preferences.xml file for the key R.string.option_showSatellite (whatever that string is). This would result in two keys with [unbeknownst to you] different names that you think point to the same value. Really, the prefs activity is using one key and your Preference class is using the other -- resulting in two different keys and two different values.
Double check your keys. Make sure you are not also using the literal "R.string.options_showSatellite" as the key in the xml file, but rather, the actual string. If you want to use the localized version then #string/options_showSatellite would work for the key. However, keys need not be localized.
If you're curious, this can be double checked by opening the preference file that is created by the preference manager in your app's data directory in a standard text editor.
onResume you have to get a fresh reference to SharedPreferences, otherwise you're just using an object that is already in memory (and has your old values)
EDIT:
Rather than down-voting answers that you don't understand, why not ask for clarification instead?
What I mean is that you should pull your Prefs the correct way (rather than how you're doing it)...
SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
And then see what happens.
Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="#string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="#string/SOME_SUMMARY"
android:title="#string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example:
We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
You can create a subclass of ListPreference in which you set an OnPreferenceChangedListener from which you will have access to the new value, and set the text on your ListPreference. I think the setSummary() function on the ListPreference will update the text under the name of the preference. If that doesn't work you can also override getView() to implement your own custom view for the Preference on which you can set the text directly.