Android Question How to limit datepicker to only past days?

Massy

Member
Licensed User
Longtime User
Hello, in my app the user has to choose a date and I'm using AnotherDatePicker to do that... I want to know: is there a way to limit the choose to only the past days? Let's say I don't want the user to get a date that still has to come... for example today is the first of september and I want that user can not click on 2 or 3 or 4 of september... and so on...
Thanks for any help
Massy
 

mangojack

Expert
Licensed User
Longtime User
This might be of interest. See HERE ...

you could use something like this and show a toast if date picked is in the future .. (not tested)
B4X:
Sub ADP_Closed (Cancelled As Boolean, Date As Long)
Dim Today As Long = DateTime.DateParse(DateTime.Date(DateTime.Now))
Date = DateTime.DateParse(DateTime.Date(Date))
If Cancelled = False AND Date > Today Then
'...........................
 
Upvote 0

Massy

Member
Licensed User
Longtime User
This might be of interest. See HERE ...

you could use something like this and show a toast if date picked is in the future .. (not tested)
Hey thanks a lot, it worked, but now I've got another problem... if Date is > Today... I show a message saying it can not be,.... and that's ok but then my ADP remain with the date inserted... and I don't know how to delete the input... it doesn't work as a label... do you know how to do it?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
another way .. Remove the above code from ADP_Closed sub then ...

in the AnotherDatePicker class edit /change the following sub.
B4X:
Private Sub SelectDay(day As Int, UpdateLabel As Boolean)
   selectedDate = DateUtils.SetDate(year, month, day)
   selectedDay = day
   selectedMonth = month
   selectedYear = year

   Dim Today As Long = DateTime.DateParse(DateTime.Date(DateTime.Now))
   selectedDate = DateTime.DateParse(DateTime.Date(selectedDate))
   If selectedDate > Today Then
     SetDate(DateTime.Now, False)  '## reset ADP to current date
     targetLabel.Text = ""         '## this covers user picking good date.. then changing to bad date
     ToastMessageShow("Bad Date ...",False)
   Else
     If UpdateLabel Then targetLabel.Text = DateTime.Date(selectedDate)
   End If

End Sub

p.s. Having never used the ADP I'm unsure if this is the best way for you app. personally I would not hide the ADP until the user chooses a valid date .
 
Last edited:
Upvote 0

Massy

Member
Licensed User
Longtime User
personally I would not hide the ADP until the user chooses a valid date .
Thanks a lot for your inputs... I'm kinda happy with what I got now... it would be perfect if instead of closing the calendar it would let it open until the user choose a valid date... just as you said... but I repeat... I'm already happy like that... so... thanks again!
Massy
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…