iOS Question Date picker snippet

luke2012

Well-Known Member
Licensed User
Longtime User
Hi All,
Is there a snippet for B4i to show a date picker (like the B4A snippet belove) ?

B4X:
'B4A code
Dim dd As DateDialog
dd.DateTicks = DateTime.Now
Dim sf As Object = dd.ShowAsync("", "Scegli una data", "CONFERMA", "", "", Null, False)
Wait For (sf) Dialog_Result(Result As Int)
If Result = DialogResponse.POSITIVE Then
DateTime.DateFormat = "dd/MM/yyyy"
Private sDate As String = DateTime.Date(dd.DateTicks)
Log(DateTime.Date(dd.DateTicks))
End If
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2017-09-20_10.08.54.png


Create a layout file named DatePicker with this layout:
SS-2017-09-20_10.09.18.png


B4X:
Sub Button1_Click
   Dim cd As CustomLayoutDialog
   Dim p As Panel
   p.Initialize("")
   p.SetLayoutAnimated(0, 1, 0, 0, Page1.RootPanel.Width - 40dip, 170dip)
   p.LoadLayout("DatePicker")
   cd.Initialize(p)
   Wait For (cd.ShowAsync("Choose date", "Ok", "Cancel", "", False)) Dialog_Result(Result As Int)
   If Result = cd.RESULT_POSITIVE Then
     Dim dp As DatePicker = p.GetView(0) 'DatePicker is the first view in the layout
     Log($"Selected date: $Date{dp.Ticks}"$)
   End If
End Sub
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
SS-2017-09-20_10.08.54.png


Create a layout file named DatePicker with this layout:
SS-2017-09-20_10.09.18.png


B4X:
Sub Button1_Click
   Dim cd As CustomLayoutDialog
   Dim p As Panel
   p.Initialize("")
   p.SetLayoutAnimated(0, 1, 0, 0, Page1.RootPanel.Width - 40dip, 170dip)
   p.LoadLayout("DatePicker")
   cd.Initialize(p)
   Wait For (cd.ShowAsync("Choose date", "Ok", "Cancel", "", False)) Dialog_Result(Result As Int)
   If Result = cd.RESULT_POSITIVE Then
     Dim dp As DatePicker = p.GetView(0) 'DatePicker is the first view in the layout
     Log($"Selected date: $Date{dp.Ticks}"$)
   End If
End Sub

Thanks @Erel.
Smart way to build a picker and also look pretty :)
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Forgot to write that it is based on the relatively new iCustomDialog library: https://www.b4x.com/android/forum/threads/custom-dialogs-with-icustomdialog-library.83526/#content (added in B4i v4.30).

I just updated B4i to v4.30 version :)

@Erel,
I need to show the dialog without a button.
After I created the DatePicker layout (as you told), can I put the code within the Application_Start ?

B4X:
Dim cd AsCustomLayoutDialog
Dim p As Panel
p.Initialize("")
p.SetLayoutAnimated(0, 1, 0, 0, Page1.RootPanel.Width - 40dip, 170dip)
p.LoadLayout("DatePicker")
cd.Initialize(p)
Wait For (cd.ShowAsync("Data di acquisto...", "CONFERMA", "", "", False)) Dialog_Result(Result As Int)
If Result = cd.RESULT_POSITIVE Then
Dim dp AsDatePicker = p.GetView(0) 'DatePicker is the first view in the layout
Log($"Selected date: $Date{dp.Ticks}"$)
EndIf

I try this but the dialog doesn't resize (within layout I set hor. ancors to both)
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Add Sleep(100) before you call it.

Erel I confirm that the calendar works fine (display successfully) after run a sleep(100) before you call it.
I'm trying to integrate this calendar within my app but I have some problems to handle the program flow as I wish.
The problem is that the program flow continue without wait for the date picker result (see the code belove).


B4X:
'Code module "DatePickerModule"
Sub Process_Globals
Private pg As Page
End Sub

Public Sub Show

If pg.IsInitialized = False Then
pg.Initialize("pg")
pg.RootPanel.LoadLayout("DatePicker")
End If

Main.NavControl.ShowPage(pg)
Sleep(100)
Dim cd As CustomLayoutDialog
Dim p As Panel
p.Initialize("")
p.SetLayoutAnimated(0, 1, 0, 0, pg.RootPanel.Width - 40dip, 170dip)
p.LoadLayout("DatePicker")
cd.Initialize(p)
Wait For (cd.ShowAsync("Data acquisto caldaia...", "CONFERMA", "", "", False)) Dialog_Result(Result As Int)
If Result = cd.RESULT_POSITIVE Then
Dim dp As DatePicker = p.GetView(0) 'DatePicker is the first view in the layout
'Log($"Selected date: $Date{dp.Ticks}"$)
Dim df As String = DateTime.DateFormat
DateTime.DateFormat = "dd/MM/yyyy"
Dim res As String = DateTime.Date(dp.Ticks)
DateTime.DateFormat = df
Log ("Date = " & res)
Main.SelectedDate = res
End If
End Sub

'Main Module

Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Public SelectedDate As String
End Sub

Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
ProdRegistration
End Sub

Public Sub ProdRegistration
Dim j AsHttpJob
j.Initialize("j", Me)
j.PostString(WSUrl1, "")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Private WSResponse As String = j.GetString2("UTF8")
DatePickerModule.Show '>>> here is where I want to show the date picker and wait for the user response to get date
Private sDate As String = SelectedDate
End If

Dim j1 As HttpJob : j1.Initialize("j1", Me)
j1.PostString(WSUrl, WSParams)
Wait For (j1) JobDone(j1 As HttpJob)
If j1.Success Then
'........................

End Sub
 
Last edited:
Upvote 0
Top