saiedmomen
saiedmomen

Reputation: 1441

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?

Upvotes: 1

Views: 2070

Answers (2)

saiedmomen
saiedmomen

Reputation: 1441

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
                }
            }

        }
    }
}

Upvotes: 1

praveen2034
praveen2034

Reputation: 119

Hi check the below url for ur requirement.

https://v4all123.blogspot.com/2017/12/simple-example-of-seekbarpreference-in.html

i think the layout height is fixed i guess. if u use wrap_content hope it should work by doing change in height or the url may help you.

Upvotes: 0

Related Questions