B4A Library Material AnimatedSwitch

Discussion in 'Additional libraries, classes and official updates' started by DonManfred, Jun 18, 2015.

  1. DonManfred

    DonManfred Expert Licensed User


    This is a wrapper for this Github project.
    • AnimatedSwitch
      • ba As BA
      • AddToParent (Parent As ViewGroup, left As Int, top As Int, width As Int, height As Int)
      • 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)
      • check
      • toggle
      • unCheck
      • Background As Drawable
      • BallColorPress As Int
      • BallColorRelease As Int
      • BallShadowColor As Int
      • BaseColorPress As Int
      • BaseColorRelease As Int
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • Left As Int
      • Margin As Int
      • PressIcon As Bitmap
      • PressIconRes As String [write only]
      • ReleaseIcon As Bitmap
      • ReleaseIconRes As String [write only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int

        aSwitch1.BallColorPress = 
        aSwitch1.BallColorRelease = 
        aSwitch1.BaseColorPress = 
        aSwitch1.BaseColorRelease = 

        aswitch2.BallColorPress = 
        aswitch2.BallColorRelease = 
        aswitch2.BaseColorPress = 
        aswitch2.BaseColorRelease = 
        aswitch2.PressIconRes = 
        aswitch2.ReleaseIconRes = 

        aSwitch3.BallColorPress = 
        aSwitch3.BallColorRelease = 
        aSwitch3.BaseColorPress = 
        aSwitch3.BaseColorRelease = 
        aSwitch3.PressIconRes = 
        aSwitch3.ReleaseIconRes = 
    sw1.jpg sw1press.jpg
    sw2.jpg sw2press.jpg
    sw3.jpg sw3press.jpg

    This library is Donationware. You can download the library, you can test the library. But if you want to USE the library in your App you need to Donate for it.
    Please click here to donate (You can donate any amount you want to donate for the library (or my work) :)

    Attached Files:

    Last edited: Jun 18, 2015
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    Hmm, it appears that you are missing the click event. Apart from that it works great @DonManfred, cheers...
  3. DonManfred

    DonManfred Expert Licensed User

    I have made a onClickListener... hmmm... [.. looking at it ..] OH! Stupid me

    will not expose any events to b4a. :D
    LucaMs likes this.
  4. DonManfred

    DonManfred Expert Licensed User

    Lib updated to V1.0.1 see Post #1
  5. johndb

    johndb Active Member Licensed User

    It appears that you can't change the size of the switches. Height and Width properties have no effect.
  6. DonManfred

    DonManfred Expert Licensed User

    Correct, you cant.....
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    Thank you...
  8. Jerez

    Jerez Active Member Licensed User

    Thanks! i will donate asap... but...any way to have same lib on iOS? i want to share same UI in both platforms.
  9. DonManfred

    DonManfred Expert Licensed User

    I dont own b4i and i also dont know the language to write a library with...
    Sorry i cant help with a b4i library
  10. ArminKH

    ArminKH Well-Known Member

    Why we cant change the size of switches?thats so small
    Thank u for sharing
  11. DonManfred

    DonManfred Expert Licensed User

    I just did the wrapper...

    Seems that the lib is using a fixed value.
    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = Utils.dpToPx(
    45, getResources());
        int height = Utils.dpToPx(
    28, getResources());
        setMeasuredDimension(width, height);
        basePainter.onSizeChanged(height, width);
        ballShadowPainter.onSizeChanged(height, width);
        ballPainter.onSizeChanged(height, width);
        iconPressPainter.onSizeChanged(height, width);
        iconReleasePainter.onSizeChanged(height, width);
    Will have a look at it to make this editable
    johndb and ArminKH like this.
  12. JTKEK

    JTKEK Member Licensed User

    can you add event like SlideSwitch:
    • close (swview as Object As , token as Object As )
    • open (swview as Object As , token as Object As )
    also a Properties:

    State As Boolean
  13. walterf25

    walterf25 Well-Known Member Licensed User

    Hey Don, great library, unfortunately the click_event is not working, I tried adding the views programatically to the activity and making the _click event according to the eventname but it doesn't get raised, i get this error when the event is declared
    and this other one when the event is not declared
    it seems like you are missing something.

    I opened up the jar file and i think i see the problem
    if you look at the code here
    public void _initialize(final BA paramBA, Object paramObject, String paramString) {
        this.eventName = paramString.toLowerCase(BA.cul);
        this.ba = paramBA;
        setObject(new MaterialAnimatedSwitch(paramBA.context));
    public void onClick(View paramAnonymousView)
    if (paramBA.subExists(MaterialAnimatedSwitchWrapper.this.eventName + "_onclick")) {
    "lib:Raising.. " + MaterialAnimatedSwitchWrapper.this.eventName + "_onclick()");
              paramBA.raiseEvent(paramBA.context, MaterialAnimatedSwitchWrapper.this.eventName + 
    "_onclick", new Object[] { paramAnonymousView });
            else {
              BA.Log("lib: NOTFOUND '" + MaterialAnimatedSwitchWrapper.this.eventName + "_onclick");
    You'll notice that the signature for the _onclick event requires the view parameter, so the _onclick event on B4A should look something like this:

    Dim switch1 as AnimatedSwitch


    Sub switch1_onclick(view1 as View)

    End Sub
    i will try it like this and get back to you!

    Last edited: Aug 12, 2015
    DonManfred likes this.
  14. walterf25

    walterf25 Well-Known Member Licensed User

    Nope, I see in the logs that the _onclick event is being raised but it still gives me an error message on the screen and in the logs

    Any ideas!

    DonManfred likes this.
  15. DonManfred

    DonManfred Expert Licensed User

    yes :-/
    it will not work as what i forgot is to set the right signature for this event

    See updated lib at post #1

    Sub AnimatedSwitch_onclick(v As Object)
    If v Is AnimatedSwitch Then
    Dim asx As AnimatedSwitch = v
    ' Do work here
        End If   
    End Sub
  16. DonManfred

    DonManfred Expert Licensed User

    See v1.0.2 in post #1
    i overread this somehow, oups ;)
    will have a look at this
  17. walterf25

    walterf25 Well-Known Member Licensed User

    Awesome, works good now, have you looked into why the views can not be resized? i can't seem to set the height or width to any other size. Will you look into this when you have time?

  18. DonManfred

    DonManfred Expert Licensed User

    Yes i did but i did not found a way to do this as yet. Sorry, i missed to answer here ;)
  19. Kwame Twum

    Kwame Twum Active Member Licensed User

    How do you automate a switch (check/uncheck/toggle) ?
    I tried using:

    Dim swt as AnimatedSwitch
    swt.BallColorPress = 
    swt.BallColorRelease = 
    swt.BaseColorPress = 
    swt.BaseColorRelease = 
    This is the error I got:
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.animation.ValueAnimator.start()' on a null object reference

    Please point me in the right direction. Great lib btw @DonManfred
  20. DonManfred

    DonManfred Expert Licensed User

    try to move the check to activity_resume or started by an timer
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice