Android Question [Solve] SetColorAnimated with Corner Radius

rraswisak

Active Member
Licensed User
Hi all,

I have a label with corner radius set to 20, then i want to change the color with SetColorAnimated method, the color was change but the corner radius is missing.

B4X:
Sub Label1_Click
     Dim Col1, Col2 As Int
     Col1 = Colors.Red
     Col2 = Colors.Green

     Label1.SetColorAnimated(500, Col1, Col2)
End Sub

If i define Col1 and Col2 as ColorDrawable, it raise error
B4X:
Sub Label1_Click
     Dim col1, col2 As ColorDrawable
     col1.Initialize(Colors.ARGB(175,255,0,0),20dip)
     col2.Initialize(Colors.ARGB(175,0,255,0),20dip)
     Label1.SetColorAnimated(500, Col1, Col2)
End Sub
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_label1_click (java line: 385)
java.lang.NumberFormatException: Invalid double: "(GradientDrawableWithCorners) anywheresoftware.b4a.objects.drawable.ColorDrawable$GradientDrawableWithCorners@5356ef18"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:114)
at java.lang.StringToReal.parseDouble(StringToReal.java:263)
at java.lang.Double.parseDouble(Double.java:295)
at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:662)
at b4a.example.main._label1_click(main.java:385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

thank you
 

rraswisak

Active Member
Licensed User
Thank you erel, but i think SetColorAnimated will remove corner radius previously set in designer.

So every time color has change, the corner radius will gone

Without Animated, i can change the color with ColorDrawable, in this case i want to use SetColorAnimated
 
Upvote 0

rraswisak

Active Member
Licensed User
Yes its work, but sorry i miss one information, in designer, i set label drawable to GradientDrawable.

try to figure it out with this code, but still get error:
B4X:
Sub Label1_Click
   Dim col1, col2 As GradientDrawable
   Dim c1(2), c2(2) As Int
   c1(0) = xui.Color_Blue
   c1(1) = xui.Color_White
   col1.Initialize("TOP_BOTTOM",c1)
   col1.CornerRadius = 20
   
   c2(0) = xui.Color_Red
   c2(1) = xui.Color_White
   col2.Initialize("TOP_BOTTOM",c2)
   col2.CornerRadius = 20
   
   If posLeft Then
       Label1.SetColorAnimated(500,col1, col2)
   Else
       Label1.SetColorAnimated(500,col2, col1)
   End If
   posLeft = Not(posLeft)
End Sub

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 73 (Main)
java.lang.NumberFormatException: For input string: "(GradientDrawable) android.graphics.drawable.GradientDrawable@17092b8"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:662)
at b4a.example.main._label1_click(main.java:458)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6300)
at android.view.View$PerformClick.run(View.java:24941)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
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:857)
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
 
Upvote 0

rraswisak

Active Member
Licensed User
ok, i have decide to use ColorDrawable instead of GradientDrawable

this is final result:
toggle.gif


thank you for all your support
 
Upvote 0
Top