B4J Question [SOLVED]Move a Button using Code

Peter Lewis

Active Member
Licensed User
Longtime User
Hi all

Is it possible to move a button code ?

I have a matrix of lines which I want to drag a button into one of the squares in the matrix and I want to snap to grid.

So I can read the position and see which is the closest square when I release the dragging of the button. I then want it to align into that block.

I cannot find an option , unless I re-initialize the button . the button is created in the designer designer

Thank you

1708966693393.png




B4X:
Private Sub Button2_MouseDragged (EventData As MouseEvent)
'    Log("mouse dragged")
    Dim view As B4XView = Sender
    Dim pd As PositionData = view.Tag
    Dim ParentX As Int = EventData.X + view.Left
    Dim ParentY As Int = EventData.Y + view.Top
    view.Left = ParentX - pd.PressedX
    view.Top = ParentY - pd.PressedY
End Sub



Private Sub Button2_MousePressed (EventData As MouseEvent)
    Log("Button2_MousePressed")
   
'    Log(EventData.PrimaryButtonPressed)
   
   
    Dim view As B4XView = Sender
    Dim pd As PositionData
    pd.PressedX = EventData.X
    pd.PressedY = EventData.Y
    view.Tag = pd
    Log(view.Tag)
   
End Sub

Private Sub Button2_MouseReleased (EventData As MouseEvent)
    Dim view As B4XView = Sender
    Log("released")
    Log(EventData.X)
    Log(EventData.Y)
   
    Dim pd As PositionData
    pd.PressedX = EventData.X
    pd.PressedY = EventData.Y
        view.Tag = pd
   
       
End Sub

button move.gif
 
Last edited:

Peter Lewis

Active Member
Licensed User
Longtime User
I just happen to have a demo (in B4J) that almost does what you want - it is a "drag-and-drop" example that moves buttons around on a grid. Here it is - it might not be exactly what you need but it might move you forwards . . .
Thank You

THAT IS EXACTLY WHAT I WANT TO DO, HOW DID YOU KNOW.

I love the Forum, So many people are so helpful and share their knowledge without any hesitation to help other people.

I am proud to be part of a community like this and @Erel should also be proud
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I wrote the demo rather quickly. I have now added two lines of code that I think improve the visual effect considerably ...

B4X:
' Respond to a mouse down action over the button grid
Public Sub touch_MousePressed(EventData As MouseEvent)
    ' Ignore unless this is the first (ie "button-down") event
    If EventData.PrimaryButtonDown And Not(dragging) Then
        ...  ...  ...
        ...  ...  ...
        dragging = True
        start.Visible = False     <=== ADD THIS LINE
    End If
End Sub

      ...  ...  ...
    

' Update data in response to drag-drop operation
Private Sub updateData(start As Int, stop As Int)
    ... ... ...
    btnA.Text = btnB.Text
    btnA.Visible = True                 <=== ADD THIS LINE
    btnB.Text = temp
    ' Any database updates should also be done here ...
    ' ... ... ... ... ...
End Sub

The demo code in post #2 has been updated.
 
Upvote 0
Top