Android Tutorial Dialogs library 2017 - Custom dialogs and async methods

Status
Not open for further replies.
Dialogs v4 is an extension to @agraham original dialogs library. It requires B4A v7+.

SS-2017-06-01_16.00.52.png


The main changes are:

- New CustomLayoutDialog which makes it easier to develop custom dialogs.
- New ShowAsync methods for all dialogs except of CustomDialog and CustomDialog2.

The ShowAsync methods behave similar to Msgbox2Async and the other internal async dialogs: DoEvents deprecated and async dialogs (msgbox)

Example:
B4X:
Dim cd As ColorDialog
Dim sf As Object = cd.ShowAsync("Choose Color", "Yes", "Cancel", "No", Null, False)
Wait For (sf) Dialog_Result(Result As Int)
If Result = DialogResponse.POSITIVE Then
   Activity.Color = cd.RGB
End If
The async methods also include a Cancelable parameter that determines whether the dialog can be cancelled with the back key or a click outside of the dialog.
There are also other small improvements in the async methods implementation.
As explained in the tutorial about DoEvents, it is recommended to switch to the async methods.
Do note that ShowAsync + Wait For behavior is not identical to the modal dialogs behavior. When Wait For is called, the code returns to the calling sub (if there is one). If you want that the calling sub will wait for the sub that shows the async dialog: https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/page-2#post-499130


CustomLayoutDialog

CustomLayoutDialog replaces CustomDialog and CustomDialog2. As it is a non-modal dialog, it is more powerful. You can handle events while the dialog is visible and manage the dialog itself.
Note that CustomLayoutDialog is supported by Android 4+ (API 14+).

Showing the dialog is done in two steps:

1. Call ShowAsync and wait for Dialog_Ready event.
2. Load the layout to the panel (or create it programmatically).

The Dialog_Result event will be raised when the dialog is closed.

You can close the dialog programmatically by calling CloseDialog with the result:
B4X:
Dialog.CloseDialog (DialogResponse.POSITIVE)

You can explicitly set the dialog's size by calling SetSize. You need to call it after calling ShowAsync and before the Dialog_Ready event.
Note that the size set is the size of the dialog. The panel itself will be smaller. Use a layout with anchors to make sure that the layout adapts properly.

Dialog.GetButton will return one of the dialogs buttons. It can be used to disable or enable a button.
For example this code enables the OK button when all fields are non-empty.
B4X:
Sub DialogAge_ItemClick (Position As Int, Value As Object)
   CheckAllFieldsValid
End Sub

Sub DialogLastName_TextChanged (Old As String, New As String)
   CheckAllFieldsValid
End Sub

Sub DialogFirstName_TextChanged (Old As String, New As String)
   CheckAllFieldsValid
End Sub

Sub CheckAllFieldsValid
   Dim valid As Boolean = DialogAge.SelectedIndex > 0 And DialogFirstName.Text.Length > 0 And DialogLastName.Text.Length > 0
   DetailsDialog.GetButton(DialogResponse.POSITIVE).Enabled = valid
End Sub

The example is attached.

The library is available here: https://www.b4x.com/android/forum/threads/dialogs-library.6776

Edit: Cross platform custom layout dialog based on XUI: https://www.b4x.com/android/forum/threads/99756/#content
 

Attachments

  • DialogsExample.zip
    9.8 KB · Views: 4,054
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code and it works properly:
B4X:
Sub Activity_Click
   Dim dd As CustomLayoutDialog
   Dim sf As Object = dd.ShowAsync("Filtri Anagrafiche", "OK", "Annulla", "", Null, False)
   dd.SetSize(100%x, 100%y)
   Wait For (sf) Dialog_Ready(pnl As Panel)
  Dim sv As ScrollView
   sv.Initialize(1000dip)
   'pnl.LoadLayout("frmFiltriAnagrafiche")
   pnl.AddView(sv, 0dip,0dip, pnl.Width, pnl.Height)
   sv.Panel.LoadLayout("2")
   Wait For (sf) Dialog_Result(res As Int)
End Sub
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
I solved.
If it serves someone, I have changed the height of the scrollview and the form becomes dynamic and allows to overcome the vertical dimension.

B4X:
msvFiltri.Panel.Height    =    m_pnlFiltri.Height + 100dip
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
It would be interesting to be able to change the background colors and dialog text, and also the style of the character.
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, and sorry for my ignorance. Maybe I'm talking nonsense.
But, is not this too convoluted?
Is there no other way to ask the user for a data?
I'm thinking something similar to this:
B4X:
Res = InputDialogAsync ("name", "", "ok", "cancel", icon)
Wait For ...
 

ema01

Member
Licensed User
Longtime User
Hi erel,
in the iOS version of an application i used iCustomDialog to create..well, custom dialogs.
I can change the background with ease, set the buttons styles with ease and so on.

Is it possible to do the same in android, without resorting to changing the theme?

basically, this is want i'd like to obtain
example.png
 

ema01

Member
Licensed User
Longtime User
To refine my question, I was able to change almost everything..

to get a uniform tint in the dialog you have to change the panel's color, obviously ("panel" is the one declared in Dialog_Ready method)
you have also to change the background of the area behind the buttons. to do so you can access that area as a view

B4X:
Dim btn As Button
btn = dialog.GetButton(DialogResponse.CANCEL)
If btn.IsInitialized Then
  Dim pnlButtons As B4XView = btn.parent
End If

pnlButtons is the view behind the dialog buttons. Now you can change the color as well.
The dialog buttons appereances can be changed of course by getting them with the BetButton method

However, there are two things i still haven't figured out how to do:
- how to change the height of the buttons and of pnlButtons
- how to get to the root of the dialog view so i can
a)change the borders, make rounded borders (applying a background to panel or pnlButtons will result in a partial rounded background, not the whole dialog)
b)make the dialog, for example, semi-transparent. could be interesting for something else i have in mind which i could do with dialogs instead of popup panels, which i'll definetly do with iCustomDialog with iOS

if anybody can help me :)

EDIT:
Ok, i don't know why it didn't show before, but i can actually change the buttons width and height.
I think the view behind (pnlButtons) is actually a scroll view (edit: No, it isn't), as scrolling is enabled if the height of the cancel button is smaller than the height of the other buttons. It seems it uses the cancel button's height to set its height.

I still can't access the panel behind..

EDIT2:
Almost... There...
20180704_174518.jpg


I still can't figure out how to change the dialog view borders, nor how to change the pnlButtons height, so that i can have the same distance from bottom as it is from the sides.

I tried casting pnlButtons as a scrollView but i get this error
B4X:
java.lang.ClassCastException: com.android.internal.widget.ButtonBarLayout cannot be cast to android.widget.ScrollView

ButtonBarLayout?

Anyway, i kept referencing to parents over parents, i tried going back 5 levels hoping to find the main view or at least a null reference exception but nothing. (tested by changing the colors to different colors and then backwards to transparent.)
 
Last edited:
Status
Not open for further replies.
Top