0

I have a screen where the user selects a Time in the screen from a TimePickerDialog instance. But in the call back I couldn't obtain the selected date. My code is like this:

public void onSomeButtonClick(object sender, EventArgs e)
{
    t = new TimePickerDialog(this, (EventHandler<TimePickerDialog.TimeSetEventArgs>)null, hour, min, false);
    t.SetButton("OK", data_TimePickerCallback);
    t.Show()
}


private void data_TimePickerCallback(object sender, EventArgs e)
{
    int hour = ((TimePickerDialog)sender)... // How to get the Time selected here??
}

Why Am I not passing the CallBack in the Constructor? Because of this error (basically: in some versions of android, the system only puts an "Ok" button in the Dialog so if you either click Ok or click outside of the timepicker dialog, both would trigger the callback as if it was confirming), and as described there, the workaround is to add the behavior through adding a Custom Button in the picker as described in the code above.

The way I was doing before was working fine in most of the devices, but not all because of this error, and the code was approximately like this:

public void onSomeButtonClick(object sender, EventArgs e)
{
    new TimePickerDialog(this, data_TimePickerCallback, hour, min, false).Show();
}

private void data_TimePickerCallback(object sender, TimePickerDialog.TimeSetEventArgs e)
{
    int hour = e.HourOfDay;
    int minute = e.Minute;
}

Before it was easy to get the Time but now with this change I didn't found a way inside the sender object to get the time (the int vars minute and hour).

Edit: I tried also implementing the TimePickerDialog.IOnTimeSetListener on my Activity class and adding the OnTimeSet(TimePicker view, int hourOfDay, int minute) to it as the code below but still resulted in the same error:

public class CadCliente : Activity, TimePickerDialog.IOnTimeSetListener{

    public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        // handles
    }

    //...
    public void onSomeButtonClick(object sender, EventArgs e)
    {
        new TimePickerDialog(this, this, hour, minute, false).Show();
    }
}
Fnr
  • 2,096
  • 7
  • 41
  • 76
  • 1
    [The two parameters `SetButton` was deprecated](https://developer.android.com/reference/android/app/AlertDialog.html#setButton) – Robbit Dec 14 '17 at 02:48
  • What about this `public TimePickerDialog(Context context, IOnTimeSetListener listener, int hourOfDay, int minute, bool is24HourView);` not callback, use `IOnTimeSetListener` interface. – Robbit Dec 14 '17 at 03:56
  • @JoeLv, I tried using the IOnTimeSetListener and resulted in the same error, I'll edit my post with the code I tried – Fnr Dec 14 '17 at 16:33
  • I have read the bug on Android 4.1, and [there are some solutions](https://issuetracker.google.com/issues/36951008#c39). If you use `SetButton`, maybe you can get the `TimePicker` and then get `NumberPicker` by `Reflection`(I couldn't find a way to get the `TimePicker`, because there is no `TimePicker` in `TimePickerDialog` except the `IOnTimeSetListener` interface). So, if you just want to avoid twice callback, why you don't use a bool value or other way? – Robbit Dec 15 '17 at 02:29

0 Answers0