B4A Library Material AnimatedSwitch

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

  1. DonManfred

    DonManfred Expert Licensed User

    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

    Code:
    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) :)
    [​IMG]
     

    Attached Files:

    Last edited: Jun 18, 2015
  2. Peter Simpson

    Peter Simpson Well-Known Member 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

    Code:
    //@Events(values={"onclick()"})
    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 Well-Known Member 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.
    Code:
    @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.

    EDIT:
    I opened up the jar file and i think i see the problem
    if you look at the code here
    Code:
    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:

    Code:
    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: 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!

    Walter
     
    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

    Code:
    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
     
  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?

    Thanks,
    walter
     
  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:

    Code:
    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
     
  20. DonManfred

    DonManfred Expert Licensed User

    try to move the check to activity_resume or started by an timer
     
Loading...