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 9060065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:07:41+00:00 2026-06-16T15:07:41+00:00

As far as I am aware, versions prior to Jelly Beans, had Set and

  • 0

As far as I am aware, versions prior to Jelly Beans, had Set and Cancel buttons when using TimePickerDialog. Jelly Beans has only Done button.

I could just leave the Done button, and could close the dialog by clicking outside the dialog, but my onTimeSetListener gets called if pressing Done or outside the dialog.

So I found this stackoverflow question which describes, that there is a bug when using DatePickerDialog. To fix the issue using DatePickerDialog, when initializing I had to set the onDateSetListener to null, and implement my own buttons to handle the BUTTON_POSITIVE (Set) and BUTTON_NEGATIVE (Cancel) onClick method. Which is fine, because when button Set is called I can access the DatePicker values like this

int yearPicked = dateDlg.getDatePicker().getYear();
int monthPicked = dateDlg.getDatePicker().getMonth();
int dayPicked = dateDlg.getDatePicker().getDayOfMonth();

So there is no need to use onDateSetListener, but if I would, it would again be called when pressing Set or Cancel.

I tried to use TimePickerDialog the same way, but the problem is, that inside BUTTON_POSITIVE onClick method, I cannot access the hour and minutes values as before, because TimePickerDialog does not provide TimePicker as DatePickerDialog provides DatePicker. And again if I would used onTimeSetListener, it would be called by pressing anything.

Calendar cal = Calendar.getInstance();

int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);

final TimePickerDialog timeDlg = new TimePickerDialog(PreferencesActivity.this, null, hour, min, true);

// Make the Set button
timeDlg.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            // CANNOT ACCES THE VALUES
            Toast.makeText(PreferencesActivity.this, "Set", Toast.LENGTH_SHORT).show();
        }
    }
});


// Set the Cancel button
timeDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_NEGATIVE) {
            Toast.makeText(PreferencesActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
        }
    }
});

timeDlg.show();
  • 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-06-16T15:07:42+00:00Added an answer on June 16, 2026 at 3:07 pm

    Based on your comment, it should be easy enough to guard the OnTimeSetListener callback with a boolean to prevent your logic from running in the case ‘Cancel’ is clicked.

    I had a quick play with your code, and the following seemed to work alright:

    private boolean mIgnoreTimeSet = false;
    
    final TimePickerDialog timeDlg = new TimePickerDialog(PreferencesActivity.this, PreferencesActivity.this, hour, min, true);
    
    // Make the Set button
    timeDlg.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            mIgnoreTimeSet = false;
            // only manually invoke OnTimeSetListener (through the dialog) on pre-ICS devices
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) timeDlg.onClick(dialog, which);
            Toast.makeText(getApplicationContext(), "Set", Toast.LENGTH_SHORT).show();
        }
    });
    
    // Set the Cancel button
    timeDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
            mIgnoreTimeSet = true;
            dialog.cancel();
        }
    });
    
    timeDlg.show();
    
    @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if (mIgnoreTimeSet) return;
        // get the values here - this will only run if 'Set' was clicked
    }
    

    For the sake of the example, I’ve assumed your PreferencesActivity implements OnTimeSetListener. Since onTimeSet(...) gets called after onClick(...), you can simply toggle a boolean that will determine whether you should do something with the values in the callback or just ignore them.

    I have to agree it’s not ideal, but at least functional. After the holidays I’ll try to have another look in a ‘better’ solution.

    One alternative would be to use reflection to get a handle on the internally used TimePicker widget, but that’s more likely to break in future Android releases.


    // Edit: Potential alternative? (not tested)

    Second alternative, which might be a bit ‘cleaner’: extend TimePickerDialog and overwrite the (empty) public method:

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
    

    In there, keep track of the user-set hour of the day and minute and implement two getters that return them. Then only get these values from the dialog if the positive button gets clicked, but just fall through for ‘Cancel’. Basically you then won’t need an OnTimeSetListener anymore, and hence it won’t matter whether it is ever gets called or not.


    // Edit2: Support for pre-ICS devices

    After receiving a couple of remarks about the proposed solution not working on pre-ICS devices, I made a small change to the above code – even tough it wasn’t within the scope of the original question.

    The main difference between pre-ICS and post-ICS devices is that the OnTimeSetListener callback isn’t automatically invoked after your dialog button’s onClick() method(s) is run. A simple workaround for that is to call the onClick() method on the dialog, which will then call the listener. In the solution above, I’ve added a version code check, because otherwise your listener will end up being called twice on post-ICS devices, which may lead to undesired side-effects.

    Tested and confirmed behaviour on Android 2.1-update1 and Android 4.2.2.

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

Sidebar

Related Questions

As far as I'm aware, the Macbook Air 2012 supports OpenGL 3.2. When using
As far as I'm aware, plain html or javascript won't allow me to upload
So far I have only been updated a view from within its controller. I
As far as my understanding after reading and researching, the purpose of using salt
I'm aware that this question has appeared in various forms before, but none of
As far as I'm aware the current stable release of HBase, 0.2, does not
As far as im aware, the WebView is the same thing as the built
As far as I am aware there is no inbuilt polygon functionality for Python.
Far as best practices are concerned, which is better: public void SomeMethod(string str) {
as far as I know both 2012-07-04T17:30:52+00:00 and 2012-07-04T17:30:52Z are dates in iso8601 format

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.