Skip to content Skip to sidebar Skip to footer

Datepickerdialog Always Opens The Current Date

I am having trouble with a DatePickerDialog, everytime I open it shows the current date not the date I set previously. Here's my code: final Calendar c = Calendar.getInstance()

Solution 1:

This may help you:

publicclassTestActivityextendsActivity {
privateint mYear;
privateint mMonth;
privateint mDay;

privatevoidshowDialog() {
    if (mYear == 0 || mMonth == 0 || mDay == 0) {
        finalCalendarc= Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog.OnDateSetListenerdatePickerListener=newDatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.publicvoidonDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            editText.setText(selectedYear + "年" + selectedMonth + 1 + "月"
                    + selectedDay + "日");
            mYear = selectedYear;
            mMonth = selectedMonth;
            mDay = selectedDay;
        }
    };

    DatePickerDialogdatePickerDialog=newDatePickerDialog(
            SampleActivity.this, datePickerListener, mYear, mMonth, mDay);

    datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
            newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        dialog.cancel();
                    }
                }
            });

    datePickerDialog.setCancelable(false);
    datePickerDialog.show();
}
}

Below sample code will give complete solution:

publicclassSecondActivityextendsFragmentActivityimplementsOnClickListener{

    privatestaticfinalStringTAG= SecondActivity.class.getName();
    privateint year;
    privateint month;
    privateint day;
    privateboolean isCancelled;

    private EditText editText;

    @OverrideprotectedvoidonCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.second_activity_layout);
        editText = (EditText) findViewById(R.id.editText);
    }

    publicvoidonButtonClick(View view) {
        onClick(view);
    }

    @OverridepublicvoidonClick(View v) {
        if(year == 0) {
            Calendarcalendar= Calendar.getInstance();
            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH);
            year = calendar.get(Calendar.YEAR);
        }
        OnDateSetListenercallBack=newOnDateSetListener() {

            @OverridepublicvoidonDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                if(isCancelled) {
                    Log.i(TAG, "Cancelled");
                    isCancelled = false;
                } else {
                    editText.setText("Date selected");
                    month = monthOfYear;
                    day = dayOfMonth;
                    SecondActivity.this.year = year;
                }
            }
        };
        DatePickerDialogdialog=newDatePickerDialog(this, callBack, year, month, day);
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", newDialogInterface.OnClickListener() {

            @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                if(which == DialogInterface.BUTTON_NEGATIVE) {
                Log.i(TAG, "Cancel");
                isCancelled = true;
                dialog.dismiss();}
            }
        });
        dialog.setCancelable(false);
        dialog.show();
    }
}

Solution 2:

Just save date,month and year values into VARIABLES while SETTING the date and in next set use ONPREPAREDIALOG function to set previously set date in your satepicker dialog - such as -

privateint thisYear, thisMonth, thisday;

private DatePickerDialog.OnDateSetListenerdateSetListener=newDatePickerDialog.OnDateSetListener() {
    publicvoidonDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
           thisYear = year;
           thisMonth = monthOfYear;
           thisday = dayOfMonth;
    }
};

@OverrideprotectedvoidonPrepareDialog(int id, Dialog dialog) {
          ((DatePickerDialog) dialog).updateDate(thisYear , thisMonth , thisday);
}

Post a Comment for "Datepickerdialog Always Opens The Current Date"