question

DimChris avatar image
0 Votes"
DimChris asked LeonLu-MSFT edited

Get time from a TimePickerRenderer on Android

I am using the code below for firing an event in my mvvm page. Problem is that i don't know how to get time from a timepickerdialog

 public class MyTimePickerRenderer : TimePickerRenderer
  {
     public MyTimePickerRenderer(Context context) : base(context)
     {
          
     }

     protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
     {
         base.OnElementChanged(e);
         //Disposing
         if (e.OldElement != null)
         {
             _element = null;
         }

         //Creating
         if (e.NewElement != null)
         {
             _element = e.NewElement;

         }
     }
     public TimePicker _element;
     public TimePickerDialog dialog;
       

     protected override TimePickerDialog CreateTimePickerDialog(int hours, int minutes)
     {

         dialog = base.CreateTimePickerDialog(hours, minutes);
         dialog.ShowEvent += Dialog_ShowEvent;
         dialog.DismissEvent += Dialog_DismissEvent;
         dialog.CancelEvent += Dialog_CancelEvent;

         return dialog;
     }


     private void Dialog_ShowEvent(object sender, EventArgs e)
     {
         //var dialog = ((TimePickerDialog)sender);

         var positiveBtn = dialog.GetButton((int)DialogButtonType.Positive);
         positiveBtn.Click += PositiveBtn_Click;
     }

     private void PositiveBtn_Click(object sender, EventArgs e)
     {

         // _element.Time = TimeSpan.FromHours(ss.Hour) + TimeSpan.FromHours(ss.Minute);
         _element.Unfocus();
         try
         {
             MessagingCenter.Send<object>(this, "TimeRange");
         }
         catch
         { }
     }
     private void Dialog_CancelEvent(object sender, EventArgs e)
     {
         _element.Unfocus();
     }
     private void Dialog_DismissEvent(object sender, EventArgs e)
     {
         _element.Unfocus();
     }
 }


How can i set the _element.Time = inside my PositiveBtn_Click event?

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.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

How can i get Time on my mvvm page? Also can you send me the same method for iOS?

You can use above custom timpicker directly in MVVM page. For example, I binding a value to time in TimePicker like following code.

<TimePicker Time="{Binding SelectedTime}"></TimePicker>


We can get the SelectedTime from custom TimePicker in the viewModel.

TimeSpan selectedTime;
        public TimeSpan SelectedTime
        {
            set         
            {
                if (selectedTime != value)
                {
                    selectedTime = value;
                    OnPropertyChanged("SelectedTime");
                  //  DisplayAlert("info",selectedTime.ToString(),"OK");
                }
            }
            get
            {
                return selectedTime;
            }
        }


Here is running screenshot.

78277-image.png


For iOS achievement, you can refer to following thread:


https://stackoverflow.com/questions/49279271/24-hours-time-picker-selected-time



image.png (116.6 KiB)
· 3
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.

@DimChris Do you try to above code in your MVVM. Is that worked? And IOS solution I post in above SO link.

0 Votes 0 ·

Thank you LeonLu i will give it a try today, and i will write my results!

0 Votes 0 ·

OK, waitting for your update.

0 Votes 0 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered DimChris commented

Hello,​

Welcome to our Microsoft Q&A platform!

If you want to create a custom TimePicker, you don't need to use MessagingCenter to transfer selected time, and you can achieve IElementController to get the time from TimePickerDialog.IOnTimeSetListener like following code, when you new TimePickerDialog object, you should achieve this IOnTimeSetListener interface.

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App68.Droid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Xamarin.Forms.TimePicker), typeof(MyTimePickerRenderer))]
namespace App68.Droid
{
    public class MyTimePickerRenderer : ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable
    {
        TimePickerDialog _dialog;
        Context context;
        public MyTimePickerRenderer(Context context) : base(context)
        {
            this.context = context;
        }

        Context CurrentContext => context;
        IElementController ElementController => Element as IElementController;

        public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)
        {
            var time = new TimeSpan(hourOfDay, minute, 0);
            Element.SetValue(Xamarin.Forms.TimePicker.TimeProperty, time);

            Control.Text = time.ToString(@"hh\:mm");

            ClearFocus();
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
        {
            base.OnElementChanged(e);

            SetNativeControl(new Android.Widget.EditText(CurrentContext));

            if (Control != null)
            {
                Control.Click += Control_Click;
                Control.FocusChange += Control_FocusChange;
                Control.InputType = Android.Text.InputTypes.Null;
                if (Element != null && !Element.Time.Equals(default(TimeSpan)))
                    Control.Text = Element.Time.ToString(@"hh\:mm");
                else
                    Control.Text = DateTime.Now.ToString("HH:mm");
            }
        }

        void Control_FocusChange(object sender, FocusChangeEventArgs e)
        {
            if (e.HasFocus)
            {
                ShowTimePicker();
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
            }
            else
            {
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
            }
        }

        void Control_Click(object sender, EventArgs e) => ShowTimePicker();

        void ShowTimePicker()
        {
            if (_dialog == null)
                _dialog = new TimePickerDialog(CurrentContext, this, Element.Time.Hours, Element.Time.Minutes, true);

            _dialog.UpdateTime(Element.Time.Hours, Element.Time.Minutes);

            _dialog.Show();
        }
    }

}


Best Regards,

Leon Lu



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.


· 1
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.

Thank you LeonLu. How can i get Time on my mvvm page? Also can you send me the same method for iOS?

0 Votes 0 ·