Xamarin.Forms TimePicker: Make some items unselectable

Vinayak Nikam 71 Reputation points
2021-06-22T09:28:47.387+00:00

Based on my custom validation logic, if any specific time is invalid, then I want to disallow user to select such entry. How to achieve this in Xamarin.Forms TimePicker? May be entry is shown disabled and user should not be able to select it. or the done/ok button remains disabled on iOS and Android.

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

Accepted answer
  1. Anonymous
    2021-06-23T06:21:13.877+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create a custom TimePicker in android to disable the Ok button with ((TimePickerDialog)dialog).GetButton((int)DialogButtonType.Positive).Enabled = false;.

       using Android.App;  
       using Android.Content;  
       using Android.OS;  
       using Android.Runtime;  
       using Android.Views;  
       using Android.Widget;  
       using App107.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 App107.Droid  
       {  
           class MyTimePickerRenderer : ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable  
           {  
               private TimePickerDialog dialog = null;  
         
               public MyTimePickerRenderer(Context context) : base(context)  
               {  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)  
               {  
                   base.OnElementChanged(e);  
                   this.SetNativeControl(new Android.Widget.EditText(Forms.Context));  
                   this.Control.Click += Control_Click;  
                   this.Control.Text = DateTime.Now.ToString("HH:mm");  
                   this.Control.KeyListener = null;  
                   this.Control.FocusChange += Control_FocusChange;  
               }  
         
               void Control_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)  
               {  
                   if (e.HasFocus)  
                       ShowTimePicker();  
               }  
         
               void Control_Click(object sender, EventArgs e)  
               {  
                   ShowTimePicker();  
               }  
         
               private void ShowTimePicker()  
               {  
                   if (dialog == null)  
                   {  
                       dialog = new TimePickerDialog(Forms.Context, this, DateTime.Now.Hour, DateTime.Now.Minute, true);  
                        
                   }  
                   dialog.SetOnShowListener(new OnTimeShowListener());  
                   dialog.Show();  
         
                   
               }  
         
               public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)  
               {  
                   var time = new TimeSpan(hourOfDay, minute, 0);  
                   this.Element.SetValue(Xamarin.Forms.TimePicker.TimeProperty, time);  
         
                   this.Control.Text = time.ToString(@"hh\:mm");  
               }  
           }  
         
           public class OnTimeShowListener : Java.Lang.Object, IDialogInterfaceOnShowListener  
           {  
               public void OnShow(IDialogInterface dialog)  
               {  
                   ((TimePickerDialog)dialog).GetButton((int)DialogButtonType.Positive).Enabled = false;  
               }  
           }  
         
       }  
    

    Here is running screenshot when you disable it.

    108482-image.png

    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.


0 additional answers

Sort by: Most helpful

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.