Search code examples
androidxmlmaxpreferencescreenseekbarpreference

Changing max value in SeekBarPreference


I can't change max value in standard SeekBarPreference.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/settings">
        <SeekBarPreference
            android:key="seekBarVolume"
            android:max="10"
            android:title="@string/seekbar_volume" />
    </PreferenceCategory>

</PreferenceScreen>

I would like seekbar has a value of:

int maxVolume = AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

I'm trying to change the value in the PreferenceActivity, but I have no idea. I stuck at:

Preference seekBar = findPreference("seekBarVolume");

Is there a way to change max value without using custom seekBarPreference?


Solution

  • The SeekBarPreference class is hidden for whatever reason. Source code shows that there is a SeekBarPreference.setMax(int) method available so it could be possible to use reflection to reach that, but it seems overkill for the task at hand.

    I would suggest you to instead use a preset number of steps (such as 10, as your layout suggests) and then calculate the requested volume value using maxVolume * seekBarValue / seekBarStepCount as that seems like a safer solution.