Reputation: 35
I've 2 DatePicker, the first is the start date and the second is the end date. I'want that both have maxDate the current date (and this works with my code) and i want that when i change start date the minDate of second DatePicker will update to this date (and vice versa). With this listner i handle the action but second DatePicker doesn't refresh its minDate on layout.
private DatePicker.OnDateChangedListener startDataListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
endDate.setMinDate(c.getTimeInMillis());
}
};
private DatePicker.OnDateChangedListener endDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
startDate.setMaxDate(c.getTimeInMillis());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startDate= (DatePicker)findViewById(***);
endDate= (DatePicker)findViewById(***);
startDate.setMaxDate(Calendar.getInstance().getTimeInMillis());
startDate.init(year, month, day, startDateListener);
endDate.setMinDate(c.getTimeInMillis()-1000);
endDate.setMaxDate(c.getTimeInMillis());
endDate.init(year, month, day, endDateListener);
}
Upvotes: 2
Views: 4950
Reputation: 11487
Just initialize the DatePickerDialog
object TWICE
OR MULTIPLE
times, every time you change the setMinDate(milisecs)
.
void yourListenerMethod() {
datePickerDialog2 = new DatePickerDialog(getActivity(), date2, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog2.getDatePicker().setMaxDate(System.currentTimeMillis()-1000);
datePickerDialog2.getDatePicker().setMinDate(date.getTime());
}
Upvotes: 1
Reputation: 1342
It is because method setMinDate() has check
if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)
&& mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR){
return;}
Simply set
mPicker.setMinDate(0);
//The Min date you wish to set.
mPicker.setMinDate(new LocalDate().minusWeeks(2)
.toDateTimeAtStartOfDay().getMillis());
By this the year has been changed for new Min Date and hence it will set the new date. This works for me
Upvotes: 2
Reputation: 391
From https://stackoverflow.com/a/19722636
This happens because of this code of
setMinDate()
has check:if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR) && mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR) return;
Simple workaround is to set min date with different year at first.
Using the suggested solution and avoiding listener execution when the solution is applied I came to this:
DatePicker startDate, endDate;
Calendar minDate = Calendar.getInstance(), maxDate = Calendar.getInstance();
boolean callFromStartDate = false, callFromEndDate = false;
private DatePicker.OnDateChangedListener startDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Avoid listener execution when there's a forced updated on picker
if(callFromEndDate){
callFromEndDate = false;
return;
}
callFromStartDate = true;
// Force picker update
minDate.set(year - 1, monthOfYear, dayOfMonth);
endDate.setMinDate(minDate.getTimeInMillis()); // Calls onDateChange from the other picker
callFromStartDate = true;
// Update the picker to the desired date
minDate.set(year, monthOfYear, dayOfMonth);
endDate.setMinDate(minDate.getTimeInMillis());
}
};
private DatePicker.OnDateChangedListener endDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(callFromStartDate) {
callFromStartDate = false;
return;
}
callFromEndDate = true;
maxDate.set(year + 1, monthOfYear, dayOfMonth);
startDate.setMaxDate(maxDate.getTimeInMillis());
callFromEndDate = true;
maxDate.set(year, monthOfYear, dayOfMonth);
startDate.setMaxDate(maxDate.getTimeInMillis());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDate = (DatePicker) findViewById(R.id.datePicker);
endDate = (DatePicker) findViewById(R.id.datePicker2);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
long initialTimeInMillis = c.getTimeInMillis();
startDate.setMinDate(initialTimeInMillis-2000000000);
startDate.setMaxDate(initialTimeInMillis);
startDate.init(year, month, day, startDateListener);
endDate.setMinDate(initialTimeInMillis-2000000000);
endDate.setMaxDate(initialTimeInMillis);
endDate.init(year, month, day, endDateListener);
}
Upvotes: 2