Android Example Simple switch button with image view

anallie0

Active Member
Licensed User
Hi
The application I'm writing I need switches, but not being an expert I arranged with some simple line of code. The result is good and does its work.
I used an image view and an int variable.
How the example attached, I change the image depending on the state of button and set a variable to store the state of the button.
Surely there are more sophisticated methods, and the experts will be able to add improvements and suggestions.

With this method you can only use images but not text. I have saved dozens of png images for everyone at this link:
https://drive.google.com/folderview...k3VEFZb2ZIQXFDQ0Iwb1c5M3p4bldyQlk&usp=sharing

You can use them at your leisure.


B4X:
#Region  Project Attributes
    #ApplicationLabel: Slide Button
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: true
#End Region

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim but1 As ImageView
    Dim set1 = 0 As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("slide")

    but1.SetLayout(42%x, 5%y, 16%x, 5%y)
    but1.Bitmap = LoadBitmap(File.DirAssets, "but 2 OFF.png")

End Sub

Sub but1_Click
set1=set1+1
If set1=1 Then
but1.Bitmap = LoadBitmap(File.DirAssets, "but 2 ON.png")
'your ON Sub
Else
set1=0
but1.Bitmap = LoadBitmap(File.DirAssets, "but 2 OFF.png")
'your OFF Sub
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
 

Attachments

Last edited:
Top