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:

woniol

Active Member
Licensed User
Longtime User
Thanks
I was waiting for it for long time.

I added two more subs for setting and getting Enabled property for this class:
B4X:
Public Sub setEnabled(b As Boolean)
    joSwitch.RunMethod("setEnabled", Array As Object(b))
End Sub

Public Sub getEnabled As Boolean
    Return joSwitch.RunMethod("isEnabled", Null)
End Sub

and this:
B4X:
Switch.Enabled = base.Enabled
to the DesignerCreateView Sub
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
It's better than the old strategy. I like it.
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi all,
How do I design alike iOS's ? (Angle of it have radius)
 

Theera

Well-Known Member
Licensed User
Longtime User
I mean that switch in picture post#1 is look strong,I would like to can design look like iOS's ,it look like pretty.(Can I use nine-patch with the custom view? or other strategies)
 

Andris

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.

Erel, in this code,

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    sw.Initialize(Me, "sw")
    Activity.AddView(sw.Switch, 10dip, 10dip, 200dip, 50dip)
    sw.TextOff = "aaa"
    sw.TextOn = "bbb"
End Sub

these 2 lines

B4X:
    sw.TextOff = "aaa"
    sw.TextOn = "bbb"

don't seem to do anything. I assume they are only to set text other than OFF and ON, but they don't work for me. Is this because the necessary sub in the class (setTextOn) is not being referenced correctly?
 

Andris

Active Member
Licensed User
Longtime User
It should change the text (see the screenshot above). I've just tested it here on Android 4.4.2 and it works. Which version are you using?

My Galaxy S3 is running Android 4.3.

I just tried changing the target SDK version in Manifest Editor to "13." It does then work as it should. But with "14" it does not.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
If I understood well, this library is suitable from Android ICS to KitKat.
So we have to catch the OS version and if it is before Android 4 we have to use other views intead of this, right ?
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I tried it and the switch buttons show up but there is no text when I tap them.

I used this in the manifest editor:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

Did I miss something else?
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

No, the ON and OFF text is not showing up, only the switch view. I can tap them and they will toggle on and off but no text is showing up.
 
Top