Limiting Future Dates on Android Studio Date Picker

  • Thread starter Thread starter fireflies
  • Start date Start date
  • Tags Tags
    Android
Click For Summary

Discussion Overview

The discussion revolves around implementing a date picker in an Android Studio app, specifically focusing on limiting the selectable dates to today and addressing issues related to the user interface when future dates are not pickable. Participants explore various approaches to enhance the functionality and appearance of the date picker.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of a date picker that restricts selection to today's date and expresses a desire for future dates to be visually present but not selectable.
  • Another participant suggests relaxing the restriction on future dates, arguing that adapting the code to the widget's features might be more beneficial.
  • A different participant emphasizes that future dates are unnecessary for the app's purpose, as it focuses on past events, indicating that allowing future dates would complicate the internal logic.
  • One participant recommends setting the max and min date attributes to define the date limits for the widget.
  • The original poster acknowledges the ability to limit dates but expresses concern about the visual presentation of the widget when future dates are not selectable.

Areas of Agreement / Disagreement

Participants express differing views on whether to allow future dates, with some advocating for flexibility and others insisting on the necessity of restricting to past dates. The discussion remains unresolved regarding the best approach to implement the desired functionality while maintaining a user-friendly interface.

Contextual Notes

Participants have not reached a consensus on how to visually represent future dates while keeping them unselectable. There are also unresolved considerations regarding the internal logic required if future dates were to be allowed.

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 :)
 
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?
 

Similar threads

Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
4
Views
3K
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
10
Views
5K
Replies
42
Views
8K