Search code examples
androiddateandroid-datepicker

How to disable the dates after current date?


Help to disable all the dates from the current date onwards in date picker.
This is my code:

public class MyDatePickerDialogue extends DatePickerDialog{
    private CharSequence title;
    private Context context;

    public MyDatePickerDialogue(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {

        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.context = context;
    }

    public void setPermanentTitle(CharSequence title) {
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        setTitle(title);
        Toast.makeText( context,"Date picked", Toast.LENGTH_SHORT).show();
        SingletonClass.getSingletonClass().singletondate = year+"-"+(month+1)+"-"+day;
        //save the date that is picked into the singleton class
        //i represents year , i1 represents month and i2  represents day

        context.startActivity(new Intent(context,DescriptionActivity.class));

    }
}

Solution

  • you can set maxDate for DatePicker view

        Date today = new Date();
        Calendar c = Calendar.getInstance(TimeZone.getDefault());
        c.setTime(today);
        maxDate = c.getTime().getTime();
        view.setMaxDate(maxDate);