Android Question Cycling through dates

Jay Young

Member
Licensed User
Longtime User
is it possible to cycle through dates with a NEXT and PREVIOUS button

ie: I have a date of 03/01/2016 and i push the NEXT button and get 03/02/2016 or have a PREVIOUS button press it then get 02/29/2016


Thanks for any advise on how to accomplish this

Jay
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Upvote 0

Jay Young

Member
Licensed User
Longtime User
Just in a label and update the text of the label on the next or previous buttons
 

Attachments

  • Screenshot_2016-03-01-15-22-21.png
    121.2 KB · Views: 253
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can use the datetime.add() function. An example follows:
B4X:
Sub Globals
   Dim lblDate As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   lblDate.Initialize("")
   lblDate.Text=DateTime.Date(DateTime.Now)
   Dim btnNext As Button,btnPrevious As Button
   btnNext.Initialize("changeDate")
   btnNext.Text="Next"
   btnPrevious.Initialize("changeDate")
   btnPrevious.Text="Previous"
   Activity.AddView(lblDate,0,0,60%x,15%y)
   Activity.AddView(btnPrevious,0,20%y,30%x,15%y)
   Activity.AddView(btnNext,30%x,20%y,30%x,15%y)
End Sub


Sub changeDate_click
   Dim stepDate As Int
   Dim tempBtn As Button=Sender
   If tempBtn.Text="Next" Then
     stepDate=1
   Else
     stepDate=-1
   End If
   Dim tempDateOfLabel As Long=DateTime.DateParse(lblDate.Text)
   tempDateOfLabel=DateTime.Add(tempDateOfLabel,0,0,stepDate)
   lblDate.Text=DateTime.Date(tempDateOfLabel)
End Sub
 
Upvote 0

Jay Young

Member
Licensed User
Longtime User
Thank you I used the changeDate code you provided, and called it from my btnPrev that cycles through the names of the days of week too, probably and easier way to do it but this is what came up with.

B4X:
Sub btnPrev_Click
    Dim prevDate As String
    Dim totalNoDays As String

    If MasterDay = 0 Then
            t = DateTime.GetDayOfWeek(DateTime.Now)
            totalNoDays = DateTime.GetDayOfYear(DateTime.Now)    
            t = t - 1
           
            For i = 1 To 7
                If i = t Then
                daymark = t
                    Select daymark
                        Case 1
                            lblWeekDay.Text = "Sunday"
                        Case 2
                            lblWeekDay.Text = "Monday"     
                        Case 3
                            lblWeekDay.Text = "Tuesday"
                        Case 4
                            lblWeekDay.Text = "Wednesday"
                        Case 5
                            lblWeekDay.Text = "Thursday"
                        Case 6
                            lblWeekDay.Text = "Friday"
                        Case 7
                            lblWeekDay.Text = "Saturday"               
                    End Select
                    MasterDay = t
                    changeDate
'                    Main.MyDay1 = Main.MyDay1 - 1
'                    DateMinus = DateUtils.SetDate(Main.MyYear, Main.MyMonth, Main.MyDay1)
                    Exit
                End If   
           
            Next
    Else
   
            t = MasterDay
            t = t - 1
            If lblWeekDay.Text = "Sunday" Then
               t = 7
            End If      
               
            For i = 1 To 7
                If i = t Then
                daymark = t
                    Select daymark
                        Case 1
                            lblWeekDay.Text = "Sunday"
                        Case 2
                            lblWeekDay.Text = "Monday"     
                        Case 3
                            lblWeekDay.Text = "Tuesday"
                        Case 4
                            lblWeekDay.Text = "Wednesday"
                        Case 5
                            lblWeekDay.Text = "Thursday"
                        Case 6
                            lblWeekDay.Text = "Friday"
                        Case 7
                            lblWeekDay.Text = "Saturday"               
                    End Select
                    MasterDay = t
                    changeDate
'                    DateMinus = Main.TodaysDate - 1
                    Exit
                End If   
           
            Next       
    End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Private currentDate As Long
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     currentDate = DateTime.Now
   End If
   Activity.LoadLayout("1")
   ChangeDays(0)
End Sub

Private Sub btnNext_Click
   ChangeDays(1)
End Sub

Private Sub btnPrev_Click
   ChangeDays(-1)
End Sub

Private Sub ChangeDays(delta As Int)
   Dim p As Period
   p.Days = delta
   currentDate = DateUtils.AddPeriod(currentDate, p)
   Activity.Title = DateUtils.GetDayOfWeekName(currentDate)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…