Android Question Create sub _Click programmatically

cheveguerra

Member
Licensed User
Longtime User
Hi everybody,

I am creating a panel programmatically, and would like to make it so that, if you click on any part of the panel and there is a button behind the panel, it does not trigger the click of that button.

Normally I would just create an empty sub with the panel name and "_Click" (e.g., myPanel_Click), and that would do the trick.

But I would like to know if it is possible to generate the Sub programmatically, or create the same result any other way.

Best regards
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Any panel brought to the front will mask the views underneath it, even when it is tranparent
You don't need a click event handler.

Edit: this works in B4J but not in B4A (I missed that the question was for B4A)
For B4A you need the empty click handler!

B4X:
    Dim maskPanel As B4XView = xui.CreatePanel("")
    Root.AddView(maskPanel, 0, 0, Root.Width, Root.Height)
    '...
    'As last step:
    maskPanel.BringToFront
 
Last edited:
Upvote 0

cheveguerra

Member
Licensed User
Longtime User
Thanks for answering,

Mmmm . . . In my experience, the only way I can stop a click from happening is by using an empty _Click Sub. This applies to buttons, editTexts, or any other elements that can be clicked.

createPanel:
Sub createPanel
    Private panelX As Panel
    Log("add panel")
    panelX.Initialize("pQuest")
    Private lbl As Label
    lbl.Initialize("")
    lbl.Text = "xxx yyy zzz"
    lbl.TextSize = 20
    lbl.TextColor = Colors.Black
'    lbl.Color = Colors.Green
    Private cd As ColorDrawable
    cd.Initialize2(Colors.ARGB(125, 255, 255, 255), 20, 1, Colors.Gray)
    panelX.Background = cd
    p_Main.AddView(panelX, 10, 0, 80%x, 200dip) 'add the panel to the layout
    panelX.AddView(lbl, 20, 10, (panelX.Width * 0.9), 80dip)
    panelX.left = (Root.Width / 2) - (panelX.Width / 2)
    panelX.top = (Root.Height / 2) - (panelX.Height / 2)
    panelX.BringToFront
    panelX.Elevation = 100dip
    panelX.Visible = True
End Sub

Even when I tried using elevation, the click always went through.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You cannot create code at runtime and execute it. Why would you want to do this?

Regarding the panel, you are right, to ensure that the underlying views do not receive the focus, you have to create the Click or Touch event.
 
Upvote 0

cheveguerra

Member
Licensed User
Longtime User
I think the only solution is for each panel you create to assign the same event name and write a single empty Sub Click using that name.
I appreciate your help, that's what I'm doing at the moment, but I thought it'd be more efficient if everything was in the same Sub.

Best regards
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
To make sure that a view behind a Panel does not get any events you need:
1. An empty Click or Touch event.
2. A higher Panel elevation than the views underneath it.
One common event routine for different Panels works.

In the code in post #3, you have this line: panelX.Initialize("pQuest")
Do you have the Sub pQuest_Click routine ?
 
Upvote 0

cheveguerra

Member
Licensed User
Longtime User
To make sure that a view behind a Panel does not get any events you need:
1. An empty Click or Touch event.
2. A higher Panel elevation than the views underneath it.
One common event routine for different Panels works.

In the code in post #3, you have this line: panelX.Initialize("pQuest")
Do you have the Sub pQuest_Click routine ?

The higher Elevation does not work, the empty pQuest_Click Sub does work, and yes, I have it defined!! 👍
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4J code for ex.:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
  
    Dim b As Button
    b.Initialize("button")
    b.Text = "New but1"
    b.Tag = "but1"
    MainForm.RootPane.AddNode(b, 0, 0, 200, 50)
End Sub

Sub button_Click
    Dim b As Button = Sender
    Log(b.Tag)
End Sub
1703340223158.png
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The higher elevation does not work, only the empty pQuest_Click Sub, and yes, I have it defined!! 👍
Perhaps he means that just setting the Elevation is not the solution, it doesn't work. That's right and that's not what Klaus intended, you need both (although BringToFront + the Click event might be enough).

If it still doesn't work, make sure you created pQuest_Click and not panelX_Click.
 
Upvote 0

cheveguerra

Member
Licensed User
Longtime User
Thank a lot everybody,

The problem was not that I couldn't do it, I had it functioning from the beginning with the empty "Sub pQuest_Click".

The initial question was centered on whether it could be accomplished programmatically, and it appears that it cannot be done that way.

BringToFront + Elevation does not work, so I will just have to add the empty pQuest_Click Sub and that's that :p

Best regards.
 
Upvote 0

cheveguerra

Member
Licensed User
Longtime User
Can you post your project ?

Before posting my previous post i did some tests.
Attached my test program.

Here is my code (I added it to your project).

The sub is just a function to create a popup questionnaire ... and have A LOT of logs for debugging :p.

If you comment the Sub on line 105 (pSombra_Click) the clicks go through, even with the Elevation and BringToFront set in lines 94 and 95.

Best regards
 

Attachments

  • TwoPanels.rar
    4.4 KB · Views: 30
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you comment the Sub on line 105 (pSombra_Click) the clicks go through, even with the Elevation and BringToFront set in lines 94 and 95.
Yes, I knew that the empty Click event routine is necessary, I wrote it in post#7.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi everybody,

I am creating a panel programmatically, and would like to make it so that, if you click on any part of the panel and there is a button behind the panel, it does not trigger the click of that button.

Normally I would just create an empty sub with the panel name and "_Click" (e.g., myPanel_Click), and that would do the trick.

But I would like to know if it is possible to generate the Sub programmatically, or create the same result any other way.

Best regards
you can create the panel programmatically and initialize always with the same eventName that is present in your app and does nothing.
like this all new panels will trigger the clicks and not view behind it.

add this sub in your app
B4X:
Sub doesNothingPanel_Click
 ' do nothing
End Sub

add panels programmatically
B4X:
DIm pnl as B4xView = XUI.Createpanel("doesNothingPanel")
...
 
Upvote 0
Top