-1

I'm trying to use the new Material Slider but I don't know if there is a way that I can get the value of the slider programmatically as a Integer.

I need the value as Integer because I have to work with the value doing some arithmetic calculations.

I get the value in float. How can I convert it to a Integer?

slider.addOnChangeListener(new Slider.OnChangeListener() {
    @Override
    public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
        //Use the value
    }
});
Khaby Lame
  • 228
  • 2
  • 16
Giovanni
  • 111
  • 2
  • 16

1 Answers1

3

You have got the value of the slider. You have to convert it to a Integer. For converting float into integer, follow below code:

slider.addOnChangeListener(new Slider.OnChangeListener() {
    @Override
    public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
        Integer slide_value = Math.round(value); 
    }
});

NOTE: Using Math.round() will round the float to the nearest integer.

Khaby Lame
  • 228
  • 2
  • 16