Android sliders with fixed numbers

4.1k Views Asked by At

I'm trying to implement a discrete slider with fixed values, but the only thing I can set is the valueFrom, valueTo and stepSize.

Here is my code how I'm trying to do

<com.google.android.material.slider.Slider
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="8dp"
        app:tickColor="@color/colorSecondaryLight"
        app:tickColorActive="@color/colorSecondary" />

Is there a way to set fixed values on the slider? (Values that I will use 2, 5, 10, 25, 50 and 100)

2

There are 2 best solutions below

0
On BEST ANSWER

The solution that worked for me was this lib called BubbleSeekBar

Step 1 - Add the dependencies on your Gradle.

implementation "com.xw.repo:bubbleseekbar:3.20-lite"

Step 2 - Create your BubbleSeekBar on your XML and add the attributes that you need. For my case, the example below worked.

<com.xw.repo.BubbleSeekBar
        android:id="@+id/bubbleSeekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:bsb_auto_adjust_section_mark="true"
        app:bsb_hide_bubble="true"
        app:bsb_min="2"
        app:bsb_section_count="5"
        app:bsb_section_text_position="below_section_mark"
        app:bsb_seek_by_section="true" />

Step 3 - Since I needed custom options, I initialized on my onCreate with the array of values declared on my string.xml

bubbleSeekBar.setCustomSectionTextArray { sectionCount, array ->
            array.clear()
            for ((index, value) in resources.getStringArray(R.array.xxxx)
                .withIndex()) {
                array.put(index, value)
            }
            array
        }

Step 4 - You can capture the changes or set a value with the methods below.

bubbleSeekBar.onProgressChangedListener
bubbleSeekBar.setProgress()

The lib is pretty good and worked for me. For more info take a look at the link at the top of this answer.

1
On

You'd be better off using SeekBar

<SeekBar
     android:id="@+id/sb"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="10"
     android:thumb="@drawable/ic_location"
     android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />