Android Question Help with B4XDialog (Create Button Event)

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I need to create a dialog box with a custom design where I can modify the quantity of a product.

1757524368226.png


I'm using this code for B4A:

Code: Copy
    Base=Activity
    Dialog.Initialize (Base)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 300dip, 300dip) 'set the content size
    p.LoadLayout("FrmFactDetEditarCant")
    Dialog.ShowCustom(p, "Ok", "", "Cancel")


But I can't get the quantity to increase when I press the blue button, even though it's already coded within an activity module.

How can I code these button events in B4XDialog?

Thank you very much for your response.
 
Solution
Okay, now it's solved. Simply add the Button objects to the view. See the code in B4A.

The first thing is to use the XIU Views library. Declare the variables in Globals.
Code: Copy
Private Dialog As B4XDialog
Private Base As B4XView
Private xui As XUI

Code: Copy
Dim btn As Button
btn.Initialize("btnsumarcant")
btn.SetBackgroundImage(LoadBitmap(File.DirAssets, "MasAzul32.png"))
Dim btn1 As Button
btn1.Initialize("btnrestarcant")
btn1.SetBackgroundImage(LoadBitmap(File.DirAssets, "MenosRojo32.png"))
Base=Activity
Dialog.Initialize (Base)
Dim p As B4XView = xui.CreatePanel("")
'Add the Buttons in Dialog
p.AddView(btn, 200dip, 60dip, 50dip, 50dip)
p.AddView(btn1, 200dip, 120dip, 50dip,50dip)
p.SetLayoutAnimated(0, 0, 0, 300dip, 300dip) 'set the...
How can I code these button events in B4XDialog?
Just add the event to the module where you load the layout with the buttons.
If this dosent work, then provide an example projekct. what shows the problem.
 
Upvote 0
@Alexander Stolte

I've already created the activity module, and it works fine when done with StartActivity(FrmFactDetEditarCant). The buttons work fine.

But the problem seems to be that B4XDialog only loads the layout, but not the events when the buttons are pressed.

Is there any way to do this?
 
Upvote 0
Okay, now it's solved. Simply add the Button objects to the view. See the code in B4A.

The first thing is to use the XIU Views library. Declare the variables in Globals.
Code: Copy
Private Dialog As B4XDialog
Private Base As B4XView
Private xui As XUI

Code: Copy
Dim btn As Button
btn.Initialize("btnsumarcant")
btn.SetBackgroundImage(LoadBitmap(File.DirAssets, "MasAzul32.png"))
Dim btn1 As Button
btn1.Initialize("btnrestarcant")
btn1.SetBackgroundImage(LoadBitmap(File.DirAssets, "MenosRojo32.png"))
Base=Activity
Dialog.Initialize (Base)
Dim p As B4XView = xui.CreatePanel("")
'Add the Buttons in Dialog
p.AddView(btn, 200dip, 60dip, 50dip, 50dip)
p.AddView(btn1, 200dip, 120dip, 50dip,50dip)
p.SetLayoutAnimated(0, 0, 0, 300dip, 300dip) 'set the content size
Dialog.ShowCustom(p, "Edit", "", "Cancel")

Then add the events in the same activity module. See Code in B4A.

Code: Copy
Sub btnsumarcant_Click
    lblcant.Text=NumberFormat(lblcant.Text+1, 0, 0)
End Sub
Sub btnrestarcant_Click
    lblcant.Text=NumberFormat(lblcant.Text-1, 0, 0)
    If lblcant.Text<=0 Then
        lblcant.Text=0
    End If
End Sub

Thank you very much. I learned a lot today.
 
Last edited:
Upvote 0
Solution
I feel the need to point out that while it obviously can be done in the class as you have proved, using templates that B4x provides will help keep your modules tidier and your project better structured and easier to maintain in the long run. You have added 20+ lines of code to your class, where as using a template would add considerably less. Do that on 2 or 3 occasions and you class will start looking a bit full. Just a suggestion, but worth considering.
 
Upvote 0
Back
Top