How to set negative values for SeekBar from Android?

3

I'm using Xamarin to develop for Android in C # and checking the component documentation SeekBar vi that it only has a "Max" property, and that the minimum value for the component is 0.

How would it be possible to make SeekBar accept negative values?

    
asked by anonymous 12.03.2014 / 02:22

1 answer

3

As far as I know, it is not possible to establish a negative value. What I usually do is increase max to support the range and subtract from the current value to show my range of values to the user.

Let's say I have a SeekBar of -100 to 100:

seekbar.setMax(200);

and subtract / translate when showing the user and / or saving the value:

int realValue = seekbar.getValue() - 100;

A similar maneuver can be made for decimal values, for example.

If you want to make everything look more elegant, you can also extend the SeekBar class and create your own component that does this calculation internally, making everything transparent for your activity:

public class NegativeSeekBar extends SeekBar {

    protected int minValue = 0;
    protected int maxValue = 0;
    ...

    public void setMin(int min){
        this.minValue = min;
        super.setMax(maxValue - minValue);
    }

    public void setMax(int max){
        this.maximumValue = max;
        super.setMax(maxValue - minValue);
    }
    ...

}
    
12.03.2014 / 03:43