3

I am having a weird issue where in the onClick() listener on TimePickerDialog has stopped working on Android 8.0.

Here is my code:

          _timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,"SET", new DialogInterface.OnClickListener() {


    //System.out.println("ZZZZZ HIT!!!! onClick for _showTimeDialog "); 
      public void onClick(DialogInterface arg0, int arg1) {
        _done = true;
        /*
         * HACK: TimePickerDialog does not have a public method to return
         * the TimePicker hence we get a handle to the TimePicker from the
         * sdk. This would not work if the id of that component changes.
         */
        System.out.println("ZZZZZ HIT!!!! onClick for _showTimeDialog ");

        int searchPlateId = _timePickerDialog.getContext().getResources().getIdentifier("android:id/timePicker", null, null);
        TimePicker searchPlate = (TimePicker) _timePickerDialog.findViewById(searchPlateId);
        //String android_os = getAndroidVersion(android.os.Build.VERSION.SDK_INT);

       // if(android.os.Build.VERSION.SDK_INT > 23)
        _timePickerDialogCallback.onTimeSet(searchPlate, searchPlate.getHour(), searchPlate.getMinute());
       // else
        //  _timePickerDialogCallback.onTimeSet(searchPlate, searchPlate.getCurrentHour(), searchPlate.getCurrentMinute());
          //_timePickerDialogCallback.onTimeSet(searchPlate, searchPlate.getHour(), searchPlate.getMinute());


        //_timePickerDialogCallback.onTimeSet(searchPlate, searchPlate.getCurrentHour(), searchPlate.getCurrentMinute());
        System.out.println("ZZZZZ END HIT!!!! onClick for _showTimeDialog ");
      }

    });        

The same code works when i wire it with the DialogInterface.BUTTON_NEUTRAL or DialogInterface.BUTTON_NEGATIVE buttons.

This also works on older version of Android.

Issue I am observing is that click on OK button doesn't hit the onClick() method.

Please advise.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Are you calling `_timePickerDialog.setButton(...)` before or after the `show()` call? – Mike M. Oct 15 '18 at 13:09
  • Before the show() call – Pavan Tiruvuri Oct 16 '18 at 06:56
  • A [somewhat recent change to `TimePickerDialog`](https://android.googlesource.com/platform/frameworks/base/+/ab14d825c3eb2a337de02af93333e8302c08c732%5E%21/core/java/android/app/TimePickerDialog.java) moved its own set call for `BUTTON_POSITIVE` into a `show()` override, so if you set your `OnClickListener` before the `show()` call, it gets overridden by the one `TimePickerDialog` sets on itself in `show()`. It looks like it should be OK if you just move it to after `show()`. – Mike M. Oct 16 '18 at 07:04
  • Still doesn't work, I have pushed the code to below the show() call.It doesn't seem like its picking the change as the Button label defaults to OK and in my code I have it set to SET – Pavan Tiruvuri Oct 16 '18 at 09:20
  • Thanks Mark, I was able to resolve by overriding show() and setting an action listener on using : getButton(BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() – Pavan Tiruvuri Oct 16 '18 at 11:46

0 Answers0