B4A Library SwitchButton

This is a wrap for this Github-project.
You need B4A V5.8+ to use this library.
You need B4A 6+ to use V1.5+ of this library.

New in V1.5
- The library is now available as aar file.
- I´ve added TextColor property


SwitchButton
Author:
DonManfred (wrapper)
Version: 2.1
  • SwitchButton
    Events:
    • onCheckedChanged (checked As Boolean)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • isDrawDebugRect As Boolean
    • isFadeBack As Boolean
    • performClick As Boolean
    • setText (onText As CharSequence, offText As CharSequence)
    • setThumbMargin (left As Float, top As Float, right As Float, bottom As Float)
    • setThumbSize (width As Float, height As Float)
    • toggleImmediately
    Properties:
    • AnimationDuration As Long
    • BackColor As ColorStateList
    • BackDrawable As Drawable
    • BackDrawableRes As String [write only]
    • BackMeasureRatio As Float
    • BackRadius As Float
    • BackSizeF As PointF [read only]
    • Background As Drawable
    • Checked As Boolean [write only]
    • CheckedImmediately As Boolean [write only]
    • Color As Int [write only]
    • DrawDebugRect As Boolean [write only]
    • Enabled As Boolean
    • FadeBack As Boolean [write only]
    • Height As Int
    • Left As Int
    • Parent As Object [read only]
    • Process As Float
    • Tag As Object
    • ThumbColor As ColorStateList
    • ThumbDrawable As Drawable
    • ThumbDrawableRes As String [write only]
    • ThumbHeight As Float [read only]
    • ThumbMargin As RectF [read only]
    • ThumbMarginRect As RectF [write only]
    • ThumbRadius As Float
    • ThumbSize2 As PointF [write only]
    • ThumbSizeF As PointF [read only]
    • ThumbWidth As Float [read only]
    • TintColor As Int
    • Top As Int
    • Visible As Boolean
    • Width As Int

B4X:
    SwitchButton1.ThumbDrawableRes = "flyme_thumb_drawable"
    SwitchButton1.BackDrawableRes = "flyme_back_drawable"
    SwitchButton2.BackDrawableRes = "ios_back_drawable"

    SwitchButton3.ThumbDrawableRes = "miui_thumb_drawable"
    SwitchButton3.BackDrawableRes = "miui_back_drawable"

    SwitchButton4.ThumbDrawableRes = "icon_github"
    SwitchButton5.ThumbDrawableRes = "ios_back_drawable"

Screenshot_20160402-132307.png
 

Attachments

  • SwitchButtonV1.0.0.zip
    19.4 KB · Views: 501
  • SwitchButtonV1.0.1.zip
    19.2 KB · Views: 521
  • SwitchButtonV1.5.zip
    33 KB · Views: 563
  • SwitchButtonV2.1.zip
    32.7 KB · Views: 661
  • SwitchButtonEx.zip
    26.4 KB · Views: 676
Last edited:

klarsys

Active Member
Licensed User
Longtime User
B4X:
Sub SB_onCheckedChanged(checked As Boolean)
    Dim sb As SwitchButton = Sender
    Log(sb.Tag)
End Sub

I get this error:
java.lang.ClassCastException: b4a.example.main cannot be cast to com.github.switchbutton.SwitchButton
 

Daniel-White

Active Member
Licensed User
Longtime User
Hi folks, I was playing with this lib, I had been trying to put the TEXT "ON" and "OFF" states. but without any luck.

I did this added to the example swtichButton1.setTEXT("ON","OFF")
I did not see in the layout the text in the switch button what I am missing there?

B4X:
witchButton1.setText("ON","OFF")
    SwitchButton1.ThumbDrawableRes = "flyme_thumb_drawable"
    SwitchButton1.BackDrawableRes = "flyme_back_drawable"
    SwitchButton2.BackDrawableRes = "ios_back_drawable"

Thanks youuuuuuuuuuu
 

Daniel-White

Active Member
Licensed User
Longtime User
Dont use a drawable when you want to get the text shown.

B4X:
SwitchButton6.setText("ON","OFF")
View attachment 45598
Thanks you soooooooooo much. so simple LoL :D

An exmple for the rest of mortals like me :eek:
B4X:
    SwitchButton1.setText("ON","OFF")
'    SwitchButton1.ThumbDrawableRes = "flyme_thumb_drawable"
'    SwitchButton1.BackDrawableRes = "flyme_back_drawable"
'    SwitchButton2.BackDrawableRes = "ios_back_drawable"
 

Daniel-White

Active Member
Licensed User
Longtime User
I did not discover how to change the color to SwitchButton1.setText("ON","OFF").
Can I put an example Black color the text ?
Thanks youuuuuuuuuuuu
 

Daniel-White

Active Member
Licensed User
Longtime User
But i´ve added them :D

See update in Post #1

OOO thanks you sooo much DonManfred. well, you push me run to upgrade to B4A Ver 6.0 and download the repositories etc. :eek:
It is working nice the TextColor property :).
Thanks you Indeed. :D
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Great Don. I have this error when i compile:

B4A version: 6.31
Parsing code. (0.00s)
Compiling code. (0.01s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.78s)
Generating R file. Error
..\resource\values\attrs.xml:5: error: Attribute "kswThumbDrawable" has already been defined
..\resource\values\attrs.xml:6: error: Attribute "kswThumbColor" has already been defined
..\resource\values\attrs.xml:7: error: Attribute "kswThumbMargin" has already been defined
..\resource\values\attrs.xml:8: error: Attribute "kswThumbMarginTop" has already been defined
..\resource\values\attrs.xml:9: error: Attribute "kswThumbMarginBottom" has already been defined
..\resource\values\attrs.xml:10: error: Attribute "kswThumbMarginLeft" has already been defined
..\resource\values\attrs.xml:11: error: Attribute "kswThumbMarginRight" has already been defined
..\resource\values\attrs.xml:12: error: Attribute "kswThumbWidth" has already been defined
..\resource\values\attrs.xml:13: error: Attribute "kswThumbHeight" has already been defined
..\resource\values\attrs.xml:14: error: Attribute "kswThumbRadius" has already been defined
..\resource\values\attrs.xml:15: error: Attribute "kswBackRadius" has already been defined
..\resource\values\attrs.xml:16: error: Attribute "kswBackDrawable" has already been defined
..\resource\values\attrs.xml:17: error: Attribute "kswBackColor" has already been defined
..\resource\values\attrs.xml:18: error: Attribute "kswFadeBack" has already been defined
..\resource\values\attrs.xml:19: error: Attribute "kswBackMeasureRatio" has already been defined
..\resource\values\attrs.xml:20: error: Attribute "kswAnimationDuration" has already been defined
..\resource\values\attrs.xml:21: error: Attribute "kswTintColor" has already been defined
..\resource\values\attrs.xml:22: error: Attribute "kswTextOn" has already been defined
..\resource\values\attrs.xml:23: error: Attribute "kswTextOff" has already been defined
..\resource\values\attrs.xml:24: error: Attribute "kswTextMarginH" has already been defined

Any suggestion ?
Thanks
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
Try to remove the #additionalres directive
Already try....

main_activity_create (java line: 369)
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2480)
at android.content.res.Resources.getDrawable(Resources.java:1975)
at android.content.Context.getDrawable(Context.java:409)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:344)
at com.kyleduo.switchbutton.SwitchButton.setThumbDrawableRes(SwitchButton.java:702)
at de.donmanfred.SwitchButtonWrapper.setThumbDrawableRes(SwitchButtonWrapper.java:148)
at b4a.example.SwitchButton.main._activity_create(main.java:369)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.SwitchButton.main.afterFirstLayout(main.java:102)
at b4a.example.SwitchButton.main.access$000(main.java:17)
at b4a.example.SwitchButton.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
 

MarcoRome

Expert
Licensed User
Longtime User
Upload a small example project which shows the problem. I´ll try to reproduce it...
I tried your example in #1

B4X:
#Region  Project Attributes
    #ApplicationLabel: SwitchBTN-Ex
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region


'#AdditionalRes: ..\Resource

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private SwitchButton1 As SwitchButton
    Private SwitchButton2 As SwitchButton
    Private SwitchButton3 As SwitchButton
    Private SwitchButton4 As SwitchButton
    Private SwitchButton5 As SwitchButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

  Dim States(3,1) As Int
  States(0,0) = 16842919    'Pressed
  States(1,0) = 16842910    'Enabled
  States(2,0) = -16842910 'Disabled

  Dim Color(3) As Int = Array As Int(Colors.Blue,Colors.Cyan,Colors.LightGray)

  Dim CSL As JavaObject
  CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,Color))
    SwitchButton1.ThumbColor = CSL

    SwitchButton1.ThumbDrawableRes = "flyme_thumb_drawable"
    SwitchButton1.BackDrawableRes = "flyme_back_drawable"
    SwitchButton2.BackDrawableRes = "ios_back_drawable"
   
    SwitchButton3.ThumbDrawableRes = "miui_thumb_drawable"
    SwitchButton3.BackDrawableRes = "miui_back_drawable"

    SwitchButton4.ThumbDrawableRes = "icon_github"
    SwitchButton5.ThumbDrawableRes = "ios_back_drawable"

    'SwitchButton1.Enabled = False

End Sub

Sub Switch_onCheckedChanged(checked As Boolean)
   
End Sub


Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
 
Top