How to set negative values for Android Seekbar?

Asked

Viewed 394 times

3

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

How would it be possible to get Seekbar to accept negative values?

1 answer

3


As far as I know, a negative value cannot be established. What I usually do is increase the max to support the interval 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 save the value:

int realValue = seekbar.getValue() - 100;

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

If you want to make everything more elegant, you can also extend the Seekbar class and create your own component that makes this calculation internally, leaving everything transparent to 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);
    }
    ...

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.