Search code examples
androidandroid-support-libraryandroid-preferences

SeekBarPreference showSeekBarValue value not shown properly


I'm trying to use SeekBarPreference from android.support.v7.preference and also I want the current value to be shown. There is an attribute showSeekBarValue that makes this possible. The value is shown but it seems like the TextView that holds the value isn't styled properly and part of the value is unfortunately hidden.

enter image description here

This is my preferences xml:

<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 this is my Preference fragment:

import android.support.v7.preference.PreferenceFragmentCompat

class SettingsFragment: PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences)
    }
}

Is there something I'm doing wrong?


Solution

  • I looked into the library code and in the layout(preference_widget_seekbar.xml) the height set on the textView is match_parent while the height of the parent is wrap_content. Setting the height of textView to wrap_content solves the issue.

    This is onViewCreated of my SettingsFragment:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        listView.viewTreeObserver.addOnDrawListener {
            val sizePref = findPreference(getString(R.string.size))
    
            val prefView: View? = listView.layoutManager.findViewByPosition(sizePref.order)
            prefView?.apply {
                //Here is the fix
                findViewById<TextView>(seekbar_value).apply {
                    layoutParams = LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
                }
                //another visual improvement
                findViewById<AppCompatSeekBar>(seekbar).apply {
                    (layoutParams as LinearLayout.LayoutParams).apply {
                        gravity = Gravity.CENTER_VERTICAL
                    }
                }
    
            }
        }
    }