Android Question on off switch .....

steve007

Member
Total newbie here looking for guidance, I want to put an image on the screen then when you touch the image it changes to another image. That's it but I really don't know how to even start this. Thanks for any help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Something like:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     img1 = LoadBitmap(File.DirAssets, "image1.jpg")
     img2 = LoadBitmap(File.DirAssets, "image2.jpg")
   End If
   Activity.SetBackgroundImage(img1)
End Sub

Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   If Action = Activity.ACTION_DOWN Then
     Activity.SetBackgroundImage(img2)
   Else If Action = Activity.ACTION_UP Then
     Activity.SetBackgroundImage(img1)
   End If
End Sub
 
Upvote 0

steve007

Member
Thanks I really appreciate that, I haven't tried it yet but all this app coding is a lot more complicated than I thought but that's a great starting point for me. I've a long way to go before my first app. Cheers
 
Upvote 0

steve007

Member
Really sorry I've probably done something wrong but I get a parsing error. I've created img1 & img2 and they're in the right folder. Everything looks right (as far as I can see) but I get this error:
Error description: Undeclared variable 'img2' is used before it was assigned any value.
Occurred on line: 37
Activity.SetBackgroundImage(img2)

Any help would be great
 
Upvote 0

steve007

Member
It's work full respect to you & you respond so quickly, I'm just trying to figure out how to keep the second image on the screen without going back to the first image. I have two pictures of a lightbulb, img1 is in the off position and img2 is in the onn position. When I press img1 it gets replaced with img2 but when I take my finger of the image it goes back to img1. I know this will be simple and I will figure it out I can't believe what a massive learning curve this is. Cheers for your help it's much appreciated
 
Upvote 0
Top