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");
}
  • then why you want to insert it.?? and if you are not showing it then how user know that he/she enter correct birthday??? – Sagar Maiyad Dec 11 '13 at 09:08
  • User must see only dates < actual date in DatePicker. This is my question, it is possible to do it please ? –  Dec 11 '13 at 09:10
  • i cant get you.. explain proper – Sagar Maiyad Dec 11 '13 at 09:12
  • In DatePicker, the user can see only the dates < actual date. For example, today is 11/12/2013. This is the Max. So, user see only date < than 11/12/2013 in DatePicker. –  Dec 11 '13 at 09:18
  • http://stackoverflow.com/questions/6116920/disable-future-dates-in-android-date-picker – Sagar Maiyad Dec 11 '13 at 09:23

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
  • 150,114
  • 66
  • 286
  • 303