Android Question Dialog with B4X buttons only

Mark Stuart

Active Member
Licensed User
Longtime User
Hi all,

I'm wanting to display a dialog with a title and 2 buttons, one under the other.
I will not use the default "Yes", "No", "Cancel" dialog buttons. They will not be defined and therefore will not appear.

Each button will have its own _Click function.
When a button is clicked, the _Click function will run and then the dialog will close.
Preferably, the dialog will close first then the subsequent code will run.

I've looked thru the forum for this, but found nothing like I've mentioned here.
Any suggestions would be welcome on how to do this.

Thanx,
Mark Stuart

EDIT: Yes, I could define a panel and load the layout to it, but would rather use a B4X dialog.
 

LucaMs

Expert
Licensed User
Longtime User
EDIT: Yes, I could define a panel and load the layout to it, but would rather use a B4X dialog.

You can assign any text to the three dialog buttons - "Yes," "No," and "Cancel" - and only use two of them.

If you really don't want to use a panel and two buttons, don't want to create your own custom dialog and absolutely want to try using the B4XDialog, you'll have to try moving its buttons. I don't know if this can be done without modifying the source code, but you can try.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
and absolutely want to try using the B4XDialog, you'll have to try moving its buttons. I don't know if this can be done without modifying the source code, but you can try.

An example of how you can modify B4XDialog at runtime (without modifying its source code), by accessing its Views:
B4X:
    Dim dlg As B4XDialog
    dlg.Initialize(Root)
   
    Dim rs As ResumableSub = dlg.Show("Message", "Do it", "Do other", "")
   
    Dim Base As B4XView = dlg.Base
   
    Dim lblText As B4XView = Base.GetView(2).GetView(0)
    lblText.SetLayoutAnimated(0, 10dip, 0dip, Base.Width - 20dip, 40dip)
   
    Dim FirstButton As B4XView = dlg.GetButton(xui.DialogResponse_Positive)
    FirstButton.SetColorAndBorder(xui.Color_Green, 1, xui.Color_White, 8dip)
    FirstButton.TextColor = xui.Color_White
    Dim FirstButtonTop As Int = lblText.Top + lblText.Height
    FirstButton.SetLayoutAnimated(0, 10dip, FirstButtonTop, Base.Width / 2, 40dip)
   
    Dim SecondButton As B4XView = dlg.GetButton(xui.DialogResponse_Negative)
    SecondButton.SetColorAndBorder(xui.Color_Red, 1, xui.Color_White, 8dip)
    SecondButton.TextColor = xui.Color_White
    Dim SecondButtonTop As Int = FirstButton.Top + FirstButton.Height + 10dip
    SecondButton.SetLayoutAnimated(0, 10dip, SecondButtonTop, Base.Width / 2, 40dip)
   
    Wait For (rs) Complete (Result As Int)
    Select Result
        Case xui.DialogResponse_Positive
            'Do it
            Log("Do it")
        Case xui.DialogResponse_Negative
            'Do other
            Log("Do other")
'        Case xui.DialogResponse_Cancel
'            Log("Don't do anything")
    End Select

1758344652919.png


It's not "pretty" but now you know how to do it.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
You can create your own custom layout.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You can create your own custom layout.
Yes but then the 3 fundamental buttons must be there and in those positions (at least one of them for closing the dialog)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes but then the 3 fundamental buttons must be there and in those positions (at least one of them for closing the dialog)
a dialog with a title and 2 buttons, one under the other.
I will not use the default "Yes", "No", "Cancel" dialog buttons. They will not be defined and therefore will not appear.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
This code line throws an error:
Dim rs As ResumableSub = dlg.Show("Message", "Do it", "Do other", "")

java.lang.ClassCastException: com.mfs.bx.birthdays.addeditperson cannot be cast to android.view.View
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This code line throws an error:
Dim rs As ResumableSub = dlg.Show("Message", "Do it", "Do other", "")

java.lang.ClassCastException: com.mfs.bx.birthdays.addeditperson cannot be cast to android.view.View

Are you sure that's the line generating the error? It's correct, unless you've misdeclared dlg. It must be of type B4XDialog ("Xui Views" library), preferably declared in Class_Globals or Process_Globals.

Also make sure you don't already have a variable named "rs" of a different type (I don't think that's the problem, but it's better to be sure).
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Here's my code. The Dim dlg as B4XDialog is the only place it is declared:
B4X:
Sub OpenDialog
    Dim dlg As B4XDialog
    dlg.Initialize(Me)
    
    Dim rs As ResumableSub = dlg.Show("Message", "Do it", "Do other", "")
    Dim base As B4XView = dlg.Base
    Dim lblText As B4XView = base.GetView(2).GetView(0)
    lblText.SetLayoutAnimated(0, 10dip, 0dip, base.Width - 20dip, 40dip)
    
    Dim FirstButton As B4XView = dlg.GetButton(xui.DialogResponse_Positive)
    FirstButton.SetColorAndBorder(xui.Color_Green,1dip,xui.Color_White,8dip)
    FirstButton.TextColor = xui.Color_White
    Dim FirstButtonTop As Int = lblText.Top + lblText.Height
    FirstButton.SetLayoutAnimated(0,10dip, FirstButtonTop, base.Width / 2, 40dip)
    
    Dim SecondButton As B4XView = dlg.GetButton(xui.DialogResponse_Negative)
    SecondButton.SetColorAndBorder(xui.Color_Red, 1dip, xui.Color_White, 8dip)
    SecondButton.TextColor = xui.Color_White
    Dim SecondButtonTop As Int = FirstButton.Top + FirstButton.Height + 10dip
    SecondButton.SetLayoutAnimated(0, 10dip, SecondButtonTop, base.Width / 2, 40dip)
    
    Wait For (rs) Complete (Result As Int)
    Select Result
        Case xui.DialogResponse_Positive
            'Do it
            Log("Do it - Yes")
        Case xui.DialogResponse_Negative
            'Do other
            Log("Do other - No")
            '        Case xui.DialogResponse_Cancel
            '            Log("Don't do anything")
    End Select
End Sub
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
That change made it work - thank you!
Learning B4XPages - something new and trying to get this old dog to learn new tricks :{

Mark
 
Upvote 0
Top