Android Question Moving Labels based on touch event

Discussion in 'Android Questions' started by FERNANDO SILVEIRA, Apr 20, 2018.

  1. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Hello Guys,

    I'm working to move labels on the screen based on touch event, but it seems touch it is only available at activity level. Is that true?

    I based my research on the ThreeActivityExample.b4a project.
    Code:
    Sub Activity_Touch (Action As Int, X As Float, Y As Float)
        
    Private dX, dY As Float
        
    Log("Action " & Action & " X=" & x & " Y=" & Y)
        
    Select Action                                    ' Selects the Action parameter
            Case Activity.ACTION_DOWN            ' Checks if ACTION_DOWN
                X0 = X                                            ' Memorizes the X coordinate
                Y0 = Y                                            ' Memorizes the Y coordinate
                X1 = lblLetter.Left                        ' Memorizes the Left coordinate
                Y1 = lblLetter.Top                        ' Memorizes the Top coordinate

            
    Case Activity.ACTION_MOVE            ' Checks if ACTION_MOVE
                dX = X - X0                                    ' Calculates the X distance moved
                dY = Y - Y0                                    ' Calculates the X distance moved
                lblLetter.Left = X1 + dX            ' Sets the new Left coordinate
                lblLetter.Top = Y1 + dY                ' Sets the new Top coordinate
     
            
    Case Activity.ACTION_UP                ' Checks if ACTION_UP
                Log("Left " & lblLetter.Left) ' Memorizes Left in the Map
                Log("Top " & lblLetter.Top)     ' Memorizes Top in the Map
                lblCoord.Text = (lblLetter.Left & "-" & lblLetter.Top)

        
    End Select
    End Sub

    This code works ok for a single label, but let's say a have 3-4 labels on the screen that I want to move then randomly (one each time). Which approach should I take?

    Regards,
    Fernando
     

    Attached Files:

  2. Jorge M A

    Jorge M A Active Member Licensed User

    FERNANDO SILVEIRA likes this.
  3. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Ok... Will have a look on it.
    Thanx
     
  4. Jorge M A

    Jorge M A Active Member Licensed User

    BTW, if you decide to use this approach, you must use the Drag event.
    Attached you app whit two labels and GD.
    Try it in release mode, and better in real device.
    Regards.
     

    Attached Files:

  5. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Jorge, I'm more than impressed. Thank you very much for your help.

    Had no idea we could accomplish this with so little code (Extented thanx to @Informatix for his amazing library, BTW).
     
  6. Jorge M A

    Jorge M A Active Member Licensed User

    Yes, in fact the solution is based on one of the examples of the library.
    @Informatix has created many fantastic ones.
    It is well worth becoming one of his donors. ;)
     
    FERNANDO SILVEIRA likes this.
  7. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice