Search code examples
xamarin.formstimepicker

Xamarin Forms: Different time is showing on the UI after clock pop is closed in TimePicker?


I am using the below thread for setting the time in multiples of 5 in the time picker. Using the custom renderers I am able to select the time in multiples of 5. But after selecting a time, when the clock-Pop up closed, a different time is showing on the UI. The issue is only in the android platform, for the ios everything is working as excepted.

My code:

public class CustomTimePickerRenderer : TimePickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
    {
        base.OnElementChanged(e);

        TimePickerDialogIntervals timePickerDlg = new TimePickerDialogIntervals(this.Context, new EventHandler<TimePickerDialogIntervals.TimeSetEventArgs>(UpdateDuration),
            Element.Time.Hours, Element.Time.Minutes, true);

        var control = new EditText(this.Context);
        control.Focusable = false;
        control.FocusableInTouchMode = false;
        control.Clickable = false;
        control.Click += (sender, ea) => timePickerDlg.Show();
        control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");

        SetNativeControl(control);
    }

    void UpdateDuration(object sender, Android.App.TimePickerDialog.TimeSetEventArgs e)
    {
        Element.Time = new TimeSpan(e.HourOfDay, e.Minute, 0);
        Control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");
    }
}

public class TimePickerDialogIntervals : TimePickerDialog
{
    public const int TimePickerInterval = 05;
    private bool _ignoreEvent = false;

    public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
        : base(context, (sender, e) =>
        {
            callBack(sender, new TimePickerDialog.TimeSetEventArgs(e.HourOfDay, e.Minute * TimePickerInterval));
        }, hourOfDay, minute / TimePickerInterval, is24HourView)
    {
    }

    public override void OnTimeChanged(Android.Widget.TimePicker view, int hourOfDay, int minute)
    {
        base.OnTimeChanged(view, hourOfDay, minute);

        if (_ignoreEvent) return;

        if (minute % TimePickerInterval != 0)
        {
            int minuteFloor = minute - (minute % TimePickerInterval);
            minute = minuteFloor + (minute == minuteFloor + 1 ? TimePickerInterval : 0);
            if (minute == 60)
                minute = 0;
            _ignoreEvent = true;
            view.CurrentMinute = (Java.Lang.Integer)minute;
            _ignoreEvent = false;
        }
    }
}

Why a different time is showing on the UI when the clock pop-up closed?


Solution

  • The problem is because the minute has been multiplied by 'TimePickerInterval' (05). Changing the parameter to 'e.Minute' will work as expected.

    Code:

    public class TimePickerDialogIntervals : TimePickerDialog
    {
        public const int TimePickerInterval = 15;
        private bool _ignoreEvent = false;
        public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView) :
            base(context, (sender, e) =>
            {
                callBack(sender, new TimePickerDialog.TimeSetEventArgs(e.HourOfDay, e.Minute));//remove '* TimePickerInterval'
            }, hourOfDay, minute / TimePickerInterval, is24HourView)
        {
        }
        ...
    }