2

I'm writing my first Android app and it involves a preference where you set an amount of minutes, from 0 to 60. I've used a SeekBarPreference, and it shows up as a simple slider which you can indeed slide around to edit the value. The preference works fine.

I would like to show the selected value next to the SeekBar (or somewhere in the vicinity), as there by default is no way for the user to see what they've actually selected. There are lots of questions on Stack Overflow about similar sliders and preferences, but since version 25.1.0, released last December, there is SeekBarPreference, which has just what I need:

The seekbar value view can be shown or disabled by setting showSeekBarValue attribute to true or false, respectively.

But among the public methods listed below, there is no method for setting showSeekBarValue? There is a method for setting the adjustable attribute, setAdjustable(boolean adjustable), however?

Ideally I'd just write android:showSeekBarValue="true" in my SeekBarPreference tag in preferences.xml, but that (obviously) doesn't work.

So in a nutshell: how do I set showSeekBarValue to true?

2 Answers2

0

In the app's build.gradle, I had to add

implementation 'com.android.support:preference-v7:26.+

Then, modify the PreferenceScreen's xml to include an app namespace:

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

Finally, inside the xml, you can add

<SeekBarPreference
    android:key="..."
    android:title="..."
    android:max="100"
    android:min="0"
    android:defaultValue="30"
    android:dependency="..."
    app:showSeekBarValue="true"
    />

This compiles without errors. To be fair, this is not a complete answer, because no value is shown in my case, even after these steps. But formally it does let you set the value in some way.

wessel
  • 534
  • 5
  • 15
  • Yes, I did use a SeekBarPreference already - it works fine, except that no value is shown. Sorry, but this answer is in no way helpful to me. I appreciate your effort though! – Jobjörn Folkesson Jan 22 '18 at 11:36
  • I figured that. As it was too long for a comment, I decided to post it as an answer nevertheless, hoping that someone can amend it to finally get a functioning SeekBarPreference for which you do not have call setTitle() on every change, to reflect the value... – wessel Jan 22 '18 at 15:27
0

You need to use PreferenceScreen from android.support.v7.preference

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <SeekBarPreference
        android:key="size"
        android:title="Size"
        android:summary="size of progressBar in dp's"
        android:max="100"
        app:showSeekBarValue="true"
        android:defaultValue="25" />
</android.support.v7.preference.PreferenceScreen>

and also PreferenceFragmentCompat from android.support.v7.preference

import android.support.v7.preference.PreferenceFragmentCompat

class SettingsFragment: PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences)
    }
}
saiedmomen
  • 1,401
  • 16
  • 33