0

I want to let the user pick the date after today, as a remind date, how can I do it?Also, how can I put the theme inside these codes,because there is no button for the user to choose.

Here is my code combine with datapickerdialog and timepickerdialog:

private void setDateTimeField (){

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, new 
DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDate;

            timePicker();
            date_time = year + "-" + (month + 1) + "-" + day;
        }
    },year, month, day);

    final Calendar calendar = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.MONTH, mm);
    cal.set(Calendar.DAY_OF_MONTH, dd);
    cal.set(Calendar.YEAR, yy);

    datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());

    datePickerDialog.show();
  }
private void timePicker(){
    // Get Current Time
    final Calendar c = Calendar.getInstance();
    hours = c.get(Calendar.HOUR_OF_DAY);
    minutes = c.get(Calendar.MINUTE);
    // Launch Time Picker Dialog
     TimePickerDialog timePickerDialog = new TimePickerDialog(this,new 
TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int 
minute) {
                    hours = hourOfDay;
                    minutes = minute;
                    modifytime.setText(date_time+" "+hourOfDay + ":" + 
minute);
                }

            }, hours, minutes, false);
    timePickerDialog.show();
}
Mark
  • 39
  • 8

1 Answers1

1

If you like to set the current date as minimum date and no past days should be selectable, then below solution will works I believe

Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
//Min date setting part
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy);
//Replace your code mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); with the below line
mDatePickerDialog.setMinDate(cal.getTimeInMillis());

Since you are allowing the user to select only future dates, you should not need to set Max date.

Complete Code sample for your reference -

private void showDateDailog() {

    final DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

        year = selectedYear;
        month = selectedMonth;
        day = selectedDate;

        addtime.setText(new StringBuilder().append(year).append("/")
                .append(month + 1).append("/").append(day));

        }
    }, year, month, day);

    final Calendar calendar = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    //Min date setting part
    cal.set(Calendar.MONTH, mm);
    cal.set(Calendar.DAY_OF_MONTH, dd);
    cal.set(Calendar.YEAR, yy);
    datePickerDialog.setMinDate(cal.getTimeInMillis());
    //Maximum date setting part, if you need (Else don't add it)
    /*Calendar calen = Calendar.getInstance();
    calen.set(Calendar.MONTH, mm);
    calen.set(Calendar.DAY_OF_MONTH, dd);
    calen.set(Calendar.YEAR, yy + 2);
    datePickerDialog.setMaxDate(calen.getTimeInMillis());*/
    datePickerDialog.show();
}
Bethan
  • 971
  • 8
  • 23
  • Hi,there~ I've change the code to yours but it doesn't change at all. – Mark Feb 08 '18 at 11:06
  • What's your output?? let me know how far you are to the solution. – Bethan Feb 08 '18 at 11:09
  • The year, month, day is in red. Why? – Mark Feb 08 '18 at 12:01
  • It's very much good if you give more details about it. What is mean by red? – Bethan Feb 08 '18 at 12:06
  • This line ` },year, month, day);` – Mark Feb 08 '18 at 12:09
  • Did you declared the year,month and day variables? If yes give me your code sample here. – Bethan Feb 08 '18 at 12:17
  • So I need to declared the year,month and day equals to 0? – Mark Feb 08 '18 at 12:22
  • You don't need to initialize, just declare them as a global variables. – Bethan Feb 08 '18 at 12:23
  • Thanks, it works, but i cant see any confirm bottuon on the dialog. How can I do it? – Mark Feb 08 '18 at 12:33
  • there is no Cancel, Ok buttons at the bottom right corner? Can you share the screen shot with the issue? – Bethan Feb 08 '18 at 12:35
  • I've runned, but it says "Attempt to invoke virtual method 'void android.app.DatePickerDialog.show()' on a null object reference". – Mark Feb 08 '18 at 13:19
  • I've put the eddittext on touch code on the question. – Mark Feb 08 '18 at 13:34
  • Try `mDatePickerDialog.show();` instead of `datePickerDialog.show();` – Bethan Feb 09 '18 at 04:18
  • I've run successful, but I can't see the button. I've put the screenshot in the question. – Mark Feb 09 '18 at 05:27
  • I can not guess what code causing the issue since I get the complete code of your activity, anyhow you have a solution. `mDatePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_NEGATIVE) { // Do Stuff } } });` – Bethan Feb 09 '18 at 10:08
  • For your reference https://stackoverflow.com/questions/2928902/how-do-i-detect-a-cancel-click-of-the-datepicker-dialog/4981308#4981308 – Bethan Feb 09 '18 at 10:11