Search code examples
androiddatepickerreset

Android DatePicker: How to reset DatePicker to current date automatically after the user selects the date?


Hello StackOverFlow Community,

I have created an app with two DatePickers in one activity. I need for the DatePicker to reset to the current date after the user chooses the date with it. Next, I have a clear button in the activity. I need that to also reset the DatePicker to the current date onClick of that button. Can anyone help me to accomplish this?

I really appreciate your time. Please, if you are going to give me a minus point for this question, can you explain why? I am trying to get better and that is why I am here. Thanks again.


Solution

  • public void onResetClick(){
         resetDateToToday();
    }
    
    public void onDateSelected(){
         resetDateToToday();
    }
    
    public void resetDateToToday(){
         Calendar cal = Calendar.getInstance();
         datePicker.updateDate(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) - 1,cal.get(Calendar.DAY_OF_MONTH));
    }