-4

I want to fix the month and year in datePickerDialog but want the user to pick date on his own. Is it possible? ex: DatePickerDialog will be fixed with month May and year 2015, user can now only change the dates between 1 to 31

Anupriya
  • 1,663
  • 3
  • 17
  • 28

3 Answers3

1

I solved my problem in the following way:

 Calendar cal = Calendar.getInstance();
                    DatePickerDialog dialog = new DatePickerDialog(this, myDateSetListener, year, month,
                            day);
                    cal.set(Calendar.MONTH, userMonthValue);

                    cal.set(Calendar.YEAR, userYearValue);

                    cal.set(Calendar.DAY_OF_MONTH, 1);
                    dialog.getDatePicker().setMinDate(cal.getTimeInMillis() - 1000);

                    // code to set max date
                    cal = Calendar.getInstance();

                    cal.set(Calendar.MONTH, userMonthValue);

                    cal.set(Calendar.YEAR, userYearValue);
                    cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
                    dialog.getDatePicker().setMaxDate(cal.getTimeInMillis() - 1000);

Now my date picker dialog is fixed for month and year,,,and user is able to change only the dates between 1 and 30 or 31

Anupriya
  • 1,663
  • 3
  • 17
  • 28
0

You can use below lines of code

 public class First extends Activity {
DatePicker _date;
Date date_current;
Date date_future;
int days_count=0;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
    Calendar ci = Calendar.getInstance();
    String CiDateTime = ci.get(Calendar.YEAR) + "-" + 
            (ci.get(Calendar.MONTH) + 1) + "-" + 
            ci.get(Calendar.DAY_OF_MONTH);

    SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");


    try {
        date_current =  f.parse(CiDateTime);
        long milliseconds = date_current.getTime();
        _date.setMinDate(milliseconds);//here is _date is DatePicker Object

        int monthMaxDays = ci.getActualMaximum(Calendar.DAY_OF_MONTH); 
        if(monthMaxDays>ci.get(Calendar.DAY_OF_MONTH))
        {
            days_count = monthMaxDays-ci.get(Calendar.DAY_OF_MONTH);
            String CiDateMax = ci.get(Calendar.YEAR) + "-" + 
                    (ci.get(Calendar.MONTH) + 1) + "-" + 
                    ci.get(Calendar.DAY_OF_MONTH)+days_count;
            date_future =  f.parse(CiDateMax);
            long milliseconds_MAx = date_future.getTime();
            _date.setMaxDate(milliseconds_MAx);
        }
        else
        {
            _date.setMaxDate(milliseconds);
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0

You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using :