B4A Library Material Dialogs - Make your dialogs nice

I think this library is the third attempt in this forum to wrap the excellent afollestad Material Dialogs library.

The difference to the other library wrappers is that this one is (except of some minor features) a complete wrap including file- and folder choosers, color chooser and a complete wrap of the core material dialog including very well support fror custom layouts. Not wrapped are the preference dialogs (We have the Preferences library for this).

I think the library (and the API) now is quite stable. Nevertheless I call it "beta" because I might have overseen anything. Also there are a few bugs in the underlying library which I hope will be fixed soon.
Please report any bugs you find in this thread.

Basic.png Basic_List.png Multi_Choose.png Input_Number.png Progress.png Color_Chooser.png File_Chooser.png

Your Support
Creating libraries and wrappers for existing library projects is a lot of work. This one took an extra amouont of testing and debugging to get all the features running.
The use of this library is totally free and you even don't need to mention in your app that you use it.
But if you use this library in your projects and you think it is useful to you please consider to make a donation:

Thanks very much for your support.

Requirements
  • B4A 6.0 or above
  • Compatible with API version 13 and up (set minSdkVersion to 13)
  • Android Support Repository
  • AppCompat library should be used.
Installation
  • Copy all files from the MaterialDialogsLibx_xx.zip file to your additional libraries folder. There are 6 files. 1 .xml, 1 .jar and 4 .aar files.
Usage
Please study the very detailed example for the usage of the dialogs. It covers most of the features of this library.

Using the CustomView method:
To display a cutom layout for your dialog use the MaterialDialogBuilder.CustomView(WrapInScrollView, Height). This adds a Panel to the dialog. The width of the dialog is defined in the material design guidelines and cannot be changed. The height can be defined freely. Optionally you can wrap your content in a ScrollView so that it is scrollable but this will add some margin to the content.

To add your content (manually or by loading a layout file) you have to use the CustomViewReady event. This event sub receives a Panel object to which your layout can be added. Here a short example for a CustomView:

B4X:
Sub SomeSub
    [...]
    Dim Builder as MaterialDialogBuilder

    Builder.Initialize("CustomViewDialog")
    Builder.CustomView(False, 300dip)
    Builder.PositiveText("Ok").NegativeText("Cancel")
    Builder.Show
    [....]
End Sub

Sub CustomViewDialog_CustomViewReady (Dialog As MaterialDialog, CustomView As Panel)
    'Load your layout here or add it manually to the panel.
    CustomView.LoadLayout("customdialoglayout")
End Sub

Using FileChooser, FolderChooser and ColorChooser dialogs:
These dialogs work a bit different than normal dialogs because the events must be handled directly by the activity.
First step is to extend the Activity from de.amberhome.materialdialogs.MaterialDialogsActivity. Then in Activity_Create initialize the MaterialDialogsManager. This Manager raises the events for the Dialogs. Here is a complete example for the ColorChooserDialog:
B4X:
'Only needed for FileChooser, FolderChooser and ColorChooser dialogs
#Extends: de.amberhome.materialdialogs.MaterialDialogsActivity

Sub Globals
    Private Manager As MaterialDialogsManager
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim primaryColor As Int

    Activity.LoadLayout("layout1")

    Manager.Initialize("DialogManager")

    Dim ac As AppCompat
    primaryColor = ac.GetThemeAttribute("colorPrimary")

    Dim Builder As ColorChooserDialogBuilder
    
    Builder.Initialize
    Builder.PreSelect(primaryColor)
    Builder.Show
End Sub

Sub DialogManager_ColorSelected (Dialog As ColorChooserDialog, Color As Int)
    Log("Color Selected: " & Color)
End Sub



Version History
V0.1
  • First public (beta) release.
V1.0
  • Fix: CustomViews with ScrollView should work better.
  • Change: MaterialDialogs_CustomViewReady event changed its signature!
  • New: Base library updated to 0.9.4.1
 

Attachments

  • MaterialDialogsLib1_0.zip
    305.3 KB · Views: 1,829
  • MaterialDialogsExample1_0.zip
    29.1 KB · Views: 1,631
Last edited:

Mahares

Expert
Licensed User
Longtime User
Hi Markus:
I was looking forward to a Time and a Date Dialogs, but unfortunately I saw neither. Are those in the planning stage?
Thank you
 

corwin42

Expert
Licensed User
Longtime User
Hi Markus:
I was looking forward to a Time and a Date Dialogs, but unfortunately I saw neither. Are those in the planning stage?
Thank you

Just use this one.
 

swabygw

Active Member
Licensed User
Longtime User
Hi - I'm using API 21 for my emulator and B4A 6.80. I added the six files in the libraries folder and opened up the example program but got a few errors. It seems a reference is missing for: ACToolBarDark, ACEditText, ACButton, ACCheckBox. I'm thinking that I've missed a step in the installation - any advice? (updated the B4A version)

EDIT: never mind - I see that it needs the AppCompat library and it worked after installing that.
 
Last edited:

mrushdi

Member
Licensed User
Longtime User
Hi corwin42, i have used your awesome library in my project, unfortunately i didn't implemented it in proper way, the progress dialogue shown at the end not in the beginning of processing, any advice?

Sub Activity_Create(FirstTime As Boolean)


objWFI.JavaDebug = True
objWFI.PingHostOnReceive = True
objWFI.Initialize("objWFI")
LogColor("[isConnectedToInternet]" & objWFI.isConnectedToInternet() ,Colors.Blue)

If objWFI.isConnectedToInternet() = True Then

'***********************************
Mng.Initialize("DialogManager")
Dim Builder As MaterialDialogBuilder
Builder.Initialize("Dialog")
Builder.Title("Progress Dialog")
Builder.Content("Please wait...")
Builder.Progress(True, 0)
Builder.ProgressIndeterminateStyle(True)
Builder.Show
'***********************************


Dim xml As XmlLayoutBuilder
NavDrawer.Initialize2("NavDrawer", Activity, NavDrawer.DefaultDrawerWidth, NavDrawer.GRAVITY_START)
Activity.LoadLayout("layout1")
NavDrawer.InitDrawerToggle
ToolBar.InitMenuListener
Dim bd As BitmapDrawable
bd = xml.GetDrawable("ic_refresh_white_24dp")
Activity.AddMenuItem3("Refresh", "Menu", bd.Bitmap, True)
NavDrawer.NavigationView.LoadLayout( "navheaderLayout.bal", NavDrawer.DefaultHeaderHeight)
NavDrawer.NavigationView.Menu.AddWithGroup2(1, 1, 1, "Home", xml.GetDrawable("ic_home_black_24dp")).Checked = True
Dim actionViewItem As ACMenuItem
actionViewItem = NavDrawer.NavigationView.Menu.AddWithGroup2(1, 2, 2, "A Switch", xml.GetDrawable("ic_android_black_24dp"))
Switch.Initialize("")
actionViewItem.ActionView = Switch
NavDrawer.NavigationView.Menu.SetGroupCheckable(1, True, True)
actionViewItem.Checkable = False
NavDrawer.NavigationView.Menu.AddWithGroup2(2, 10, 1000, "Settings", xml.GetDrawable("ic_settings_black_24dp"))
NavDrawer.NavigationView.Menu.AddWithGroup2(2, 11, 1100, "Feedback", xml.GetDrawable("ic_feedback_black_24dp"))
NavDrawer.NavigationView.Menu.AddWithGroup2(2, 12, 1200, "Help", xml.GetDrawable("ic_help_black_24dp"))
NavDrawer.NavigationView.Menu.SetGroupCheckable(2, True, True)
NavDrawer.AddSecondaryDrawer(150dip, NavDrawer.GRAVITY_END)
NavDrawer.SecondaryNavigationView.LoadLayout("navHeaderLayout2", 100dip)
HeaderLabel.Text = "Fist Header"
NavDrawer.SecondaryNavigationView.LoadLayout("navHeaderLayout2", 50dip)
HeaderLabel.Text = "Second Header"
NavDrawer.SecondaryNavigationView.ItemIconColor = Colors.Red
NavDrawer.SecondaryNavigationView.SetItemTextColors(Colors.Gray, Colors.Red, Colors.Red, Colors.LightGray)
Dim subMenu As ACSubMenu
subMenu = NavDrawer.SecondaryNavigationView.Menu.AddSubMenu(3, 20, 20, "Pages")
'
dbCursor = Starter.dbSQL.ExecQuery("SELECT * FROM " & Starter.DBTblNamebanks)
If FirstTime Then
DisableStrictMode
ALEX
AAIB
MISR
CA
ALBARAKA
CBE
NBE
EG
ABK
BLOM
CAIRE
SC
HDB
ADIB
NBG
End If

If clv1.IsInitialized = False Then
clv1.Initialize(Me, "clv1")
End If
clv1.AsView.Color = Colors.Transparent


headerpnl.Initialize("")
PC.Initialize
Dim p, p1 As Panel
p.Initialize("")
p1.Initialize("")
p.Color = Colors.rgb(234, 234, 224)
Dim pageName, pageName1 As String
pageName = "سعر الدولار حاليا"
pageName1 = "متوسط سعر الذهب"

headerpnl.Color = Colors.rgb(234, 234, 224)
Dim LeftPos As Int
For i = 0 To 2
Dim cd As ColorDrawable
cd.Initialize(Colors.rgb(161, 150, 132), 4%x)
lblG(i).Initialize("")
lblG(i).Background = cd
If Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) >=5 And Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) < 6 Then
TextSize = GetDeviceLayoutValues.ApproximateScreenSize / 5
lblG(i).TextSize = 18 * TextSize
Else If Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) >= 4 And Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) < 5 Then
TextSize = GetDeviceLayoutValues.ApproximateScreenSize / 4
lblG(i).TextSize = 18 * TextSize
Else If Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) >= 3 And Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1) < 4 Then
TextSize = GetDeviceLayoutValues.ApproximateScreenSize / 3
lblG(i).TextSize = 18 * TextSize
End If
' Log(Density)
' Log(GetDeviceLayoutValues)
' Log(Round2(GetDeviceLayoutValues.ApproximateScreenSize * 1.24,1))
' Log(GetDeviceLayoutValues.ApproximateScreenSize )
' Log(GetDeviceLayoutValues.Scale)
lblG(i).TextColor = Colors.Black
lblG(i).Typeface=Typeface.LoadFromAssets("az-topaz.ttf")
lblG(i).Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
Select i
Case 2: lblG(i).Text = "اسم البنك"
Case 1: lblG(i).Text = "سعر البيع"
Case 0: lblG(i).Text = "سعر الشراء"
End Select
lblG(i).Tag = i
headerpnl.AddView(lblG(i), LeftPos + 3 * DividerWidth , 0.5%y, Widths(i) - DividerWidth, 6%y)
LeftPos = LeftPos + Widths(i)
Next

p.AddView(headerpnl,1%x,2dip,98%x,7%y)
p.AddView(clv1.AsView, 1%x, 8%y, 98%X, 70%y)
PC.AddPage(p, pageName)
PC.AddPage(p1, pageName1)
subMenu.AddWithGroup2(3, 100+0, 100+0, pageName, xml.GetDrawable("ic_bookmark_black_24dp"))
subMenu.AddWithGroup2(3, 100+1, 100+1, pageName1, xml.GetDrawable("ic_bookmark_black_24dp"))
NavDrawer.SecondaryNavigationView.CheckedItem = 100
subMenu.SetGroupCheckable(3, True, True)
VP.PageContainer = PC
Dim Ac As AppCompat
TabLayout.Color = Ac.GetThemeAttribute("colorPrimary")

TabLayout.SetViewPager(VP)
For i = 0 To dbCursor.RowCount - 1
dbCursor.Position = i
clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 8%y), 9%y, $"Item #${i}"$)
Next

Else

ToastMessageShow("من فضلك تأكد من انك متصل بالانترنت لتحديث الاسعار" , True)
End If

End Sub
 

corwin42

Expert
Licensed User
Longtime User
1. Please use CODE Tags for code.
2. You don't need the dialog manager just for a progress dialog.
3. The progress dialog don't schow because your code will never enter the message loop until the end of ActivityCreate. Split it up and use CallSubDelayed or ExecQueryAsync for asynchronous SQL execution.
 

SandroB4A

Member
Licensed User
Longtime User
Hello,

I'm trying to use the material dialog togheter to Material Preference library, in my app I've a setting key associated to a list, then I want to show this list using a material dialog (Simple List Dialog).

The problem is I don't know how to fill the builder of the dialog with the list values, the syntax of the builder say:
B4X:
Builder.Items(ItemsArray() As CharSequence) As MaterialDialogBuilder

In the dialog example only "Array as String(...)" was used.

Any help is very appreciated, thanks!
 
Last edited:

SandroB4A

Member
Licensed User
Longtime User
Hi DonManfred, tks for the reply!

The builder doesn't accept array of strings (seem to me), I will study more in details the CharSequenceBuilder....
 

ilan

Expert
Licensed User
Longtime User
hi

are the dialogs modal dialogs? is the activity stopped when you show a dialog?

thank you
 
Last edited:

a2dcore

New Member
Licensed User
Good day to all,

Is there a way to detect input change to enable or disable the POSITIVE button in a CustomView dialog?

Thanks in advance. :)
 

ivan.tellez

Active Member
Licensed User
Longtime User
Hi!

What its the correct way to dismiss a Indeterminate progress dialog programmatically?

(The equivalent to regular ProgressDialogHide)
 

ivan.tellez

Active Member
Licensed User
Longtime User
What its the correct way to dismiss a Indeterminate progress dialog programmatically?
Never mind.

Found that MaterialDialog is an instance of android.app.Dialog, so you cannot dismiss a dialog using it's Builder. You can only dismiss a dialog using the dialog itself. So, you had to save the dialog instance to dismiss it.




B4X:
Sub Globals
    Dim mProgress As MaterialDialogBuilder
    Dim mdProgress As MaterialDialog
End Sub

Sub ShowDialog(mContent As String, mTitle As String)
    mProgress.Initialize("mProgress")
    mProgress.Title(mTitle)
    mProgress.Content(mContent)
    mProgress.Progress(True, 0)
    mProgress.ProgressIndeterminateStyle(False)
    mdProgress = mProgress.Show
End Sub

Sub DismissDialog
    If mdProgress.IsShowing Then
        mdProgress.Dismiss
    End If
End Sub
 
Top