0

In registration, the user must indicate his birthday. So he should choose a date from a DatePicker. Is it possible to not showing the dates > actual date in the DatePicker ?

private void showDatePicker()
{
    DatePickerFragment date = new DatePickerFragment();

    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);

    date.setCallBack(ondate);
    date.show(getActivity().getSupportFragmentManager(), "Date Picker");
}

2 Answers2

1

You can check this with condition using Date class's after / before method. Check below Ex:

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);

    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
                cday);
    }
    return null;
}
private final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    // onDateSet method
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        Date currentDate = new Date();

        Date date = new Date(year - 1900, monthOfYear, dayOfMonth);

        if(currentDate.after(date)){
            Log.d("System out", "True: Current date: "+currentDate +"  Selected date: "+date);
        }else{
            Toast.makeText(getApplicationContext(), "kindly select a valid date", Toast.LENGTH_LONG).show();
            showDialog(DATE_DIALOG_ID);
        }
    }
};
UrMi
  • 878
  • 1
  • 6
  • 10
0

Use setMaxDate() of DatePicker.

You don't show what your DatePickerFragment is but if it's some kind of wrapper for DatePickerDialog, you can access the DatePicker with getDatePicker() (requires API level 11).

laalto
  • 144,748
  • 64
  • 275
  • 293