Android date popup, range - android

I have a date picker that allows a user to select a date, by default this lets you change the year higher and higher to infinite or something, but I want to confine the user to a maximum year. Such as only 2 years from now.
new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay)
How do I set the max date within that function? I'm sure I can do a "if date > 2014 , then show a toast message and show the datepicker again until they select a proper date" but I would prefer to just not let the date picker function go above a certain year.
Insight appreciated!

Add a OnDateChangedListener to DatePicker Object and then override the
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
//Do the check here for the year
}
Hope this helps

Create a datepicker and add that view to your dialog layout, If you want any custom behaviour then you need a custom date picker, override the onDateChanged() to behave as per your need.
If just you need is to restrict the maximum date of the date picker, use setMaxDate(long maxDate) for the datepicker.
you can use getDatePicker() on the dialog to get the instance of the it inside the dialog.
refer http://developer.android.com/reference/android/app/DatePickerDialog.html
and http://developer.android.com/reference/android/widget/DatePicker.html
for more info.

Related

Date is shown twice if setMaxDate is used on DatePickerDialog

When setting the maximum date on a date picker dialog like this:
DatePickerDialog dialog = new DatePickerDialog(this, listener, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
The date is shown normally, but also a second time at the top of the dialog. I've seen that many people have encountered this bug but I didn't find a solution.
You can also see it on this image from another user
Is there any way to prevent or solve this, besides using reflection?
I solved the bug by calling dialog.setTitle(null) every time a maximum date is set.
I posted this issue here: https://issuetracker.google.com/72284741

change date format in datepicker dialog android

I have edit text field and I have a method that handles onclick event on edit field and show up a date picker dialog. However I want to change date format in the dialog . It shows Aug 31 2014, I like to show 31 08 2014. Is it possible?
This question has been answered elsewhere on the site, here for example. Basically the datepicker dialog shows up in the preferred format of the end user, so if they have their phone or emulator set to mm/dd/yyyy it will show up like that, if you want it to come up dd/mm/yyyy while you are testing it make sure your emulator is set up with that as the preferred date format.
In DatePickerDialog.OnDateSetListener you will get onDateSet method where you can get
int year = selectedYear;
int month = selectedMonth;
int day = selectedDay;
dateCalendar.set(Calendar.YEAR, year);
dateCalendar.set(Calendar.MONTH, month);
dateCalendar.set(Calendar.DAY_OF_MONTH, day);
String monthname = new SimpleDateFormat("MMM").format(dateCalendar.getTime());
in SimpleDateFormat you can change date format as you want

How to set a date in DatePicker

I want DatePicker shows a specific date when opened, unfortunately seems I can set only the date limits with setMinDate and setMaxDate.
Is there a way to set a date without block the selection with date limits?
DatePicker date = new DatePicker(context);
date.init(year, month, day, null);
where year, month and day are ints. Here is an explanation about init

Android DatePickerDialog to a year of history

I'm coding an android app about the egyptian history, and I want to add a DatePicker in a dialog, where I will show years, and whenever the user sets a year, it shows in the layout the events in that year . If he changes the year, the event changes ...etc
It is a historical app, and I don't know how to do it!
I need help.
According to the doc (http://developer.android.com/reference/android/widget/DatePicker.html)
You can set min and max date according to the era you want to cover:
android:maxDate The minimal date shown by this calendar view in mm/dd/yyyy format.
android:minDate The minimal date shown by this calendar view in mm/dd/yyyy format.
And then use the onDateChangedListener to display the correct Layout.
http://developer.android.com/reference/android/widget/DatePicker.OnDateChangedListener.html
As you want to use only a year, I would advise to use a NumberPickerDialog instead.
DatePicker is maybe "too much" for your use case.

Fixing a certain year in datepicker in android

I want to fix a certain range of year in datepicker. I only want that range of the year.
I only have the DatePickerDialog object and a OnDateSetListener object, how do I incorporate a datepicker into this arrangement?
Try getting the DatePicker from the dialog using DatePickerDialog.getDatePicker() and then using DatePicker.setMaxDate() and DatePicker.setMinDate()

Categories

Resources