0

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();

        }
    });

1 Answers1

0

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());
    }
Vinay Hegde
  • 1,424
  • 1
  • 10
  • 23