Changing summary from outside preferenceactivity - android

I am using a preferenceActivity in android to manipulate my shared preferences. I figured out how to change the summary with an OnSharedPreferenceChangeListener from within the preferenceActivity. However, I am using a picker wheel so I use an intent to launch it from the preference activity like this...
<Preference
android:summary="Snooze Picker Wheel"
android:title="Snooze Timer"
android:key="snooze_pref">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.alarm.SnoozePicker"
android:targetPackage="com.example.alarm" />
</Preference>
I've been researching, but I cannot find a way to update the summary of the picker wheel to the item that is selected.
So my question is, is there a way to change the summary of a preferenceActivity from a different activity? What would be the implementation for that, and where could I learn more about specifically changing the summary from a different activity? Thank you for any help.
Edit: I got something working by setting a shared preference in the snoozePicker activity and then setting it like this in the preference activity in onCreate and onResume
if (sharedpreferences.contains(Snooze)) {
int prefs = sharedpreferences.getInt(Snooze, -1);
Preference editSnoozePref = (Preference) findPreference("snooze_pref");
editSnoozePref
.setSummary("Snooze Setting is " + prefs + " minutes");
}

AFAIK, it is not possible to change summary from other Activity.
As a work around, you can save the summary in a SharedPreference and set this summary when your PreferenceActivity is created. It will be seamless experience for the user as the summary will be visible only after PreferenceActivity is created.

Related

SharedPreferences dependencies

I wanted to create ListPreference which, when changed will also reload values of others ListPreference objects. I tried to do that by calling:
if(key.equals("important_pref")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("some_pref", "some_val");
editor.apply();
}
in onSharedPreferenceChanged function.
It does the work but I need to reload preferences screen to see the efect. Is there a way to avoid that and reload values instantly?
My guess is that I can't do this that way, because the first commit needs to be ended before changing something else.
This is a very common requirement that is unfulfilled I'm afraid. You either have to reload the screen by hacks like setPreferencesScreen(null); or (better) register a SharedPreferencesChanged listener and update the prefs manually. See : update preferences values after changing them programatically
In your case you should add if clauses in your existing SharedPreferencesChanged listener to "hear" the change in the "some_pref" preference and update the displayed value manually.

CheckBoxPreference Default Value

I have a CheckBoxPreference defined as follows:
<CheckBoxPreference
android:defaultValue="true"
android:key="prefVisible"
android:summary="#string/pref_visible_summary"
android:title="#string/pref_visible" >
</CheckBoxPreference>
My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.
Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?
Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true
This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.
No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html

OnClick in PreferenceFragment Android?

I am displaying my "Settings" screen through a class that extends PreferenceFragment. I need to know when a particular setting is clicked so that I can handle the Intent to launch at runtime, as opposed to declaring in xml like this:
<PreferenceScreen
android:title="#string/terms_conditions"
android:summary=""
android:layout="#layout/settings_item">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.somecompany.com/m/EULA.aspx" />
</PreferenceScreen>
I need to fire a different Intent depending on other variables involved.
So, what I need is to know when the user clicks the "Terms and Conditions" within the settings fragment.
Have the class in which you need to know about config changes implement
SharedPreferences.onSharedPreferenceChangeListener
and install it as a listener in your SharedPreferences via
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(listener);
Then handle the changes is
onSharedPreferenceChanged()
The relevant documentation is about SharedPreferences and SharedPreferences.OnSharedPreferenceChangeListener

get and set listPreference from another activity not working

I'm trying to get and set a listPreference value from different activities and it's not working.
When I read and write it from my main activity, it only keeps whatever I write, so I'm assuming that I'm not targeting the listPreference correctly when I'm out of the activity because it's working inside my preference activity no problem.
I've seen some references on the developer website to CharSequence with getValue and getEntryValues but I haven't had luck getting them to work correctly either.
Here is my code for clicking a button and setting the listpreference value then it launches an intent to switch activities:
Main Activity, attempting to set the value of the listpreference to the first index value;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ListPreferenceInXML", "1");
editor.commit();
String levelCheck = settings.getString("ListPreferenceInXML","1");
In my next activity I call read the value on launch to see which listPreference is active and it is always the number I write from the mains activity listed above. The problem is when I goto the actual Preference activity and it doesn't match or update when I change it on the ListPreference and launch the same activity from there (it still reads the value I set from the Main activity button)
code as follows for activity trying to read ListPreference:
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();
So I finally figured it out, the problem was with the way I was calling the preferences. Instead of calling the preferences like this, in both cases;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Call them like this:
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
I'm not sure if there is a step missing out of the first way of calling the preferences but this 2nd way worked like a champ.

How to Initialize Preferences Screen on First Invocation?

I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.
However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.
Since all I do to invoke the Preferences Screen is
startActivity(new Intent(this, EditPreferences.class));
And my EditPreferences class only contains:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);
}
I am not sure where or how to tell it to pre-initialize the visual display with the default setting.
I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?
Can't you define the default value in the XML itself?
<CheckBoxPreference ...
android:defaultValue="true"
... />
You can specify a default value on a preference (in your xml layout for example):
<EditTextPreference android:defaultValue="whatever" />

Categories

Resources