Search code examples
androidandroidxandroid-preferencesandroid-seekbarandroid-settings

Problems while implementing AndroidX SeekBarPreference


I'm trying to add a SeekBarPreference to my Android Settings PreferenceScreen. Everything works when I use an EditTextPreference for example, so it's definitely the SeekBar which is causing problems.

First problem: I can specify a min value with app:min="", but app:max="" doesn't seem to exist.. I guess it's a bug in the API, because I found this statement in the docs:

Other SeekBar specific attributes (e.g. title, summary, defaultValue, min, max) can be set directly on the preference widget layout.

Second problem: When I use the code below, my app crashes with the following message when opening the settings: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

My code:

public class PreferenceFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.settings, rootKey);
    }
}

and

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        app:key="category_playback"
        app:title="Playback">

        <SeekBarPreference
            app:defaultValue="15"
            app:key="skip_time"
            app:min="5"
            app:summary="Seconds to skip"
            app:title="Skip time" />

    </PreferenceCategory>

</PreferenceScreen>

Anyone got an idea on how to fix it? If there's no solution I will try to implement it programmatically.

Thanks.


Solution

  • For your first problem use:

    android:max="10"
    

    For your second problem, try clearing your app data.