Android Question Animation to Custom View

Cableguy

Expert
Licensed User
you have to implement it yourself…

If you could share your custom view code, we could provide more insight and help, otherwise all we can do is give guidelines and general/generic answers.
 

Kiran Raotole

Active Member
Licensed User
ok this is my custom view code :
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    'mBase = Base
    mHeight = Base.Height
    
    Base.Color = Colors.Red
    Dim cd As ColorDrawable
    cd.Initialize(Props.Get("Color"),20)
    Base.Background = cd
    mBase.Initialize("mBase")
    mButton.Initialize("mButton")
    Base.AddView(mBase,0,0,Base.Width,mHeight)
    mBase.AddView(Lbl,0,0,Base.Width,mHeight)
    mBase.Background = cd
    
    If Props.Get("NoDefaultColor")="N" Then
        mBase.Color = Colors.RGB(191,16,232)
    Else
        mBase.Color = Props.Get("Color")
    End If
    
    If Props.Get("NoDefaultFontSize")="N" Then
        Lbl.TextSize = 14
    Else
        Lbl.TextSize = Props.Get("FontSize")
    End If
    
    If Props.Get("NoDefaultTextColor")="N" Then
        Lbl.TextColor = Colors.Black
    Else
        Lbl.TextColor = Props.Get("TextColor")
    End If
    
    Lbl.Text = Props.Get("Text")
    

End Sub
and this is my custom_click code :
B4X:
Private Sub mBase_Click
    'mBase.SetVisibleAnimated(1000,True)
    If SubExists(mCallBack,mEventName &  "_Click") Then
        CallSub(mCallBack,mEventName & "_Click")
    End If
End Sub
I want to give it animation like button click.
 

Star-Dust

Expert
Licensed User
Use Reflector library
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    mBase = Base
 
    Dim obj As Reflector
    obj.Target = mBase
    obj.SetOnClickListener("mBase_Click")
   
    Dim cd As ColorDrawable
    cd.Initialize(Props.Get("Color"),20)
    mBase.Background = cd
   
    If Props.Get("NoDefaultColor")="N" Then
        mBase.Color = Colors.RGB(191,16,232)
    Else
        mBase.Color = Props.Get("Color")
    End If
   
    If Props.Get("NoDefaultFontSize")="N" Then
        Lbl.TextSize = 14
    Else
        Lbl.TextSize = Props.Get("FontSize")
    End If
   
    If Props.Get("NoDefaultTextColor")="N" Then
        Lbl.TextColor = Colors.Black
    Else
        Lbl.TextColor = Props.Get("TextColor")
    End If
   
    Lbl.Text = Props.Get("Text")
   
End Sub
 

Kiran Raotole

Active Member
Licensed User
it shows error :
java.lang.Exception: Sub mbase_click signature does not match expected signature.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:193)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.agraham.reflection.Reflection$2.onClick(Reflection.java:985)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24774)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 

Star-Dust

Expert
Licensed User
B4X:
Private Sub mBase_Click (viewtag as obiject)
    'mBase.SetVisibleAnimated(1000,True)
    If SubExists(mCallBack,mEventName &  "_Click") Then
        CallSub(mCallBack,mEventName & "_Click")
    End If
End Sub
 

npsonic

Active Member
Licensed User
If you want to create custom button just change base to transparent and add default button on base.
It's easier to change how button looks like than trying to create button from panel.
 
Last edited:

LucaMs

Expert
Licensed User
'mBase = Base
mBase.Initialize("mBase")
Note:

a) you don't need to initialize mBase; just declare it (in Class_Globals) and set it "equal" to Base (all this is already written in the custom view template);
b) you don't need to create a new drawable, just use the Base drawable, which will/can be set by the user (the developer) by Designer;
c) you don't need to add a listener for the button you are adding;
d) you don't need to check if the event sub exists;

[d) the way you're using to set the default values is at least unusual]
https://www.b4x.com/android/forum/threads/custom-view-with-designer-support.28953/


I'm attaching an example; you can change the animation, for example zooming in and out your button, instead of moving it as I did.
You can see that I changed the button color, border color and corners (and text) simply by Designer.

1.gif


Few lines of code to do it:
B4X:
Sub Class_Globals
    ' ...
    Private mBase As Panel
    Private mButton As Button
End Sub
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
   mBase = Base

   mButton.Initialize("mButton")
   Base.AddView(mButton, 0, 0, Base.Width, Base.Height)
   mButton.Background = Base.Background
 
   mButton.Text = Lbl.Text
   mButton.TextSize = Lbl.TextSize
End Sub
B4X:
Private Sub mButton_Click
   Dim Duration As Int = 50
   Dim Offset As Float = 1.05
   Wait For (Animation (mBase, Duration, mBase.Left * Offset, mBase.Top * Offset)) Complete (Dummy As Boolean)
   Wait For (Animation (mBase, Duration, mBase.Left / Offset, mBase.Top / Offset)) Complete (Dummy As Boolean)
   CallSub(mCallBack, mEventName & "_Click")
End Sub

Private Sub Animation (VW As View, duration As Long, X As Int, Y As Int) As ResumableSub
   VW.SetLayoutAnimated(duration, X, Y, VW.width, VW.Height)
   Sleep(duration)
   Return True
End Sub

[Well done; I like this post :D
upload_2018-9-2_3-11-23.png
]
 

Attachments

Last edited:

Cableguy

Expert
Licensed User
Note:

d) you don't need to check if the event sub exists;
If you Don't check for the sub existance (this sub you are checking is not the one in the Library it self) and if it doesn't exist, it will throw an error and crash the app...
A dev may want want while checking the lib usage, see how it behaves in its simplest form and not implement all triggerable subs., so if you Don't check for instance, you may not even be able to test the lib visual behaviour.
 

LucaMs

Expert
Licensed User
If you Don't check for the sub existance (this sub you are checking is not the one in the Library it self) and if it doesn't exist, it will throw an error and crash the app
Download my example, remove Sub cvMyButton1_Click from the Main and tell me if your app crashes.
 
Top