I want to add a upper limit to my date picker (spinner). The max date should be "today". I tried to add this line of code but then my app crashes:
dpDate.setMaxDate(new Date().getTime());
Here's the whole code, can anybody see what's wrong? :)
addToCal.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(Activity4.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_add_call,null);
final DatePicker dpDate = (DatePicker)findViewById(R.id.dpDate);
dpDate.setMaxDate(new Date().getTime()); //this makes the app crash
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
You must add setMaxDate
on onCreateDialog
method like below
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.DialogTheme,null,year,month,day);
//set max date
dialog.getDatePicker().setMaxDate(new Date().getTime());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.