Get time from a TimePickerRenderer on Android

Dimitris Christoforidis 301 Reputation points
2021-03-16T12:43:25.533+00:00

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?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-03-16T13:53:45.613+00:00

    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


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-03-16T13:31:37.56+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.