Just trying the stepper, and one part confused me!...
# compose-wear
g
Just trying the stepper, and one part confused me! To try it out, tried a hard-coded example, which illustrates:
Copy code
Stepper(
    value = curr.value,
    onValueChange = { value ->
        curr.value = value
        Log.i(TAG, "New value: $value")
    },
    valueRange = 100.0f .. 220.0f,
    steps = (220 - 100) / 5 - 1
) {
Aim was to be able to have a range of 100 to 220 and be able to step values by 5. The part that confused me was the
steps
value. Docs say "Step value is calculated as the difference between min and max values divided by steps+1". So, to get the correct step size, I have to subtract one from what I initially thought as the intuitive number of steps (
(220 - 100) / 5
). It's no big deal, reading the documentation solved it, just wasn't sure why "steps+1", though I've likely just misunderstood something.
y
My (non-authoritative) reading: Steps + 1 because range 0-100, which increment of 1, there are 101 steps including 0.
But then
Steps: Specifies the number of discrete values, evenly distributed between across the whole value range. Must not be negative. If 0, stepper will have only min and max values and no steps in between
which makes it sound like it should be 99 steps, but I guess this is just the special value for no steps.
Which matches your value. So it's the "between" that is key.
Yeah, I think the docs might need some ()
s
For values [0..10] and step=5, you want 3 values: zero, 5 and 10 and that's what (10-0)/5 + 1 gives us. NB it's not 'divided by (steps+1)'. We could move the +1 to the start so that it reads 1 + (max-min)/steps.
👍🏻 1
g
I agree, 3 steps makes sense in that example, I could also have understood 2. But for me, to get that to work, namely, range of 0..10 with values 0, 5 and 10, I have to specify
steps
as 1, which doesn't make much sense conceptually, as in I don't see in what sense this description has 1 steps. But given the docs, I can calculate it as 1: (10 - 0) / 5 -1
Example:
Copy code
Stepper(
    value = curr.value,
    onValueChange = { value ->
        curr.value = value
        Log.i(TAG, "New value: $value")
    },
    valueRange = 0.0f .. 10.0f,
    steps = 1
) {
// Results in stepper that provides 0, 5 and 10
Ah ok, I think it is just me. As you say "between" is the key word here @yschimke. So if I'm thinking of steps more as "intermediate values" or "intermediate steps", namely, excluding both start and end of the range. 👍 (Hopefully I can be forgiven for not immediately taking steps to be exclusive in this way?)