B4A Library [Lib] BetterDialogs

I tried to improve the most useful dialogs of the Dialogs library. In my version, HTML tags are interpreted (even in the text of buttons), the InputBox uses a Masked EditText and has more options, you can check if the input is valid before closing the dialog, and the CustomDialog is fully customizable. I also solved an issue with the virtual keyboard.

MsgBox (under Gingerbread and JellyBean):

msgbox.png


InputBox (under Gingerbread and JellyBean):

inputbox.png


CustomDialog (under Gingerbread and JellyBean):

cd1.jpg


Another CustomDialog (under Gingerbread and JellyBean):

cd2.png


v1.1:
- I added a CloseDialog function;
- I added a margin parameter to the custom dialog.

v1.2:
- I fixed a bug in InputBox when there's a default value;
- I added the BackKeyPressed event for custom dialogs.

v1.3:
- I added a PasswordMode option to the InputBox;
- I added a CancelOnTouchOutside option.

v1.4:
- I added a CursorPosition option to the InputBox.

v2.0:
- I moved the CustomDialog parameters to the BD_CustomDlgParams class.
- I added two new parameters: Duration and DialogGravity.
- I removed the Margin parameter and added the MarginHeight parameter (to be used when Gravity is set to TOP).
- The BodyWidth and BodyHeight parameters defines now accurately the size of the dialog body.

v2.1:
- I added a "closing" event raised just before a custom dialog is dismissed. You can cancel the closure by returning False.

v2.11:
- I added the "DlgResponse" parameter to the Closing event.

This library does not work with Android versions < 2.
It requires another library: Masked EditText
 

Attachments

  • BetterDialogs v2.11.zip
    55.6 KB · Views: 2,369
  • Java source - BetterDialogs.zip
    8.5 KB · Views: 749
Last edited:

baron

Member
Licensed User
Longtime User
Since Android 5, the buttons are too small and squeezed together on the right. How can I fix this ?
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Created a custom dialog loaded from a layout. I have a button in the layout, with a click event. I know the click event for this button is dispatched. From in the click event I am trying to use a message box (BD.MsgBox) to confirm the user's selected action. But it does not display. In the debugger it jumps to the end of the button click routine with no clue as to why in the log.

B4X:
Sub btnResetDatabase_Click

    BD.MsgBox("CONFIRM","Are you sure?","Yes","","No",Null)

    Select Case BD.MsgBox( _
        "CONFIRM DATA RESET", _
        "Are you sure you want to DELETE ALL ORDERS on this mobile device." & CRLF &  _
        "Your edits to these orders will be permanently and irretrievably lost.", _
        "YES", _
        "cancel", _
        "NO",Null)
        Case DialogResponse.POSITIVE
        Case DialogResponse.NEGATIVE
        Case DialogResponse.CANCEL
    End Select
   
End Sub

Neither message box displays. I tried the one NOT a member of Better Dialogs, and it does not display either.

Is it allowed? If not is there some guide or reference to what CAN NOT be done from within a custom dialog?
 

Informatix

Expert
Licensed User
Longtime User
Created a custom dialog loaded from a layout. I have a button in the layout, with a click event. I know the click event for this button is dispatched. From in the click event I am trying to use a message box (BD.MsgBox) to confirm the user's selected action. But it does not display. In the debugger it jumps to the end of the button click routine with no clue as to why in the log.

B4X:
Sub btnResetDatabase_Click

    BD.MsgBox("CONFIRM","Are you sure?","Yes","","No",Null)

    Select Case BD.MsgBox( _
        "CONFIRM DATA RESET", _
        "Are you sure you want to DELETE ALL ORDERS on this mobile device." & CRLF &  _
        "Your edits to these orders will be permanently and irretrievably lost.", _
        "YES", _
        "cancel", _
        "NO",Null)
        Case DialogResponse.POSITIVE
        Case DialogResponse.NEGATIVE
        Case DialogResponse.CANCEL
    End Select
 
End Sub

Neither message box displays. I tried the one NOT a member of Better Dialogs, and it does not display either.

Is it allowed? If not is there some guide or reference to what CAN NOT be done from within a custom dialog?
I don't know where's the issue without the whole code, but you should place your MsgBox code in a different sub and call this code with CallDelayedSub.
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
B4X:
Sub btnResetDatabase_Click
    CallSubDelayed(Me,"ResetDatabase")
End Sub

Sub ResetDatabase

    Select Case BD.MsgBox( _
        "Are you sure you want to DELETE ALL ORDERS on this mobile device." & CRLF &  _
        "Your edits to these orders will be permanantly and irretrievably lost.", _
        "CONFIRM DATA RESET", _
        "YES", _
        "cancel", _
        "NO",Null)
        Case DialogResponse.POSITIVE
        Case DialogResponse.NEGATIVE
        Case DialogResponse.CANCEL
    End Select

End Sub
FYI I tried replacing the button click event handler with a CallSubDelayed from which the BD.MsgBox is called. From within the dialog, clicking the button, the button event is called as evidenced in the debugger. CallSubDelayed is called. But it simply resumes to allow the dialog to operate. Once exiting the dialog the msgbox appears. Can not seem to get the msgbox to display WHILE the dialog is still active.
 

Informatix

Expert
Licensed User
Longtime User
FYI I tried replacing the button click event handler with a CallSubDelayed from which the BD.MsgBox is called. From within the dialog, clicking the button, the button event is called as evidenced in the debugger. CallSubDelayed is called. But it simply resumes to allow the dialog to operate. Once exiting the dialog the msgbox appears. Can not seem to get the msgbox to display WHILE the dialog is still active.
Sorry but I don't understand what you try to do. Are you trying to display two message boxes at the same time ? It's impossible.
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I was trying to us a message box to confirm an action initiated by clicking a button within a custom dialog. Are you saying that a custom dialog is a message box?

(added) Haven't seen your reply yet, but I presume the answer is yes, which tells me I need to use a new activity for my preferences and settings editor screen, rather than a custom dialog.
 

Informatix

Expert
Licensed User
Longtime User
I was trying to us a message box to confirm an action initiated by clicking a button within a custom dialog. Are you saying that a custom dialog is a message box?
Both are modal dialogs. Modal dialogs are not natively supported and use a set of functions written by Erel for B4A. Among other limitations, you cannot display two of them at the same time.
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Bit by bit my understanding grows. Thank you for taking time to explain it.
 

skyracer90

Member
Licensed User
Longtime User
Hallo to Informatix,
thanks for this great lib....
Where can i find a list of
LoadDrawable picture

thx, Rainer
 

Dey

Active Member
Licensed User
Longtime User
I tried to improve the most useful dialogs of the Dialogs library. In my version, HTML tags are interpreted (even in the text of buttons), the InputBox uses a Masked EditText and has more options, you can check if the input is valid before closing the dialog, and the CustomDialog is fully customizable. I also solved an issue with the virtual keyboard.

MsgBox (under Gingerbread and JellyBean):

View attachment 18214

InputBox (under Gingerbread and JellyBean):

View attachment 18213

CustomDialog (under Gingerbread and JellyBean):

View attachment 18279

Another CustomDialog (under Gingerbread and JellyBean):

View attachment 18280

v1.1:
- I added a CloseDialog function;
- I added a margin parameter to the custom dialog.

v1.2:
- I fixed a bug in InputBox when there's a default value;
- I added the BackKeyPressed event for custom dialogs.

v1.3:
- I added a PasswordMode option to the InputBox;
- I added a CancelOnTouchOutside option.

v1.4:
- I added a CursorPosition option to the InputBox.

v2.0:
- I moved the CustomDialog parameters to the BD_CustomDlgParams class.
- I added two new parameters: Duration and DialogGravity.
- I removed the Margin parameter and added the MarginHeight parameter (to be used when Gravity is set to TOP).
- The BodyWidth and BodyHeight parameters defines now accurately the size of the dialog body.

This library does not work with Android versions < 2.
It requires another library: Masked EditText


Hello Informatix
you can wrap the new line with CRLF
example
Message dim as string = "Ok CONTINUE" & CRLF "NO STOP"
BD.Msgbox (Title, Message, "Ok", "No", LoadBitmap (File.DirAssets, Icon))
Result now is
Ok CONTINUE NO STOP
QUALITIES would
Ok CONTINUE
NO STOP
Thanks for an answer
congratulations for your work
 

Informatix

Expert
Licensed User
Longtime User
Hello Informatix
you can wrap the new line with CRLF
example
Message dim as string = "Ok CONTINUE" & CRLF "NO STOP"
BD.Msgbox (Title, Message, "Ok", "No", LoadBitmap (File.DirAssets, Icon))
Result now is
Ok CONTINUE NO STOP
QUALITIES would
Ok CONTINUE
NO STOP
Thanks for an answer
congratulations for your work
HTML is expected in the message so you can break lines with the <BR/> tag.
 

Harris

Expert
Licensed User
Longtime User
Hello,

I am having an issue with BD_MessageBox.
The sub below (EndThisShift) calls MB in my BDmod code module.
On "occasion", when the dialog appears, it will execute the Else clause without selecting any button....?
When this occurs, selecting "Yes" closes the messagebox but does not execute the Positive result since (i don't know why)...
I repeat this process many times, it may behave correctly and WAIT for the DialogResponse and work correctly.

Any Thoughts?

MB sub included (which I modified to handle text sizes...)

Thanks



B4X:
Sub EndThisShift

  Dim res As Int

  res = BDmod.MB( "Shift End Requested", "Are You Sure You Want To End Your Shift?", " Yes","Cancel","")
  If res = DialogResponse.POSITIVE Then
    ToastMessageShow1("Shift Ended",False)
    CallSubDelayed( LogServmod,"Shiftend")
    apm1.Visible = False
    apm2.Visible = False
    GaugeInited = False
  Else
    ToastMessageShow1("Shift Was Not Ended....",False) ' "Sometimes" I hit this WITHOUT selecting Yes or Cancel...
  End If
End Sub



B4X:
Sub MB(title As String,msg As String,pos As String,can As String,neg As String) As Int
    Dim gBD As BetterDialogs
    Dim dlg_params As BD_CustomDlgParams
    Dim DR As Int
    Dim port As Boolean
    Dim lval As LayoutValues
    Dim sc As Float
    Dim Scrn As Double
    Dim psize, lsize As Int
    lval = GetDeviceLayoutValues
    sc = lval.scale
    Scrn = lval.ApproximateScreenSize
    If Scrn < 6 Then
       psize = 18
       lsize = 20
    Else
       psize = 35
       lsize = 45
    End If

    If Scrn > 7.5 Then
       psize = 55
       lsize = 65
    End If  

    If lval.Height > lval.Width Then
       port = True
    Else
       port = False
    End If


    Dim btnOK As Button
    btnOK.Initialize("")
    btnOK.Text = pos
    btnOK.TextColor = Colors.White

    If port = True Then
      btnOK.TextSize = psize / sc
    Else
      btnOK.TextSize = lsize / sc
    End If
    btnOK.Background = CreateStateListDrawable1

    Dim btnCancel As Button
    btnCancel.Initialize("")
    btnCancel.Text = can
    If port = True Then
      btnCancel.TextSize = psize / sc
    Else
      btnCancel.TextSize = lsize / sc
    End If
    btnCancel.TextColor = Colors.White
    btnCancel.Background = CreateStateListDrawable1

    Dim btnneg As Button
    btnneg.Initialize("")
    btnneg.Text = neg
    If port = True Then
      btnneg.TextSize = psize / sc
    Else
      btnneg.TextSize = lsize / sc
    End If
    btnneg.TextColor = Colors.White
    btnneg.Background = CreateStateListDrawable1


    Dim pnlBody As Panel
    pnlBody.Initialize("")
    Dim sld As StateListDrawable
    sld.Initialize
    Dim gd1 As GradientDrawable
    gd1.Initialize( "TOP_BOTTOM", Array As Int(Colors.DarkGray, Colors.Black))
    gd1.CornerRadius = 0dip
    sld.AddCatchAllState(gd1)

    Dim lab As Label


    lab.Initialize("")
    lab.Text = msg
'lab.Text = "Are You Sure You Want To End Your Shift Today. Maybe it will be tomorrow? It could even be the day after that - Who Knows?"
    lab.TextColor = Colors.Yellow
    lab.Gravity = Gravity.CENTER_HORIZONTAL+Gravity.CENTER_VERTICAL

dlg_params.Initialize
dlg_params.Title = title
dlg_params.TitleWidth = -1
dlg_params.TitleHeight = 60dip
dlg_params.DialogBody = pnlBody
dlg_params.BodyWidth = 90%x
dlg_params.BodyHeight = 40%y
dlg_params.MarginHeight = 0dip
dlg_params.Background = sld
dlg_params.PositiveButton = btnOK
dlg_params.CancelButton = btnCancel
dlg_params.NegativeButton = btnneg
dlg_params.OpenKeyboard = False



    If port = True Then
       If Scrn < 6 Then
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
         lab.TextSize = SetTextSize(lab,lab.Text,sc)

         DR = gBD.CustomDialog( dlg_params, "CD2"   )
       Else
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
         lab.TextSize = SetTextSize(lab,lab.Text,sc)
         dlg_params.Title = "<H1>"&title&"</H1>"

         DR = gBD.CustomDialog(dlg_params, "CD2")
  
       End If
    Else

        pnlBody.AddView(lab , 10%x, 0 , 65%x, 55%y)
        lab.TextSize = SetTextSize(lab ,lab.Text, sc)
         dlg_params.Title = "<H1>"&title&"</H1>"
         dlg_params.BodyWidth = 90%x
         dlg_params.BodyHeight = 60%y
         dlg_params.DialogBody = pnlBody

         DR = gBD.CustomDialog(dlg_params,  "CD2")
    End If

    Return DR

End Sub
 

Attachments

  • Screenshot_2015-09-27-21-33-32.png
    Screenshot_2015-09-27-21-33-32.png
    223.4 KB · Views: 221
Last edited:

Informatix

Expert
Licensed User
Longtime User
Hello,

I am having an issue with BD_MessageBox.
The sub below (EndThisShift) calls MB in my BDmod code module.
On "occasion", when the dialog appears, it will execute the Else clause without selecting any button....?
When this occurs, selecting "Yes" closes the messagebox but does not execute the Positive result since (i don't know why)...
I repeat this process many times, it may behave correctly and WAIT for the DialogResponse and work correctly.

Any Thoughts?

MB sub included (which I modified to handle text sizes...)

Thanks



B4X:
Sub EndThisShift

  Dim res As Int

  res = BDmod.MB( "Shift End Requested", "Are You Sure You Want To End Your Shift?", " Yes","Cancel","")
  If res = DialogResponse.POSITIVE Then
    ToastMessageShow1("Shift Ended",False)
    CallSubDelayed( LogServmod,"Shiftend")
    apm1.Visible = False
    apm2.Visible = False
    GaugeInited = False
  Else
    ToastMessageShow1("Shift Was Not Ended....",False) ' "Sometimes" I hit this WITHOUT selecting Yes or Cancel...
  End If
End Sub



B4X:
Sub MB(title As String,msg As String,pos As String,can As String,neg As String) As Int
    Dim gBD As BetterDialogs
    Dim dlg_params As BD_CustomDlgParams
    Dim DR As Int
    Dim port As Boolean
    Dim lval As LayoutValues
    Dim sc As Float
    Dim Scrn As Double
    Dim psize, lsize As Int
    lval = GetDeviceLayoutValues
    sc = lval.scale
    Scrn = lval.ApproximateScreenSize
    If Scrn < 6 Then
       psize = 18
       lsize = 20
    Else
       psize = 35
       lsize = 45
    End If

    If Scrn > 7.5 Then
       psize = 55
       lsize = 65
    End If 

    If lval.Height > lval.Width Then
       port = True
    Else
       port = False
    End If


    Dim btnOK As Button
    btnOK.Initialize("")
    btnOK.Text = pos
    btnOK.TextColor = Colors.White

    If port = True Then
      btnOK.TextSize = psize / sc
    Else
      btnOK.TextSize = lsize / sc
    End If
    btnOK.Background = CreateStateListDrawable1

    Dim btnCancel As Button
    btnCancel.Initialize("")
    btnCancel.Text = can
    If port = True Then
      btnCancel.TextSize = psize / sc
    Else
      btnCancel.TextSize = lsize / sc
    End If
    btnCancel.TextColor = Colors.White
    btnCancel.Background = CreateStateListDrawable1

    Dim btnneg As Button
    btnneg.Initialize("")
    btnneg.Text = neg
    If port = True Then
      btnneg.TextSize = psize / sc
    Else
      btnneg.TextSize = lsize / sc
    End If
    btnneg.TextColor = Colors.White
    btnneg.Background = CreateStateListDrawable1


    Dim pnlBody As Panel
    pnlBody.Initialize("")
    Dim sld As StateListDrawable
    sld.Initialize
    Dim gd1 As GradientDrawable
    gd1.Initialize( "TOP_BOTTOM", Array As Int(Colors.DarkGray, Colors.Black))
    gd1.CornerRadius = 0dip
    sld.AddCatchAllState(gd1)

    Dim lab As Label


    lab.Initialize("")
    lab.Text = msg
'lab.Text = "Are You Sure You Want To End Your Shift Today. Maybe it will be tomorrow? It could even be the day after that - Who Knows?"
    lab.TextColor = Colors.Yellow
    lab.Gravity = Gravity.CENTER_HORIZONTAL+Gravity.CENTER_VERTICAL

dlg_params.Initialize
dlg_params.Title = title
dlg_params.TitleWidth = -1
dlg_params.TitleHeight = 60dip
dlg_params.DialogBody = pnlBody
dlg_params.BodyWidth = 90%x
dlg_params.BodyHeight = 40%y
dlg_params.MarginHeight = 0dip
dlg_params.Background = sld
dlg_params.PositiveButton = btnOK
dlg_params.CancelButton = btnCancel
dlg_params.NegativeButton = btnneg
dlg_params.OpenKeyboard = False



    If port = True Then
       If Scrn < 6 Then
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
         lab.TextSize = SetTextSize(lab,lab.Text,sc)

         DR = gBD.CustomDialog( dlg_params, "CD2"   )
       Else
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
         lab.TextSize = SetTextSize(lab,lab.Text,sc)
         dlg_params.Title = "<H1>"&title&"</H1>"

         DR = gBD.CustomDialog(dlg_params, "CD2")
 
       End If
    Else

        pnlBody.AddView(lab , 10%x, 0 , 65%x, 55%y)
        lab.TextSize = SetTextSize(lab ,lab.Text, sc)
         dlg_params.Title = "<H1>"&title&"</H1>"
         dlg_params.BodyWidth = 90%x
         dlg_params.BodyHeight = 60%y
         dlg_params.DialogBody = pnlBody

         DR = gBD.CustomDialog(dlg_params,  "CD2")
    End If

    Return DR

End Sub
You should replace this dialog by a standard MsgBox2 and re-try. Do you have the same issue?
 

Harris

Expert
Licensed User
Longtime User
You should replace this dialog by a standard MsgBox2 and re-try. Do you have the same issue?

I shall give it a try. It is so infrequent it is hard to trap.

Thanks
 

Dey

Active Member
Licensed User
Longtime User
Hello to all
I have to enter a tax code
you can set so that the keyboard stays in upper case?
IP.InputType = IP.INPUT_TYPE_TEXT_WITH_CAPS
only the first letters then switch to lower case,
You can be limited as such. 16 characters when entering?
Thank you
 
Top