How can I do a slide switch with 2 positions

204 views
Skip to first unread message

Alvaro Torres

unread,
Jan 10, 2016, 1:35:21 PM1/10/16
to MIT App Inventor Forum

Hi everybody, I'm trying to make a slider two positions, only two positions, I do not want it to be like a volume slider or for adjusting the intensity of a screen for example, I want the slider only have two states and I would like to be look like the picture attached, anyone who can help me ?
BOTON OFF.png
BOTON ON.png

Abraham Getzler

unread,
Jan 10, 2016, 5:35:05 PM1/10/16
to MIT App Inventor Forum
You can use a button with no text, transparent background,
and the choice of your two images as a background.
Each button push, check the background image and replace it
with the other one.

If you want swipe action, use a small canvas instead with transparent background,
and the choice of your two images as a background.
When you get a swipe, use the x direction to decide which
background image to apply.

ABG

Alvaro Torres

unread,
Jan 10, 2016, 6:34:49 PM1/10/16
to MIT App Inventor Forum
Abraham thanks for your answer, but I do not understand the procedure and the blocks I have to use for this. You could send a screen explaining the blocks?

Abraham Getzler

unread,
Jan 10, 2016, 11:15:26 PM1/10/16
to MIT App Inventor Forum
See attached.
ABG
2016-01-10 23_10_34-5554__build_.png
2016-01-10 23_11_24-5554__build_.png
blocks.png
BOTON OFF.png
BOTON ON.png
OnOffSlider.aia

Scott Ferguson

unread,
Jan 11, 2016, 2:04:09 PM1/11/16
to MIT App Inventor Forum
Nice, simple solution, Abraham :-)
---
sf

Abraham Getzler

unread,
Jan 11, 2016, 3:54:25 PM1/11/16
to MIT App Inventor Forum
Thanks.

The solution might be too simple, if the OP wants to emit a sound
when transitioning states.  In such a case, an additional test should
be added to do nothing if the desired target state is already in effect,
to avoid machine-gunning the transition sound during the drag.

ABG

Scott Ferguson

unread,
Jan 11, 2016, 5:53:38 PM1/11/16
to MIT App Inventor Forum
Yes. I have run into this issue before.
I have done a test using a flag variable as you suggest, then change it's logic value to stop processing the blocks in the dragged event block.
Another solution is the first time the dragged event fires, disable the component.
That prevents it from firing multiple times.
Of course if you will need to use the switch again during that session you will eventually need to enable the component again somewhere.
---
sf
Reply all
Reply to author
Forward
0 new messages