Android Question Problem with LongClick

Eduardo Pajaro

Member
Licensed User
Receive a warm greeting.

Please help me with the following:

I do not have a cell phone, I'm learning from the emulator.

I have placed 10 images (in file) of a frog in white and another 10 frogs in color, I store them in an array. To save Kb ImageView by code (Img.Initialize ("Imageview")). When loading the activity I show the 10 white frogs, I give a CLICK event and change that frog (tag) to the color tag. UNTIL ALL GOOD !!!.

The problem starts with the ImageView_longClick event, I want to do the opposite but using the longClick, that is, the color frog goes to white, but this event stays in infinity and does not make me the change I want.

I want to do it this way because a customer can erase a choice in this way,

Place color frog  CLICK

Back the frog to white  LONGCLICK


Help me please.

I attach the code ... THANKS



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

Sub Process_Globals

End Sub

Sub Globals


Dim Azar As Short
Dim PosH, PosV As Short
Dim ImgRana(11) As ImageView
Dim Panel1 As Panel


End Sub


Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("LayoutJugar")

Pro_Crear_Ranas

End Sub

'---------FIN------------------FIN------------------FIN---------------------- FIN -----------------------FIN-----------

Sub Pro_Crear_Ranas ' Estas son las ranas mostradas al arrancar el activity de Jugar
Dim b As Short
PosH = 1

For b = 1 To 10
Dim Img As ImageView
Img.Initialize("Imageview")
Img.Tag = b
Img.Gravity = Gravity.FILL
ImgRana(b) = Img
Select b
Case 1: ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca1.jpg"))
Case 2:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca2.jpg"))
Case 3:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca3.jpg"))
Case 4:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca4.jpg"))
Case 5:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca5.jpg"))
Case 6:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca6.jpg"))
Case 7:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca7.jpg"))
Case 8:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca8.jpg"))
Case 9:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca9.jpg"))
Case 10:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca10.jpg"))

End Select

Panel1.AddView(ImgRana(b), PosH, 180, 44, 44)
PosH = PosH + 45
Next

End Sub


Sub Pro_Recambio (rr As Short)
Select rr
Case 1:ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1.png"))
Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor2.png"))
Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor3.png"))
Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor4.png"))
Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor5.png"))
Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor6.png"))
Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor7.png"))
Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor8.png"))
Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor9.png"))
Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor10.png"))
End Select
End Sub

Sub Pro_cambio(w As Short )
Select w
Case 1: ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca1.jpg"))
Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca2.jpg"))
Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca3.jpg"))
Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca4.jpg"))
Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca5.jpg"))
Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca6.jpg"))
Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca7.jpg"))
Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca8.jpg"))
Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca9.jpg"))
Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca10.jpg"))

End Select

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub






Sub ImageView_Click
Dim I As ImageView
I = Sender
Pro_Recambio(I.Tag )
End Sub


Sub ImageView_LongClick
Dim I As ImageView
I = Sender
Log(I)
Pro_cambio(I.Tag )

End Sub
 

ronell

Well-Known Member
Licensed User
Longtime User
I do not have a cell phone, I'm learning from the emulator.
emulator is not recommended

also put codes in code tags
CODETAGS.png
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Run the app in debug mode, put a breakpoint in your ImageView_LongClick & then step through the code & see what it's doing. This is usually the best way to figure out where something is going wrong...

Also, to avoid confusion (& possible errors) you should probably avoid using object names as prefixes for subs - eg: instead of ImageView_Click, maybe use img_Click or FrogImage_Click.

- Colin.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You initialize
B4X:
Img.Initialize("Imageview") 'View is lowercase

but call
B4X:
Sub ImageView_Click ' View is uppercase
..
End Sub

Sub ImageView_LongClick ' View is uppercase
..
End Sub

Try
B4X:
Sub Imageview_Click
..
End Sub

Sub Imageview_LongClick
..
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
You initialize
B4X:
Img.Initialize("Imageview") 'View is lowercase

but call
B4X:
Sub ImageView_Click ' View is uppercase
..
End Sub

Sub ImageView_LongClick ' View is uppercase
..
End Sub
I'm pretty sure sub names are not case sensitive in b4a.

- Colin.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I did run the code with a slight change because I do not have the layout, it works for me
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim Azar As Short
    Dim PosH, PosV As Short
    Dim ImgRana(11) As ImageView
    Dim Panel1 As Panel
End Sub


Sub Activity_Create(FirstTime As Boolean)

    'Activity.LoadLayout("LayoutJugar")
    Panel1.Initialize("Panel1")
    Activity.AddView(Panel1,0,0,100%x,100%y)

    Pro_Crear_Ranas
End Sub

'---------FIN------------------FIN------------------FIN---------------------- FIN -----------------------FIN-----------
Sub Pro_Crear_Ranas ' Estas son las ranas mostradas al arrancar el activity de Jugar
    Dim b As Short
    PosH = 1

    For b = 1 To 10
        Dim Img As ImageView
        Img.Initialize("Imageview")
        Img.Tag = b
        Img.Gravity = Gravity.FILL
        ImgRana(b) = Img
        Select b
            Case 1: ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca1.jpg"))
            Case 2:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca2.jpg"))
            Case 3:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca3.jpg"))
            Case 4:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca4.jpg"))
            Case 5:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca5.jpg"))
            Case 6:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca6.jpg"))
            Case 7:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca7.jpg"))
            Case 8:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca8.jpg"))
            Case 9:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca9.jpg"))
            Case 10:ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca10.jpg"))
              
        End Select
      
        Panel1.AddView(ImgRana(b), PosH, 180, 44, 44)
        PosH = PosH + 45
    Next
  
End Sub


Sub Pro_Recambio (rr As Short)
    Select rr
        Case 1:ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1.png"))
        Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor2.png"))
        Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor3.png"))
        Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor4.png"))
        Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor5.png"))
        Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor6.png"))
        Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor7.png"))
        Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor8.png"))
        Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor9.png"))
        Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor10.png"))
    End Select
End Sub

Sub Pro_cambio(w As Short )
    Select w
        Case 1: ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca1.jpg"))
        Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca2.jpg"))
        Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca3.jpg"))
        Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca4.jpg"))
        Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca5.jpg"))
        Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca6.jpg"))
        Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca7.jpg"))
        Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca8.jpg"))
        Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca9.jpg"))
        Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca10.jpg"))
          
    End Select
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ImageView_Click
    Dim I As ImageView
    I = Sender
    Pro_Recambio(I.Tag )
End Sub


Sub ImageView_LongClick
    Dim I As ImageView
    I = Sender
    Log(I)
    Pro_cambio(I.Tag )

End Sub

But, in my view, it is not necessary to double-click to return to the initial image.

One click to change it and one click to go back. The Tab can store what image it contains at that time and change it with the other
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Select case is useless and too repetitive where there are variables that can be indexed as in this case. The files are distinguished by a number. I suggest this that seems simpler

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

    Dim Azar As Short
    Dim PosH, PosV As Short
    Dim ImgRana(11) As ImageView
    Dim Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)

    'Activity.LoadLayout("LayoutJugar")
    Panel1.Initialize("Panel1")
    Activity.AddView(Panel1,0,0,100%x,100%y)

    Pro_Crear_Ranas
End Sub

'---------FIN------------------FIN------------------FIN---------------------- FIN -----------------------FIN-----------
Sub Pro_Crear_Ranas ' Estas son las ranas mostradas al arrancar el activity de Jugar
    Dim b As Short
    PosH = 1

    For b = 1 To 10
        Dim Img As ImageView
        Img.Initialize("Imageview")
        Img.Tag = "ranablanca" & B
        Img.Gravity = Gravity.FILL
        ImgRana(b) = Img
        ImgRana(b).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranablanca" & B & ".jpg"))
        Panel1.AddView(ImgRana(b), PosH, 180, 44, 44)
        PosH = PosH + 45
    Next
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ImageView_Click
    Dim I As ImageView = Sender
    Dim fi As String =  I.tag
   
    If fi.StartsWith("ranablanca") Then
        i.SetBackgroundImage(LoadBitmap (File.DirAssets, fi.replace("ranablanca","ranacolor") & ".png"))
        I.Tag=fi.replace("ranablanca","ranacolor")
    Else
        i.SetBackgroundImage(LoadBitmap (File.DirAssets, fi.replace("ranacolor","ranablanca") & ".JPG"))
        I.Tag=fi.replace("ranacolor","ranablanca")
    End If
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Another point.
You can replace this code:
B4X:
Sub Pro_Recambio (rr As Short)
    Select rr
    Case 1:ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1.png"))
    Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor2.png"))
    Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor3.png"))
    Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor4.png"))
    Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor5.png"))
    Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor6.png"))
    Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor7.png"))
    Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor8.png"))
    Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor9.png"))
    Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor10.png"))
    End Select
End Sub
Simply by this:
B4X:
Sub Pro_Recambio (rr As Short)
    ImgRana(rr).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1" & rr & ".png"))
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Another point.
You can replace this code:
B4X:
Sub Pro_Recambio (rr As Short)
    Select rr
    Case 1:ImgRana(1).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1.png"))
    Case 2:ImgRana(2).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor2.png"))
    Case 3:ImgRana(3).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor3.png"))
    Case 4:ImgRana(4).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor4.png"))
    Case 5:ImgRana(5).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor5.png"))
    Case 6:ImgRana(6).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor6.png"))
    Case 7:ImgRana(7).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor7.png"))
    Case 8:ImgRana(8).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor8.png"))
    Case 9:ImgRana(9).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor9.png"))
    Case 10:ImgRana(10).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor10.png"))
    End Select
End Sub
Simply by this:
B4X:
Sub Pro_Recambio (rr As Short)
    ImgRana(rr).SetBackgroundImage (LoadBitmap (File.DirAssets, "ranacolor1" & rr & ".png"))
End Sub

From my example I removed Pro_Cambio and PRO_Recambio because in the event management I entered everything with the same method suggested by you.
B4X:
Sub ImageView_Click
    Dim I As ImageView = Sender
    Dim fi As String =  I.tag
  
    If fi.StartsWith("ranablanca") Then
        i.SetBackgroundImage(LoadBitmap (File.DirAssets, fi.replace("ranablanca","ranacolor") & ".png"))
        I.Tag=fi.replace("ranablanca","ranacolor")
    Else
        i.SetBackgroundImage(LoadBitmap (File.DirAssets, fi.replace("ranacolor","ranablanca") & ".JPG"))
        I.Tag=fi.replace("ranacolor","ranablanca")
    End If
End Sub
 
Upvote 0
Top