Limiting Future Dates on Android Studio Date Picker

  • Thread starter Thread starter fireflies
  • Start date Start date
  • Tags Tags
    Android
AI Thread Summary
The discussion centers on implementing a date picker in an Android app using Android Studio. The code provided successfully initializes a date picker that restricts users to selecting dates only up to the current date, December 5, 2017. However, a problem arises when the date picker shows a blank space for the year 2018, as no future dates can be selected. The user seeks a solution to make future months and days visible but not selectable, while also ensuring that selecting a date rounds to the next day, such as displaying January 6, 2017, after selecting January 1. Suggestions include using the max and min date attributes to define limits, but concerns are raised about the visual presentation of the widget. The conversation emphasizes the need for a balance between functionality and user interface aesthetics, with a focus on allowing past date selection without cluttering the widget with unselectable future dates.
fireflies
Messages
210
Reaction score
12
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 :)
 
Technology news 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.
 
Did you try Setting the max and min date attributes? They apparently define the date limits for the widget.
 
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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top