Skip to first unread message
Assigned to kkas...@gmail.com by taifu...@gmail.com

Peppe Cavallo

unread,
Aug 31, 2017, 6:00:22 AM8/31/17
to mitappinv...@googlegroups.com
Hi guys,

I looked around for this issue, but I couldn't find any solution.

I'm trying to set the value of a slider thumb position by invoking the "set slider.thumbPosition" method with a button click and using a value read from a textbox. If the value to set is far from the current thumb position, the slider doesn't get the exact value, but a closer value. If now I repeat the same assignment, it takes the correct value. 

As example, the slider (min:0-max:1023) has a thumb position of 20. I try to set a new value of 700. The slider gets a value of 696. Now I type again "700" in the textbox, click the button, and the thumb position finally goes to 700.

How do you explain it? How can I solve?

Thanks in advance.

Taifun

unread,
Sep 1, 2017, 10:42:35 AM9/1/17
to MIT App Inventor Forum
as far as I know, the slider can only have 100 different positions, see also https://groups.google.com/d/msg/mitappinventortest/TBtxXLcHSZI/RUCIHzZjp0IJ
Taifun

Peppe Cavallo

unread,
Sep 1, 2017, 12:56:32 PM9/1/17
to MIT App Inventor Forum
Hi Taifun, thanks for your reply. But still I don't understand why it takes the right value after the second assignment.

Evan Patton

unread,
Sep 1, 2017, 1:11:21 PM9/1/17
to MIT App Inventor Forum
@Peppe: I haven't confirmed this by running it, but this is my expectation from examining the sources:

1) Slider wraps the Android widget SeekBar, which is a subclass of ProgressBar.
2) Slider is an event listener for SeekBar to report when the thumb position changes.
3) You set the ThumbPosition.
3) Slider internally stores the new ThumbPosition as an optimization.
4) Slider transforms the thumb position based on the min/max range into a [0,100] range for SeekBar.
5) SeekBar's position is set, which is stored as the last position to optimize redrawing (e.g., don't redraw if the value hasn't changed).
6) SeekBar after its visual refresh fires an onPositionChanged event, which triggers the callback on Slider.
7) Slider updates its thumb position value by reversing the transformation in step 4.
8) You read the value back, which is 696.
9) You set the ThumbPosition to 700 again.
10) Slider stores the new thumb position internally as an optimization (Step 3).
10) Slider transforms the new thumb position to the [0,100] range for SeekBar (Step 4).
11) Slider sets the SeekBar position, but since it is the same value in [0,100] it does not update itself because the old value and new value match.
12) Because the onPositionChange handler is not called, the cached thumb position is 700 rather than 696.

Evan

Italo

unread,
Sep 1, 2017, 1:28:44 PM9/1/17
to MIT App Inventor Forum
Using a small canvas, a ball and two lines, it's relatively easy to make your own slider. Want to give it a try?
Reply all
Reply to author
Forward
0 new messages