Android Code Snippet light switch: minimal code , generous resources

With less than 25 lines of code, a light switch transition effect is shown:
1700127958779.png

Edit: A video demo link: video
B4X:
#Region  Project Attributes
    #ApplicationLabel: Light Switch
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    '
End Sub

Sub Globals
    Private x As XmlLayoutBuilder
    Private tButton As B4XView
    Dim tButtonJO As JavaObject
    Private image As B4XView
    Dim imageJO As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    x.LoadXmlLayout(Activity, "main")
    image=x.GetView("image")
    tButton=x.GetView("button")
    Dim event As Object = tButton.As(JavaObject).CreateEventFromUI("android.view.View.OnClickListener","toggleBulb",Null)
    tButton.As(JavaObject).RunMethod("setOnClickListener",Array(event))
End Sub

Private Sub toggleBulb_Event(MethodName As String, Args() As Object)
    Dim drawable As JavaObject=(Me).As(JavaObject).InitializeStatic("android.graphics.drawable.TransitionDrawable")
    imageJO=image.As(JavaObject)
    drawable=imageJO.RunMethod("getDrawable",Null)
    tButtonJO = tButton.As(JavaObject)
    If tButtonJO.RunMethod("isChecked",Null) = True Then
        drawable.RunMethod("startTransition",Array(1000))
    Else
        drawable.RunMethod("reverseTransition",Array(1000))
    End If
End Sub
You have to make sure that all resources are included:
 

Attachments

  • res.zip
    91 KB · Views: 78
Last edited:

Beja

Expert
Licensed User
Longtime User
With less than 25 lines of code, a light switch transition effect is shown:
View attachment 147808
Edit: A video demo link: video
B4X:
#Region  Project Attributes
    #ApplicationLabel: Light Switch
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    '
End Sub

Sub Globals
    Private x As XmlLayoutBuilder
    Private tButton As B4XView
    Dim tButtonJO As JavaObject
    Private image As B4XView
    Dim imageJO As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    x.LoadXmlLayout(Activity, "main")
    image=x.GetView("image")
    tButton=x.GetView("button")
    Dim event As Object = tButton.As(JavaObject).CreateEventFromUI("android.view.View.OnClickListener","toggleBulb",Null)
    tButton.As(JavaObject).RunMethod("setOnClickListener",Array(event))
End Sub

Private Sub toggleBulb_Event(MethodName As String, Args() As Object)
    Dim drawable As JavaObject=(Me).As(JavaObject).InitializeStatic("android.graphics.drawable.TransitionDrawable")
    imageJO=image.As(JavaObject)
    drawable=imageJO.RunMethod("getDrawable",Null)
    tButtonJO = tButton.As(JavaObject)
    If tButtonJO.RunMethod("isChecked",Null) = True Then
        drawable.RunMethod("startTransition",Array(1000))
    Else
        drawable.RunMethod("reverseTransition",Array(1000))
    End If
End Sub
You have to make sure that all resources are included:
How the bulbs are drawn?
 
Top