B4A Library Material AnimatedSwitch

AnimatedSwitch
Version:
1

This is a wrapper for this Github project.
  • AnimatedSwitch
    Fields:
    • ba As BA
    Methods:
    • 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
    Properties:
    • 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

B4X:
    Activity.LoadLayout("Layout1")
    aSwitch1.BallColorPress = Colors.Red
    aSwitch1.BallColorRelease = Colors.Green
    aSwitch1.BaseColorPress = Colors.Blue
    aSwitch1.BaseColorRelease = Colors.Magenta

    aswitch2.BallColorPress = Colors.Cyan
    aswitch2.BallColorRelease = Colors.DarkGray
    aswitch2.BaseColorPress = Colors.Cyan
    aswitch2.BaseColorRelease = Colors.Blue
    aswitch2.PressIconRes = "ic_delete_deep_purple_500_36dp"
    aswitch2.ReleaseIconRes = "ic_delete_blue_500_36dp"

    aSwitch3.BallColorPress = Colors.Gray
    aSwitch3.BallColorRelease = Colors.DarkGray
    aSwitch3.BaseColorPress = Colors.Gray
    aSwitch3.BaseColorRelease = Colors.DarkGray
    aSwitch3.PressIconRes = "ic_stop_deep_orange_500_36dp"
    aSwitch3.ReleaseIconRes = "ic_play_arrow_white_36dp"

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) :)
 

Attachments

  • AnimatedSwitchEx.zip
    14.8 KB · Views: 552
  • libAnimatedSwitchV1.0.1.zip
    35.2 KB · Views: 452
  • libAnimatedSwitchV1.0.2.zip
    36.2 KB · Views: 519
Last edited:

johndb

Active Member
Licensed User
Longtime User
It appears that you can't change the size of the switches. Height and Width properties have no effect.
 

Jerez

Active Member
Licensed User
Longtime User
AnimatedSwitch
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) :)

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

ArminKH

Well-Known Member
Why we cant change the size of switches?thats so small
Thank u for sharing
 

DonManfred

Expert
Licensed User
Longtime User
Why we cant change the size of switches?
I just did the wrapper...

Seems that the lib is using a fixed value.
B4X:
  @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
 

JTKEK

Member
Licensed User
Longtime 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
 

walterf25

Expert
Licensed User
Longtime 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
java.lang.Exception: Sub switch1_onclick signature does not match expected signature.
and this other one when the event is not declared
lib: NOTFOUND 'switch2_onclick

it seems like you are missing something.

EDIT:
I opened up the jar file and i think i see the problem
if you look at the code here
B4X:
  public void _initialize(final BA paramBA, Object paramObject, String paramString) {
    this.eventName = paramString.toLowerCase(BA.cul);
    this.ba = paramBA;
    setObject(new MaterialAnimatedSwitch(paramBA.context));
    ((MaterialAnimatedSwitch)getObject()).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        if (paramBA.subExists(MaterialAnimatedSwitchWrapper.this.eventName + "_onclick")) {
          BA.Log("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:

B4X:
Dim switch1 as AnimatedSwitch

switch1.Initialize("switch1")

Sub switch1_onclick(view1 as View)

End Sub

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

Thanks,
Walter
 
Last edited:

walterf25

Expert
Licensed User
Longtime 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

java.lang.Exception: Sub switch1_onclick signature does not match expected signature.

Any ideas!

Walter
 

DonManfred

Expert
Licensed User
Longtime User

walterf25

Expert
Licensed User
Longtime 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

B4X:
Sub AnimatedSwitch_onclick(v As Object)
    Log("AnimatedSwitch_onclick()")
    If v Is AnimatedSwitch Then
        Dim asx As AnimatedSwitch = v
        ' Do work here
    End If  
End Sub
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?

Thanks,
walter
 

Kwame Twum

Active Member
Licensed User
Longtime User
How do you automate a switch (check/uncheck/toggle) ?
I tried using:

B4X:
Dim swt as AnimatedSwitch
swt.Initialize("swt")
swt.BallColorPress = Colors.Green
swt.BallColorRelease = Colors.Gray
swt.BaseColorPress = Colors.LightGray
swt.BaseColorRelease = Colors.LightGray
Panel1.AddView(swt,100%x-70dip,20dip,70dip,70dip)
swt.check

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
 
Top