B4J Question How to not consume the click event

nima66

Member
In Android, I used the following code so that when the transparent panel is touched, the touch is not consumed and when the button is touched, the button event is also executed.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Private deleteButton As Button
    deleteButton.Initialize("deleteButton")
    deleteButton.Text="Delete Item"
    Activity.AddView(deleteButton, 10%x, 5%y, 20%x, 8%y)

    TransparentPanelOnTop.Initialize("TransparentPanelOnTop")
    Activity.AddView(TransparentPanelOnTop, 0, 0, 100%x, 100%y)
    TransparentPanelOnTop.Color = Colors.Transparent
    TransparentPanelOnTop.BringToFront
    
    Dim rf As Reflector
    rf.Target = TransparentPanelOnTop
    rf.SetOnTouchListener("TransparentPanelOnTop_Touch")
End Sub

Sub TransparentPanelOnTop_Touch (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean 'Return True to consume the event
    dv1.closeView
    Return False
End Sub
Sub deleteButton_Click
    Log("deleteButton_Click")
End Sub

In b4j, when the pane click is not consumed, its own click event and the parent click event are executed.
How can I make the pane click event and the button click event be executed?
 
Solution
Don't add a transparent panel. Add a filter event on the root panel:
B4X:
    Dim r As Reflector
    r.Target = Root
    r.AddEventFilter("RootTouch", "javafx.scene.input.MouseEvent.MOUSE_PRESSED")
End Sub

Private Sub RootTouch_Filter(e As Event)
    Dim mouse As MouseEvent = e
    Log(mouse.X & ", " & mouse.Y)
End Sub

nima66

Member
What is the purpose of the transparent panel? Do you need its touch events?
There are several options in the main panel
A transparent panel is added over them
Then a small custom menu that I created on top of all of them
I want the menu to close if the custom menu is open and any part of the transparent panel is touched.
This way there is no need to drag and drop to close the menu and the user can close the menu more easily
If the touch of the transparent panel is consumed, we can no longer touch the options below the transparent panel
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't add a transparent panel. Add a filter event on the root panel:
B4X:
    Dim r As Reflector
    r.Target = Root
    r.AddEventFilter("RootTouch", "javafx.scene.input.MouseEvent.MOUSE_PRESSED")
End Sub

Private Sub RootTouch_Filter(e As Event)
    Dim mouse As MouseEvent = e
    Log(mouse.X & ", " & mouse.Y)
End Sub
 
Upvote 1
Solution

nima66

Member
Don't add a transparent panel. Add a filter event on the root panel:
B4X:
    Dim r As Reflector
    r.Target = Root
    r.AddEventFilter("RootTouch", "javafx.scene.input.MouseEvent.MOUSE_PRESSED")
End Sub

Private Sub RootTouch_Filter(e As Event)
    Dim mouse As MouseEvent = e
    Log(mouse.X & ", " & mouse.Y)
End Sub
It was great
Thank you Erel
 
Upvote 0
Top