Define step value increment for custom RangeSeekBar

suggest change

Versions

This page customizes a third-party RangeSeekBar. It is not an Android framework class.

Introduction

A customization of the Android RangeSeekBar proposed by Alex Florescu at https://github.com/anothem/android-range-seek-bar

The step value is the increment used while the seek bar moves.

Remarks

1- Add the increment attribute in attrs.xml

<resources>
    <declare-styleable name="RangeSeekBar">
        <attr name="increment" format="integer|float"/>
    </declare-styleable>
</resources>

2- Define a default value in RangeSeekBar.java and create the attribute also

private static final int DEFAULT_INCREMENT = 1;
private int increment;

3- Init the increment value in private void init(Context context, AttributeSet attrs)

if (attrs == null) {
    increment = DEFAULT_INCREMENT;
} else {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RangeSeekBar);
    increment = a.getInt(R.styleable.RangeSeekBar_increment, DEFAULT_INCREMENT);
    a.recycle();
}

4- Define the increment value in protected synchronized void onDraw(@NonNull Canvas canvas)

You’ll have to replace the minText and maxText value. So instead of :

You’ll have :

int x;

x = (int) ((getSelectedMinValue().intValue()+increment)/increment);
x = x*increment;
if (x<absoluteMaxValue.intValue()) 
    minText = ""+x;
else
    minText=""+(absoluteMaxValue.intValue()-increment);


x = (int) ((getSelectedMaxValue().intValue()+increment)/increment);
x = x*increment;
maxText = ""+x;

5 - Now you just have to use it. Hope it helps

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents