question

SreejithSree-2948 avatar image
0 Votes"
SreejithSree-2948 asked JarvanZhang-MSFT answered

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?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

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

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

Check the 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)
    {

    }
    ...
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.