xamarin.formsandroid-timepicker

Xamarin.forms TimePickerDialog Android KeyBoard input


i am using Xamarin.Forms TimePicker with custom renderer functionality. Everything works fine when user uses the default scroll feature of TimePickerDialog.

Issue is I want to hide the Android keyboard when user tap on the TimePickerDialog to update the time in HH:mm format.

I would be thankful for any help and guidance.


Solution

  • You could set the InputType of the EditText in Custom Renderer

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
            {
                base.OnElementChanged(e);
    
                if(Control!=null)
                {
                 
                   Control.InputType = Android.Text.InputTypes.Null;
                   
    
                }
            }
    

    Update

    If you create a custom TimePicker in Android , you can also set the property DescendantFocusability of TimePicker like

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    using xxx.Droid;
    
    [assembly:ExportRenderer(typeof(Xamarin.Forms.TimePicker),typeof(MyTimePickerRenderer))]
    namespace xxx.Droid
    {
        public class MyTimePickerRenderer : TimePickerRenderer
        {
            Context Context;
    
            IElementController ElementController => Element as IElementController;
    
            public MyTimePickerRenderer(Context context) : base(context)
            {
                Context = context;
            }
    
    
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
            {
                base.OnElementChanged(e);
    
                if(Control!=null)
                {
    
                    //  Control.InputType = Android.Text.InputTypes.Null;
    
                    Control.Click += Control_Click;
    
                }
            }
    
            private AlertDialog _dialog;
    
    
            private void Control_Click(object sender, EventArgs e)
            {
                Xamarin.Forms.TimePicker model = Element;
    
                Android.Widget.TimePicker picker = new Android.Widget.TimePicker(Context);
    
                picker.DescendantFocusability = DescendantFocusability.BlockDescendants;  // block keyboard here 
    
               var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
                layout.AddView(picker);
    
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
    
                var builder = new AlertDialog.Builder(Context);
                builder.SetView(layout);
    
    
              
                builder.SetNegativeButton("Cancel  ", (s, a) =>
                {
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    // It is possible for the Content of the Page to be changed when Focus is changed.
                    // In this case, we'll lose our Control.
                    Control?.ClearFocus();
                    _dialog = null;
                });
                builder.SetPositiveButton("Ok ", (s, a) =>
                {
    
                    ElementController.SetValueFromRenderer(Xamarin.Forms.TimePicker.TimeProperty, new TimeSpan(picker.Hour,picker.Minute,0));
                    // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
                    // In this case, the Element & Control will no longer exist.
                    if (Element != null)
                    {
                        
                        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                        // It is also possible for the Content of the Page to be changed when Focus is changed.
                        // In this case, we'll lose our Control.
                        Control?.ClearFocus();
                    }
    
                    _dialog = null;
                });
    
                _dialog = builder.Create();
                _dialog.DismissEvent += (ssender, args) =>
                {
                    ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                };
                _dialog.Show();
            }
        }
    }
    

    enter image description here