1

I am suing a date picker in my project,but I am dont want future dates to be enabled to select:-

I also used the answer in the link,

Disable future dates in Android date picker

but it does not work for me, I am using android studio 2.2.3,and my api is 22.

public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);

    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        ddate.setText(day + "/" + (month + 1) + "/" + year);





    }

    }
vithika
  • 213
  • 1
  • 5
  • 16

1 Answers1

2

I guess you added that sample code of "maxDateRange" in your code but didn't call it. Just replace your code given above with this:

public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}

public class DatePickerFragment extends DialogFragment implements
    DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog dialog = new DatePickerDialog(getActivity(), mDateSetListener, cyear, cmonth, cday);
    dialog.getDatePicker().setMaxDate(new Date().getTime());
    return dialog;


}

public void onDateSet(DatePicker view, int year, int month, int day) {

    ddate.setText(day + "/" + (month + 1) + "/" + year);





}

} 

Hope it will work!

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11