B4J Tutorial Modal dialogs

B4J allows you to show modal dialogs or modal forms.
The difference between a modal form to non-modal form is that with a modal form the calling code will only continue after the form is closed.

Showing a modal form is done by calling Form.ShowAndWait.

For example to create a very simple color picker:
SS-2013-11-19_09.12.06.png


B4X:
Sub btnColor_Action
   Dim mf As ModalForm
   mf.Initialize(MainForm)
   MainForm.BackColor = mf.Show
End Sub

ModalForm is a class. Internally it calls Form.ShowAndWait.

B4X:
Public Sub Show As Paint
   frm.ShowAndWait
   'The next line will only be executed after the form is closed.
   Return colorsMap.Get(lstColors.SelectedItem)
End Sub

Msgbox library is available here: Msgbox library
 

Attachments

  • ModalExample.zip
    2 KB · Views: 2,027
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Erel... Anyway to do this in Basic4Android??? I don't understand how to a) create the forms, and b) to link it into b4a. I'd like to make a small, modal dialog that pops up with a panel containing 2 labels, 2 sliders, and 3 buttons down below... Want to pop that up modal, allow settings, and then dismiss the panel.

I've searched and I came up with this B4J example, but am pretty sure I can't use it! Any suggestions would be appreciated... Thnx in advance...
 

Shadow&Max

Active Member
Licensed User
Longtime User
Thank you... None of them suit my needs... I was able to do it by disabling controls underneath while my panel was visible, and re-enable everything after it goes away.
 

derez

Expert
Licensed User
Longtime User
Instead of enabling and disabling, you can put the dialog on a panel covering the whole screen (transparent or opaque or partial) and consume any touch by this panel.
 

Shadow&Max

Active Member
Licensed User
Longtime User
See, I didn't want to cover the entire screen... I wanted it to look like a pop-up dialog box. I was able to do it very cleanly...
 

FrenchDeveloper

Member
Licensed User
Longtime User
Instead of enabling and disabling, you can put the dialog on a panel covering the whole screen (transparent or opaque or partial) and consume any touch by this panel.
Hello Derez,
In B4j, how to allow clicks on underlaying applications when the transparent panel is topmost ?
 

jonydoboi

Member
Licensed User
Longtime User
Another example based on the first one. The return type is String instead of Paint.
It took a while to figure out.
 

Attachments

  • Modal.zip
    2 KB · Views: 794
Last edited:

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Erel, for your continued support.

Your competence is only exceeded by your perseverance - I do hope you find a couple of clones....

The example in post 1 does not work in Win 10. Since I do not know how to modify FXML (there is no Form in the Views), I created a form in Open Designer. The only way it works is by setting the form backcolor to Transparent.

I tried to improve the app (Ha! - presumptuous of me), and I got stuck :oops:

I got this far:
B4X:
    For i = 0 To colorsMap.Size -1
        Dim lblx As TextField
        lblx.Initialize("")
        lblx.Text=colorsMap.GetKeyAt(i)
        lblx.Style="-fx-text-fill: " & colorsMap.GetKeyAt(i) & ";"
        Log(lblx.Text)
        lstColors.Items.Add(lblx)
    Next

which shows each item in the Listview in the matching color.

Since I am adding a label, I DO NOT KNOW HOW TO RETRIEVE ITS TEXT VALUE AND "CLICK" DOES NOT WORK ON CLICKING THE LABEL.
How do I do this?

Thank you.

Sandy
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I al not at my pc so i am not so sure about the answer but if you are passing the textfield to the listview you can retrieve the information not with a click of the textfield but with the event on selection changed of the listview

Dim tf as textfield = listview.get( listview.selectedindex)

Something like that.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Enrique.

B4X:
    Dim tf As TextField = lstColors.Items.get(lstColors.selectedindex)
    Log(tf.text)
    Return fx.Colors ??????????

It is as above. Now that I have the name of the color in tf.text, how do I return the corresponding fx.colors?

Sandy
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I hope it is like this

Return Mapcolors.getkeyat(lstColors.Items.get(lstColors.selectedindex)

You will return the specified index of the colorsmap. Well I hope as soon as I am at my computer I will try it too. I
 

tdocs2

Well-Known Member
Licensed User
Longtime User
I hope it is like this

Return Mapcolors.getkeyat(lstColors.Items.get(lstColors.selectedindex)

You will return the specified index of the colorsmap. Well I hope as soon as I am at my computer I will try it too. I

Close - Thank you, Enrique - you put me on the right track. This is the code:

B4X:
Public Sub Show As Paint
    frm.ShowAndWait
    Dim tf As TextField = lstColors.Items.get(lstColors.selectedindex)
    Return colorsMap.Get(tf.text)
End Sub

Best wishes.

Sandy

PS: How can I capture a click on the label itself? Got to get some sleep....
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
for a label within a listview:

B4X:
sub listview_selectedindexchanged(index as int)
     dim l as label 
     label = listview.items.get(index)
     log(label.text)
end sub
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Enrique.

The sub works, but it does not capture the mouse click on the label. I have to click outside of the label....

Best wishes.

Sandy
 
Top