Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin.Forms TimePicker with 15 minutes Intervals?

SheemuSheemu USMember ✭✭✭

how to pass min interval in the Xamarin Forms TimePicker.

I had solution for the renderer iOS:

[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderers))]
namespace Anjep_Toolbox.MobileApp.iOS.Renderer
{
public class TimePickerRenderers : TimePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if(e.NewElement != null)
{
var timePicker = (UIDatePicker)Control.InputView;
timePicker.Locale = new NSLocale("no_nb");
timePicker.MinuteInterval = new nint(15);
}

  //if (Control != null)
  //{
  //  Control.Text = DateTime.Now.ToString("HH:mm");
  //}
}

}
}

but not found any working solution for the android platform.

so guys kindly let me know if you have a solution.

Please help me!

Posts

  • SheemuSheemu USMember ✭✭✭

    also, I have got it for the android by this approach:

    Renderer class code
    using System;
    using Android.App;
    using Android.Widget;
    using YOURNAMESPACE.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;

    [assembly: ExportRenderer(typeof(Xamarin.Forms.TimePicker), typeof(MyTimePickerRenderer))]
    namespace Anjep_Toolbox.MobileApp.Droid.Renderer
    {
    public class MyTimePickerRenderer : ViewRenderer<Xamarin.Forms.TimePicker, EditText>
    {
    TimePickerDialogIntervals timePickerDlg;
    class TimePickerDialogIntervals : TimePickerDialog
    {
    public const int TimePickerInterval = 15;
    private bool _ignoreEvent = false;

    public TimePickerDialogIntervals(MyTimePickerRenderer context)
    : base(context.Context, new EventHandler<TimePickerDialog.TimeSetEventArgs>(context.UpdateDuration), context.Element.Time.Hours, context.Element.Time.Minutes, true)
    {
    }

    public override void OnTimeChanged(Android.Widget.TimePicker view, int hourOfDay, int 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;
    }
    

    }
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
    {
    base.OnElementChanged(e);
    // New up
    if (Control == null)
    {
    timePickerDlg = new TimePickerDialogIntervals(this);

    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);
    }

    }
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
    if (Control == null || Element == null)
    return;
    if("Focusable" == e.PropertyName && timePickerDlg != null)
    {
    var timeSender= sender as Xamarin.Forms.TimePicker;
    //Control.Text = timeSender.Time.Hours.ToString("00") + ":" + timeSender.Time.Minutes.ToString("00");
    timePickerDlg.UpdateTime(timeSender.Time.Hours, timeSender.Time.Minutes);
    timePickerDlg.Show();
    }
    base.OnElementPropertyChanged(sender, e);
    }

    public 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");
    }
    }
    }

    #
    I am calling it when going to click on the focus of the picker.

    TimePicker1.Focus();

    then it should be called OnElementPropertyChanged event further showing picker dialog as per properties name.

    Thank's :smile:

Sign In or Register to comment.