How to prevent select passed date in DatePickerDialog (setMinDate not work as expected)

I found DatePicker(Dialog) very confusing…
When setMinDate() is set… the shown calendar really “visually disable” passed dates (make them gray) but these dates are still posible to select! I want to user prevent to do so. Ideal way would be make passed dates non-clickable / unselectable. If it is not possible, than at least catch this event (which one? onDateChanged?, onSelected? …) and toast a message and disable OK button until user selects a valid date.

Unfortunatelly, datepicker(dialog) has no onChangedListener (actually it has but has no setter for it). So the only event I get is on OK button pressed. It is too late.

  • NPE when getting Robolectric ShadowApplication with Volley and Dagger
  • In-App Emoji Keyboard
  • How to Correctly Extend LinearLayout to Create a Custom View
  • Android SkImageDecoder::Factory returned null Error
  • My app icon appears smaller then other
  • Android SDK Version 25 - android.support v7:25 gives “No resource found that matches @color/hint_foreground_material_light” Adobe Creative SDK?
  • Here is my code

        dateTextView = (TextView) view.findViewById(R.id.createOrder_date);
        dateTextView.setText(R.string.createOrder_time_now);
        dateTextView.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                final Calendar calendarNow = Calendar.getInstance();
                final int yearNow = calendarNow.get(Calendar.YEAR);
                final int monthNow = calendarNow.get(Calendar.MONTH);
                final int dayNow = calendarNow.get(Calendar.DAY_OF_MONTH);
    
                DatePickerDialog datePickerDialog = new DatePickerDialog(CreateOrderFragment.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(year, monthOfYear, dayOfMonth);
    
                        if (calendar.before(calendarNow)) {
                            Toast.makeText(CreateOrderFragment.this.getActivity(), getResources().getString(R.string.createOrder_datePicker_selectFutureDate), Toast.LENGTH_SHORT).show();
                            view.updateDate(yearNow, monthNow, dayNow);
                            calendar = calendarNow;
                        }
    
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        dateTextView.setText(sdf.format(calendar.getTime()));
                    }
                }, yearNow, monthNow, dayNow);
    
                datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                datePickerDialog.setTitle(getResources().getString(R.string.createOrder_datePicker_title));
                datePickerDialog.show();
    
            }
        });
    

    enter image description here

    Related posts:

    How to set Internet options for Android emulator?
    Up arrow does not show after calling ActionBarDrawerToggle.setDrawerIndicatorEnabled(false)
    Android System App 101
    Android: Sending a byte array via Http POST
    How to put two buttons on same line in Android
    Publishing my first android application in Android market
  • SwipeRefreshLayout + ViewPager, limit horizontal scroll only?
  • Android Best Way to Detect and Handle User INACTIVITY
  • How do you animate a change in a view's padding?
  • Is there a way to get count of number methods used in a jar file
  • Best option to store username and password in android app
  • How to create Bitmap from grayscaled byte buffer image?
  • 2 Solutions collect form web for “How to prevent select passed date in DatePickerDialog (setMinDate not work as expected)”

    When you receive the date in onDateSent(), just check if it’s earlier than the date you set in setMinDate(). If so, then just show the DatePickerDialog again.

    Code:

    final long tomorrow = System.currentTimeMillis() + DateUtils.MILLIS_IN_DAY;
    
    private void showDatePickerDialog()
    {
        mDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
        {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
    
                //kv If user tries to select date in past (or today)
                if (calendar.getTimeInMillis() < tomorrow)
                {
                    //kv Make them try again
                    showDatePickerDialog();
    
                    Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
                }
                else
                {
                    //success
                }
            }
        }, calendarTwoWeeksInFuture.get(Calendar.YEAR), calendarTwoWeeksInFuture.get(Calendar.MONTH), calendarTwoWeeksInFuture.get(Calendar.DAY_OF_MONTH));
    
        //kv Set minimum date as tomorrow
        mDatePickerDialog.getDatePicker().setMinDate(tomorrow);
    
        mDatePickerDialog.show();
    }
    

    i have been looking for a while and there seem to be no way to do that in new datePickerDialog (5+) you can handle it yourself by rather returning the min/max value from on click or by reopening the dialog, its up to developer now.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.