Limiting Future Dates on Android Studio Date Picker

  • Thread starter Thread starter fireflies
  • Start date Start date
  • Tags Tags
    Android
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
fireflies
Messages
210
Reaction score
13
So, I am making an app on Android Studio. It has a date picker on it. The code is

Java:
mDisplayDate = (TextView) findViewById(R.id.textSelectDate);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        java.util.Calendar cal = java.util.Calendar.getInstance();
        int year = cal.get(java.util.Calendar.YEAR);
        int month = cal.get(java.util.Calendar.MONTH);
        int day = cal.get(java.util.Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(OldMemory.this,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                mDateSetListener,
                year,month,day);
        dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();
    }
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override

    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String Month=NULL;
        Log.d(TAG, "onDateSet: dd/mm/yyyy: "+dayOfMonth+" " +month+", "+year);
        if(month==0)
            Month="January";
        else if(month==1)
            Month="February";
        else if(month==2)
            Month="March";
        else if(month==3)
            Month="April";
        else if(month==4)
            Month="May";
        else if(month==5)
            Month="June";
        else if(month==6)
            Month="July";
        else if(month==7)
            Month="August";
        else if(month==8)
            Month="September";
        else if(month==9)
            Month="October";
        else if(month==10)
            Month="November";
        else if(month==11)
            Month="December";
        Date = dayOfMonth+" " +Month+", "+year;
        mDisplayDate.setText(Date);
    }
};

It works well. But as you can see I limited the date picking upto today's date. That is, no future date can be picked. But here arises the problem... that is today's date is December 05, 2017... so, the date picker starts here, since 2018 is not pickable so that place is blank, but there is Jan in months field, and 01 in day's field. That is not pickable. how to make them pickable? So, that the date gets rounded to initial year if I pick any after date? And I want it shows 06 after 5, not 01. like if I select next one, the date should be rounded to 06 January 2017... (Since I am making this app this year, I am creating the starting date from 01 January, 2017- though not coded that part still)...

Any help/link is appreciated :)
 
on Phys.org
Why not relax the condition and allow future dates?

In general, adapt your code to the features of the widget and restrict only when necessary.
 
It is because future date won't be needed here... the person will only search things of past. future date will need additional internal logic.
 
yes, I could limit. But the limit doesn't look good on the widget. How can I do that the future dates will be shown but not pickable?