Limiting Future Dates on Android Studio Date Picker

  • Thread starter fireflies
  • Start date
  • Tags
    Android
In summary, the conversation discusses the implementation of a date picker on an app being developed on Android Studio. The code limits the date picking to today's date and does not allow for future dates, which may cause issues when the current date is in the later months of the year. The solution offered is to relax the condition and allow for future dates, but still adapt the code to restrict when necessary. Additionally, setting the max and min date attributes can define the date limits for the widget.
  • #1
fireflies
210
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
  • #2
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.
 
  • #3
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.
 
  • #4
Did you try Setting the max and min date attributes? They apparently define the date limits for the widget.
 
  • #5
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?
 

1. What is a Date Picker on Android Studio?

A Date Picker on Android Studio is a user interface element that allows users to select a date from a calendar view. It is commonly used in applications that require users to input dates, such as booking or scheduling apps.

2. How do I add a Date Picker to my Android Studio project?

To add a Date Picker to your Android Studio project, you can use the DatePicker widget provided by the Android framework. You can add it to your layout file using XML code or programmatically in your Java code. There are also many third-party libraries available that offer more customization options for Date Pickers.

3. Can I customize the appearance of the Date Picker?

Yes, you can customize the appearance of the Date Picker to some extent. The Android framework provides attributes that allow you to change the text color, background color, and date format. However, if you need more customization options, you can use third-party libraries or create a custom Date Picker by extending the DatePickerDialog class.

4. How do I retrieve the selected date from the Date Picker?

You can retrieve the selected date from the Date Picker by setting an OnDateChangedListener and implementing the onDateChanged() method. This method will be called every time the user selects a new date, and you can get the selected date from the DatePicker object passed as a parameter.

5. Can I use a Date Picker for date validation?

Yes, you can use a Date Picker for date validation. By setting a minimum and maximum date, you can restrict the user from selecting a date outside of the specified range. You can also use the onDateChanged() method to validate the selected date and show an error message if it does not meet your criteria.

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
Replies
10
Views
2K
Replies
42
Views
6K
Back
Top