Android DatePicker : Previous dates are not disabled on my phone after installation but works PERFECTLY on the emulator

StackOverflow https://stackoverflow.com/questions/22523734

  •  17-06-2023
  •  | 
  •  

Pregunta

I'm using a DatePicker with a Button to select from theDialog and then display it on an EditText. Also i'm disabling the previous dates based on the current date.

So i have this onClick method in my app :

@Override
public void onClick(View v) {

    if (v == btnCalendar) {

        // Process to get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // Launch Date Picker Dialog
        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        // Display Selected date in textbox

                         if (year < mYear)
                                view.updateDate(mYear,mMonth,mDay);

                            if (monthOfYear < mMonth && year == mYear)
                                view.updateDate(mYear,mMonth,mDay);

                            if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
                                view.updateDate(mYear,mMonth,mDay);

                        txtDate.setText(dayOfMonth + "-"
                                + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        dpd.show();
    }   }

My question is, it works PERFECTLY on the emulator but when i install it on my phone, it doesn't work. I mean the previous dates are not disabled. It is selecting which shouldn't happen right? How can i fix this? I'm using Android Jelly Bean 4.4.

Thanks in advance.

¿Fue útil?

Solución

Before dpd.show() try dpd.getDatePicker().setMinDate(your date here). You can also set a max date if that's one of your requirements.

Hope this helps

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top