locked
Datepicker: How to override done button clicked of datepicker in xamarin android? RRS feed

  • Question

  • User247716 posted

    I want to override done button clicked, dont wanna use dateselected event. In iOS i have done, but dont no how to do in android.

    Thursday, April 13, 2017 11:21 AM

All replies

  • User207279 posted

    @"Abdul.3549" It's not straight forward in Android as the Entire datepicker dialog is created internally, you can use reflection to achieve it but the entire Forms control flow will get changed. I can share some test code which i've tried

    [assembly: ExportRenderer(typeof(DatePicker), typeof(CustomDatePickerRenderer))]
    namespace XamarinFormsMaps.Droid
    {
        public class CustomDatePickerRenderer : DatePickerRenderer
        {
            private DatePickerDialog datePickerDialog;
    
            protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
            {
                base.OnElementChanged(e);
                this.Control.Click += Control_Click;
            }
    
            private void Control_Click(object sender, EventArgs e)
            {
    
                var picker = typeof(DatePickerRenderer);
                var triggerMehotd = picker.GetMethod("OnTextFieldClicked", BindingFlags.NonPublic);
                triggerMehotd.Invoke(this, null);
                var fieldInfo = picker.GetField("_dialog", BindingFlags.NonPublic);
                datePickerDialog = (DatePickerDialog)fieldInfo.GetValue(this);
                // OK button click
                datePickerDialog.GetButton(-1).Click += CustomPageRenderer_Click;
            }
    
            private void CustomPageRenderer_Click(object sender, EventArgs e)
            {
                datePickerDialog.Hide();
                Element.Focus();
            }
        }
    }
    
    Thursday, April 13, 2017 12:44 PM
  • User247716 posted

    thanks @DK90 for replying.

    Reflection is not working fine here, triggerMehotd and fieldInfo returns always null.

    Thursday, April 20, 2017 2:36 PM
  • User207279 posted

    @"Abdul.3549"

    picker.GetMethod("OnTextFieldClicked", BindingFlags.NonPublic | BindingFlags.Instance); Use this line.

    Tuesday, April 25, 2017 5:38 AM
  • User130968 posted

    @DK90 How should I recognize 'Cancel' button event. What changes I have to do ?

    Friday, July 7, 2017 1:34 PM
  • User141898 posted

    Hi @Abdul.3549

    How you did this in iOS? I would appreciate for a tiny code snippet.

    Cheers Philipp

    Monday, February 5, 2018 9:32 AM