For the application I am working on, I need to have a preference screen, which has a EditTextPreference, SwitchPreference and a VolumePreference. I am using the VolumePreference as I need a preference that is set with a slider, and VolumePreference was the only one I could find that fit the bill. Here is my Preference XML:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:capitalize="words"
android:defaultValue="@string/pref_default_channel"
android:inputType="number"
android:key="pref_channel"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_channel"
android:summary="@string/pref_summary_channel"/>
<SwitchPreference
android:key="pref_customVol"
android:title="@string/pref_title_custom_vol"
android:summary="@string/pref_summary_custom_vol"
android:defaultValue="false" />
<VolumePreference
android:name="Volume Preference"
android:title="Notification Volume"
android:summary="Set your notification volume, so you will be notified when your Seahorse is almost finished running"
android:key="pref_notifVolPref"
android:dependency="pref_customVol"/>
I am able to get the value set by the SwitchPreference easily from shared preferences, using the code:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean customVol = sharedPref.getBoolean("pref_customVol", false);
However, when I try to do the same to get the value from my VolumePreference, it always returns the default value of -1. Here is the code I use to retrieve the volume preference:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int vol = sharedPref.getBoolean("pref_notifVolPref", -1);
Am I using the VolumePreference incorrectly? Do I need to create my own custom preference to display a slider preference? Has anyone had this issue before? Thanks
first extends PreferenceActivity
second add this to onCreate()
addPreferencesFromResource(R.xml.account_preferences);
and add this to layout
Or in main activity use this.