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,364
  • Java source - BetterDialogs.zip
    8.5 KB · Views: 745
Last edited:

drachmad

Member
Licensed User
Longtime User
Hi Informatix, a nice lib, I like it.
But I see it is still a modal-dialog, and according to the "experts" it is better to use a non-modal dialog in Android.
Dialog class that already use a non-modal, e.g. AnotherDatePicker by Erel and ClsWheel v. 1.4 by Klaus. I don't think it is a difficult task to make a lib became a non-modal dialog.
When I started b4a, I agree that modal dialog is easier to understand because it is like in VB.NET or alike.

After trying a little bit, just an idea.
Although I can make a variable and put the variable inside the BD.msgbox(...), it is nice if I can do like:
<code>
Dim MB As BD_MessageBox
MB.Title = "<B>M</B>essage <B>B</B>ox"
MB.Message = "This<TT> message box </TT>can interpret the <Font Color=#A5A564>HTML</Font> tags.<BR/><H1>Nice!</H1>"
MB.Icon = LoadDrawable("btn_rating_star_on_pressed")
MB.Positive = "<B>Y</B>es"
MB.Cancel = "<B>C</B>ancel"
MB.Negative = "<B>N</B>o"
</code>
 
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime User
Thank you. I will try this out. Would it be possible to add Non-Modal and list (Middle) in one of those dialogs with title (Top) and buttons (Bottom). Special use for listing all Bluetooth device in the list and at the bottom button used for "Scan for Device" or something else.
 

rbsoft

Active Member
Licensed User
Longtime User
Beautiful. I will give it a try.
 

Informatix

Expert
Licensed User
Longtime User
But I see it is still a modal-dialog, and according to the "experts" it is better to use a non-modal dialog in Android.

Android has, natively, no modal dialog because such a dialog would stop the main thread for a time (until the user quits the dialog) and would raise an "Application not responding" error after a few seconds. But in some cases you really need a modal dialog (precisely when you want to stop the flow of your application and do not want any side effect due to the views in the background). Is there a convenient way to do that without a modal dialog? Not really.
If you search in the StackOverflow forum (the main forum for Java users), you'll see that's a concern for a lot of people and has nothing to do with a nostalgy of VB.net (I developped on all kind of systems, with dozens of languages so I'm not plagued by the VB syndrom).
As you may understand, creating a modal dialog is not easy because Android is not designed for it. Fortunately, Erel provided us with a solution that behaves well with the B4A message handler so it's harmless to create modal dialogs for B4A and not too complicated (but you have to use Java to write your library).
A non-modal dialog is just a panel with a few views on it. Most B4A users are able to do that so I didn't feel the need to make a library with non-modal dialogs (that doesn't mean there's not a need).

After trying a little bit, just an idea.
Although I can make a variable and put the variable inside the BD.msgbox(...), it is nice if I can do like:
<code>
Dim MB As BD_MessageBox
MB.Title = "<B>M</B>essage <B>B</B>ox"
MB.Message = "This<TT> message box </TT>can interpret the <Font Color=#A5A564>HTML</Font> tags.<BR/><H1>Nice!</H1>"
MB.Icon = LoadDrawable("btn_rating_star_on_pressed")
MB.Positive = "<B>Y</B>es"
MB.Cancel = "<B>C</B>ancel"
MB.Negative = "<B>N</B>o"
</code>

I don't think I will change that, so, yes, you should use variables.
I didn't do that because I like to see quickly what a function do (by looking at the parameters). When a function has only one parameter grouping many settings, it is difficult at first glance to see what it does. There's no technical reason behind.
 

Informatix

Expert
Licensed User
Longtime User
Thank you. I will try this out. Would it be possible to add Non-Modal and list (Middle) in one of those dialogs with title (Top) and buttons (Bottom). Special use for listing all Bluetooth device in the list and at the bottom button used for "Scan for Device" or something else.

I won't do non-modal dialogs (see the post above).

I'm not sure I will add new dialogs to the library because you can create most of them with my CustomDialog function. If you have troubles with ListView in the dialog, try to use my CheckList class.
 

drachmad

Member
Licensed User
Longtime User
Great job!!!

Hi Informatix, thanks for your long explanation.
Anyway, you do a great job!!!
 

pwme

Member
Licensed User
Longtime User
Dialogs - default dimensions

Hi,

DB.CustomDialog("Filtro",-1,0dip, pnl,100%x,100%y, 0dip, Colors.RGB(0, 0, 0), "Chiudi", Null, Null, True)

as you can see I' m passing 100%x, 100%y, but I see that you leave some default space around the dialog box. How many space do you leave?

For example (2%x left,2% right, 6% top, 2% bottom)?

Thank you in advance
 

Informatix

Expert
Licensed User
Longtime User
Hi,

DB.CustomDialog("Filtro",-1,0dip, pnl,100%x,100%y, 0dip, Colors.RGB(0, 0, 0), "Chiudi", Null, Null, True)

as you can see I' m passing 100%x, 100%y, but I see that you leave some default space around the dialog box. How many space do you leave?

For example (2%x left,2% right, 6% top, 2% bottom)?

Thank you in advance

The OS uses a 9-patch drawable to display the dialog. This drawable has a padding, but I don't remember exactly its size. Personaly, I set the Margin parameter to 20dip and that seems to be good for all versions of the OS.

Note for the custom dialog: DlgWidth+(2*Margin) should never exceed 100%x.
 

urikupfer

Member
Licensed User
Longtime User
Hi Informatix
Is there a way to use CustomDialog more than one's ,when the views declared in sub Globals ?, because in the second call there is an error massage:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
at android.view.ViewGroup.addView(ViewGroup.java:3210)
at android.view.ViewGroup.addView(ViewGroup.java:3155)
at android.view.ViewGroup.addView(ViewGroup.java:3131)
at flm.b4a.betterdialogs.BetterDialogs.CustomDialog(BetterDialogs.java:493)
at flm.b4a.testdialogs.main._activity_resume(main.java:315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at flm.b4a.testdialogs.main.afterFirstLayout(main.java:95)
at flm.b4a.testdialogs.main.access$100(main.java:16)
at flm.b4a.testdialogs.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 

Mahares

Expert
Licensed User
Longtime User
@Frederic: Comme vos autres, cette librairie est superbe.
I tried: <mark>Informatix</mark> to highlight a text, but it does not highlight it. Apparently it does not support HTML 5.0 tags that were not available in 4.1. Is that correct or don't I know what I am talking about?
 

basil99

Active Member
Licensed User
Longtime User
if i press "cancel" at inputbox, i got this

B4X:
LogCat connected to: B4A-Bridge: CCI Cosmo-355117042007297
Connected to B4A-Bridge (Bluetooth)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Installing file.
PackageAdded: package:flm.b4a.testdialogs
listening on: 159a04cc-f60c-4c14-89a8-c8f22bae9dfe
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
DR=-1
main_activity_resume (B4A line: 47)
Return r.RunMethod2("getDrawable", ID_Drawable, "java.lang.int")
java.lang.NoSuchMethodError: flm.b4a.maskededittext.MaskedEditTextWrapper.getCompactText
   at flm.b4a.betterdialogs.BetterDialogs.InputBox(BetterDialogs.java:424)
   at flm.b4a.testdialogs.main._activity_resume(main.java:326)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at flm.b4a.testdialogs.main.afterFirstLayout(main.java:95)
   at flm.b4a.testdialogs.main.access$100(main.java:16)
   at flm.b4a.testdialogs.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
   at dalvik.system.NativeStart.main(Native Method)

same with some text entered and Ok button.

Android version 2.2


Also I noticed Masked Edit text lib is not checked when i load the sample, but nothing change if i load it manually in libs tab
 
Top