B4A Class Switch view (Android 4+)

upload_2014-3-18_9-44-0.png


This class wraps the native Switch view using JavaObject. This view was added in Android 4+. You can either add the view with the designer as a CustomView (don't forget to set Custom Type field) or add it by code.

See the attached example.

Edit: Switch is available in the ViewsEx library.
 
Last edited:

Croïd

Active Member
Licensed User
Longtime User
Hi Erel,

Is it possible reverse color for switch ? (for harmonize with maps location)

switch.png
 

Dave O

Well-Known Member
Licensed User
Longtime User
I'm using this switch to replace a checkbox in an Android 5+ app.

I'm more familiar with using libraries than classes in B4A, so I downloaded the zip file and compiled it to a library as is. Then I added the library to my app.

The switch shows up fine and works, but no text appears, even though I set the Text property in the designer.

The switch responds to taps in the area left of the switch control itself, so it looks like that's where the text should appear.

So, I tried setting Text in code, but there is no Text property, just TextOn and TextOff (which apparently have been deprecated in Android 5+).

Is there a way to get the text to show up beside the switch? I'd prefer to use the switch over a checkbox, per the Material Design guidelines.

Thanks!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Switch doesn't show any text. You can use a label together with the Switch.

OK thanks, that's good to know that it's different from a checkbox when it comes to the text.

It looks like the switch itself renders at the far right of the view's bounds, so I've set the switch's width to overlap a separate label. With the switch on top (z order), that means that the user can tap the switch or the label and I can handle this with just the switch's event listener.

As a bonus, the switch picks up the custom theme colors I'm using in the app.

Thanks Erel!
 

BarryW

Active Member
Licensed User
Longtime User
View attachment 23580

This class wraps the native Switch view using JavaObject. This view was added in Android 4+. You can either add the view with the designer as a CustomView (don't forget to set Custom Type field) or add it by code.

See the attached example.

Edit: Switch is available in the ViewsEx library.

Where is the sample?
 
Top