Reputation: 569
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?
Upvotes: 2
Views: 844
Reputation: 1441
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)
}
}
Upvotes: 0
Reputation: 574
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.
Upvotes: 0