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
Related
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.
I have a class which extends from ListView. I added some extra functionality(drag and drop) for this new class. My question is there any way i can use this extended ListView class in a preference activity.
I need to provide drag and drop functionality for a legacy preference activity.
Are you asking if it's possible to open an arbitrary activity from a clicked item in a preference activity? If so, you need to do two things. First, add a PreferenceScreen item to your preferences xml file:
<PreferenceScreen
android:key="CUSTOM_ACTIVITY_KEY"
android:title="Title"
android:summary="Summary" />
Then in your settings activity's onCreate:
Preference pref = getPreferenceScreen().findPreference("CUSTOM_ACTIVITY_KEY");
final Intent intent = new Intent(this, CustomActivity.class);
if (pref != null)
{
pref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(final Preference preference)
{
startActivity(intent);
return false;
}
});
}
If running a custom activity off of a PreferenceScreen item isn't enough, the only other option I could think of is to roll your own Preferences implementation. Someone else should correct me if I'm mistaken, but I think it might be possible to pull the Preferences source and modify accordingly.
I came across another API in
android.preference.PreferenceScreen.bind(ListView listView)
Binds a ListView to the preferences contained in this PreferenceScreen via getRootAdapter().
So after inflating the layout file, we will be able to attach the our custom listview to this the preferenceScreen of the activity.
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" />
Inside my Configuration activity i need to create a preference screen with a fixed View at the top showing a preview of content configured in the page. I don't want to change the main preference screen (i have already a separate activity for that) i want a different layout for a "nested" preferencescreen.
What i've tried is specifying an Intent inside the preference screen however when i click on this options nothing happens and activity goes into timeout... Activity is correctly configured on the manifest (and extends ConfigureActivity as the main one does).
<PreferenceScreen
android:key="inner"
android:title="Title"
android:summary="Summary"
>
<intent
android:action="android.appwidget.action.APPWIDGET_CONFIGURE"
android:targetPackage="my.package.lib"
android:targetClass="my.package.lib.ConfigureClass"
/>
</PreferenceScreen>
Another idea could be creating a custom "preference" that launches another configuration activity, could this work? Would be correct/acceptable to have multiple configuration activities?
The following code on the main ConfigureActivity works however i don't know if its a clean way to do what i want. Could someone confirm?
PreferenceScreen b = (PreferenceScreen) findPreference("my_empty_preference_screen");
b.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(ConfigureActivity.this, ConfigureActivity.class);
intent.setAction("android.appwidget.action.APPWIDGET_CONFIGURE");
ConfigureActivity.this.startActivity(intent);
return false;
}
});
I'm currently writing a live wallpaper for Android and it has a PreferenceScreen which currently contains only one preference - a DialogPreference to set various properties of animation.
User workflow to configure it currently looks like this:
Settings... => (shows the preferences list with only one title ) Animation speed => MyDialogPreference
What I want is to make the workflow like this:
Settings... => MyDialogPreference
I.e. I'm looking for a way to skip showing that preferences list with only one item and to show that dialog right away.
But it seems that PreferenceActivity requests itself to have PreferenceScreen as a root element of preference hierarchy. So... is it even possible to do what i want? :)
Code references:
Activity code:
public class ForestLakePreferences extends PreferenceActivity
{
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
getPreferenceManager().setSharedPreferencesName(
ForestLakeWallpaper.PREFS_NAME);
addPreferencesFromResource(R.xml.preferences);
}
}
Prefs resource:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="lake_preferences">
<DurationEditDialog
android:title="#string/prefs_duration_title"
android:dialogTitle="#string/configure_durations_dlg_title"
android:dialogLayout="#xml/set_durations_layout" />
</PreferenceScreen>
It turned out this can't be done in that way.
But i've found a workaround: i made my activity not a PreferencesActivity, but a custom one and made it look like dialog by placing the following in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Dialog" .... />
A DialogPreference has a showDialog(Bundle state) method, try calling it. I am not sure if you will have to give it anything else like the Preferences or anything.