How to get setting value from VolumePreference

595 views Asked by At

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

1

There are 1 answers

0
Ali Bagheri On
  • first extends PreferenceActivity

  • second add this to onCreate()

    addPreferencesFromResource(R.xml.account_preferences);

    Preference checkPreference = getPreferenceScreen().findPreference("checkbox_contacts_sync");
    Preference.OnPreferenceChangeListener lis1 = new Preference.OnPreferenceChangeListener()
    {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o)
        {
            Toast.makeText(SystemManager.getAppContext(), "yess", Toast.LENGTH_LONG).show();
            return true;
        }
    };
    checkPreference.setOnPreferenceChangeListener(lis1);
    

and add this to layout

<ListView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:visibility="visible"
              android:id="@android:id/list">
    </ListView>

Or in main activity use this.

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.OnSharedPreferenceChangeListener shListener = new SharedPreferences.OnSharedPreferenceChangeListener()
        {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key)
            {
                Assistance.print("change");
                Toast.makeText(SystemManager.getAppContext(),"ohh",Toast.LENGTH_LONG).show();
            }
        };
        sharedPreferences.registerOnSharedPreferenceChangeListener(shListener);