Help with DateDialog

sarim123

Member
Licensed User
Longtime User
Hello everyone,
How would i make it so when the user presses a button, the DateDialog pops up, and when the user presses ok, the date gets put into a editext.

Im having lots of trouble and cant seem to figure it out:BangHead:
Can anybody help?
 

Dman

Active Member
Licensed User
Longtime User
Here's how I did mine. I don't know if it is the right way but it works.

B4X:
Sub btnDate_Click
   
       'Date dialog code
   Dim Dd As DateDialog


   ' set the date shown when the dialog is openend
         SelectedDateInTicks = DateTime.now
   Dd.DateTicks = SelectedDateInTicks   
   
   'Dd.DateTicks = btnDate.Text
   ret = Dd.Show("Today's date: " & DateTime.Date(Dd.DateTicks), "Date-Chooser", "OK", "", "Cancel", Null)
   If ret = DialogResponse.CANCEL Then Return

   dow = DateTime.GetDayOfWeek(Dd.DateTicks)
   dddate = DateTime.Date(Dd.DateTicks)
   
     dayofweek
   
   btndate.Text = DateTime.date(Dd.DateTicks)
     
End Sub

Sub dayofweek

   Select dow
   Case 1
   downame = "Sun"
   Case 2
   downame = "Mon"
   Case 3
   downame = "Tue"
   Case 4
   downame = "Wed"
   Case 5
   downame = "Thu"
   Case 6
   downame = "Fri"
   Case 7
   downame = "Sat"
   End Select
End Sub
Sub GetTicksFromDialog(td As TimeDialog) As Long
    Return DateTime.TimeParse(NumberFormat(td.Hour, 2, 0) & ":" & NumberFormat(td.Minute, 2, 0) _
        & ":00")
End Sub
 
Upvote 0

Dman

Active Member
Licensed User
Longtime User
I think the only ones you need for that is dialogs and maybe reflection, I'm not sure. I used a few in that project because I did all sorts of different things.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
you forgot to include your Sub Globals and Sub Process_Globals
 
Upvote 0

Dman

Active Member
Licensed User
Longtime User
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim SelectedDateInTicks As Long
   Dim dow As Int
   Dim downame As String
        Dim datenow As String
   Dim now As Long
   Dim dddate As String
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
nope. Didnt work.
The button doesn't do anything
Thanks though.
Heres my project.
If you are looking at this zip, only look on "First0"
 
Upvote 0

Dman

Active Member
Licensed User
Longtime User
I see a button on that screen and when I click it and select a date, it shows up on the button.

And if I change this line

B4X:
 btndate.Text = DateTime.date(Dd.DateTicks)


to this

B4X:
 edtText.Text = DateTime.date(dd.DateTicks)

I see it in the textbox.
 
Last edited:
Upvote 0

sarim123

Member
Licensed User
Longtime User
how would i add the date to text box without deleting the text thats already there.

:sign0098:exactly what i was trying to accomplish but how would i add the date to text box without deleting the text thats already there. Thanks so much DMan!!!
:sign0163:
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
ok, thanks but...

ok 2 small issues. Where would i put the code above? could you put it into the code and post it?

When i hit cancel it puts today date in instead of canceling

Can anyone help me with this?

Thanks DMan.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to change this line:
B4X:
    ret = dd.Show("Today's date: " & DateTime.Date(dd.DateTicks), "Date-Chooser", "OK", "", "Cancel", Null)
to this one !
B4X:
    ret = dd.Show("Today's date: " & DateTime.Date(dd.DateTicks), "Date-Chooser", "OK", "Cancel", "", Null)
For tha date what exactly do you want ?
Is this what you want?
B4X:
btndate.Text = "Date " & DateTime.date(dd.DateTicks)
Best regards.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
didnt work

im sorry klaus but that didnt work. when you put in text into the edittext and select the date, everything in the edittext will be erased except the date. same thing happens when the cancel button is pressed. I attached my code and the project. The code is on the activity called "first0"

B4X:
Sub Process_Globals
    Dim lstText As List
      Dim SelectedItem As Int      : SelectedItem = -1
End Sub

Sub Globals
   Dim edtText As EditText
   Dim ltvTexts As ListView
   Dim btnSubmit, btnDelete As Button
   Dim pnlmenu As Panel
   Dim dd As DateDialog
   Dim datespin As Spinner
   Dim datestr As String
   Dim edittext1 As EditText
   
   Dim SelectedDateInTicks As Long
    Dim dow As Int
    Dim downame As String
        Dim datenow As String
    Dim now As Long
    Dim dddate As String
   Dim btndate As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim i As Int
   
   Activity.LoadLayout("main2.bal")
   ltvTexts.ScrollingBackgroundColor = Colors.Transparent
   ltvTexts.Color = Colors.Transparent
   ltvTexts.Width = 100%x
   pnlmenu.Initialize ("")
   pnlmenu.Color = Colors.Transparent
   ltvTexts.ScrollingBackgroundColor = Colors.Transparent
   
    ltvTexts.SingleLineLayout.Label.TextColor = Colors.Black
   ltvTexts.SingleLineLayout.ItemHeight = 100dip
   ltvTexts.SingleLineLayout.Label.Height = 100dip

   'education-notepad-page.png.Width = 100%x   education-notepad-page.png.Height = 100%y
'   File.Delete(File.DirInternal, "Text.txt") ' just for testing
   If FirstTime = True Then
      If File.Exists(File.DirInternal, "Text.txt") = True Then
         lstText = File.ReadList(File.DirInternal, "Text.txt")
      Else
         lstText.Initialize
      End If
   End If
   FillListView
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   File.WriteList(File.DirInternal, "Text.txt", lstText)
End Sub

Sub btnSubmit_Click
   If edtText.Text <> "" Then
      lstText.InsertAt(0, edtText.Text)
      FillListView
      edtText.Text = ""
   Else
      Msgbox("No entry !", "ERROR")
   End If
End Sub

Sub FillListView
   ltvTexts.Clear
   For i = 0 To lstText.Size - 1
      ltvTexts.AddSingleLine(lstText.Get(i))
   Next
End Sub

Sub btnDelete_Click
   Dim Answ As Int

   If SelectedItem > -1 Then
      Answ = Msgbox2("Do you really want to delete " & lstText.Get(SelectedItem) & " ?", "Delete", "Yes", "", "No", Null)
      If Answ = DialogResponse.POSITIVE Then
         lstText.RemoveAt(SelectedItem)
         ltvTexts.RemoveAt(SelectedItem)
         SelectedItem = -1
         btnDelete.Visible = False
      End If
   Else
      Msgbox("No item selected !", "ERROR")
   End If
End Sub

Sub ltvTexts_ItemClick (Position As Int, Value As Object)
   SelectedItem = Position
   btnDelete.Visible = True
End Sub

'Sub ltvTexts
   'ltvTexts.TextSize = 20
   'ltvTexts.TextColor = Colors.Green
'End Sub

Sub Button1_Click
   Activity.LoadLayout("Main.bal")
End Sub

Sub btnDate_Click
    
        'Date dialog code
    Dim dd As DateDialog


    ' set the date shown when the dialog is openend
            SelectedDateInTicks = DateTime.now
    dd.DateTicks = SelectedDateInTicks    
    
    'Dd.DateTicks = btnDate.Text
    ret = dd.Show("Today's date: " & DateTime.Date(dd.DateTicks), "Date-Chooser", "OK", "Cancel", "", Null)
    If ret = DialogResponse.CANCEL Then Return

    dow = DateTime.GetDayOfWeek(dd.DateTicks)
    dddate = DateTime.Date(dd.DateTicks)
    
     dayofweek
    
   edtText.Text = edtText.Text & " - " & DateTime.date(dd.DateTicks)

   
      
End Sub

Sub dayofweek

    Select dow
    Case 1
    downame = "Sun"
    Case 2
    downame = "Mon"
    Case 3
    downame = "Tue"
    Case 4
    downame = "Wed"
    Case 5
    downame = "Thu"
    Case 6
    downame = "Fri"
    Case 7
    downame = "Sat"
    End Select
End Sub
Sub GetTicksFromDialog(td As TimeDialog) As Long
    Return DateTime.TimeParse(NumberFormat(td.Hour, 2, 0) & ":" & NumberFormat(td.Minute, 2, 0) _
        & ":00")
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, but I don't understand how you are working.
In the project in your last post there is no btnDate_Click routine in the first0 Activity !?
I reused the datenotworking from your post #8.
And it WORKS.
You need to be careful because when you click a second one onto btnDate and select a date this one will be added in edtText to the previous one.

Best regards.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
Thanks! klaus!!!!!

Exactly what i was trying to accomplish. thanks soo much! ya, i should have used the code from post 8.

thank you guys.:sign0098::icon_clap:
 
Upvote 0
Top