Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:55:48+00:00 2026-05-27T01:55:48+00:00

I have a datePicker dialog in my app. It works fine when I select

  • 0

I have a datePicker dialog in my app. It works fine when I select a date. However if in the dialog I change the date and then press cancel, the original edittext remains unchanged as it should, but the Date picker still has the date from before when it was cancelled. I would like to make sure that EVERY time I go into the Date Picker, it sets the date from the EditText. My code is as follows.

onCreate() Method

//  Date Listener
    fdEtDate.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });

Further down the Activity

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    dateSetListener,
                    cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
    case STD_DIALOG_ID:
        return new TimePickerDialog(this,
                    stdSetListener,
                    stdHH, stdMM,
                    true);
    case STA_DIALOG_ID:
        return new TimePickerDialog(this,
                    staSetListener,
                    staHH, staMM,
                    true);
    }
    return null;
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateSetListener =
    new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            updateDisplay();
        }
};
// updates the date in the TextView
private void updateDisplay() {
    fdEtDate.setText(
        new StringBuilder()
                .append(Utils.pad(cal.get(Calendar.DATE))).append(Const.SQL_DATE_SEP)
                .append(Utils.pad(cal.get(Calendar.MONTH)+1)).append(Const.SQL_DATE_SEP)
                .append(cal.get(Calendar.YEAR)));

    fdEtStd.setText(
        new StringBuilder()
                .append(Utils.pad(stdHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(stdMM)));

    fdEtSta.setText(
        new StringBuilder()
                .append(Utils.pad(staHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(staMM)));
}

Is seems to create a new Dialog each time, so why isn’t it initialised each time with the Date from fdEtDate (EditText)

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:55:48+00:00Added an answer on May 27, 2026 at 1:55 am

    you should use onPrepareDialog to set the initial date every time the dialog is shown.

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case START_DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(
                mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.DAY_OF_MONTH))
        }
    }
    

    A dialog is created only the first time it is shown and from that point it is reused unless you destroy it explicitly with removeDialog().

    If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it’s important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it

    http://developer.android.com/guide/topics/ui/dialogs.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datepicker which shows date in format dd/MM/yyyy(i know i coould change
In my app i have an editText . If i click on that then
I have a DatePicker and a TimePicker in my app. Can anyone tell me
I have a jquery datepicker which is renders the current date by default as
I have a DatePickerDialog in my app. In the title of the dialog, the
I have a dialog, and I have a datepicker field on the dialog. When
What's new in jQuery(including UI) and will I have problems(using UI dialog and datepicker)
I have a JQuery modal dialog with 2 JQuery UI datepicker inputs. My problem
I have the following code: $(document).ready(function(){ $(.datepicker).each(function() { $(this).datepicker({ beforeShowDay: function(date){ return [$(this).parent().find(span.days).html(), ]
Let's say I have a datePicker called foobar: <g:datePicker name=foobar value=${new Date()} precision=day />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.