Bug 58665 - Date Picker and Time Picker Crash in Android
Summary: Date Picker and Time Picker Crash in Android
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
: 56577 58666 ()
Depends on:
Blocks:
 
Reported: 2017-08-09 11:53 UTC by Ojas Teredesai
Modified: 2017-10-19 15:36 UTC (History)
8 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Ojas Teredesai 2017-08-09 11:53:11 UTC
We are using the date and time picker in our Xamarin Application and based on our application requirement we have to customize it to make change in its opening behavior. So we have used Renderer of both controls but it works perfectly on iOS platform but application gets crashed on Android with error 'element is not of type Xamarin.Forms.TimePicker Parameter name: element a'. for more detail error please find the attached Screen Shorts.
 
Because of above issue we are blocked to deliver our next task to QA team.
 
We are using below code for Android Render which causes application crash:
 
namespace Zerochaos.Mobile.UI.Droid.Renderers
{
    public class ZCTimePickerRenderer:TimePickerRenderer
    {
        public ZCTimePickerRenderer()
        {
        }
        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
        {
            base.OnElementChanged(e);

            //this.Control.SetTextColor(Android.Graphics.Color.Green);
        }

    }
}
 
 
 
Below code we are using for iOS render and its working fine.
 
public class CustomeTimePickerRenderer:TimePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
        {
            base.OnElementChanged(e);

            CustTimePicker timePickerobj = (CustTimePicker)Element;

            if (timePickerobj != null)
            {
                SetBorderStyle(timePickerobj);
                SetTextColor(timePickerobj);

                Control.AdjustsFontSizeToFitWidth = true;
            }

            if (e.OldElement == null)
            {
                //Wire events
            }

            if (e.NewElement == null)
            {
                //Unwire events
            }

            var toolbar = new UIToolbar(new CoreGraphics.CGRect(0.0f, 0.0f, Control.Frame.Size.Width, 44.0f));

            toolbar.Items = new[]
            {

                 new UIBarButtonItem("Cancel", UIBarButtonItemStyle.Plain, delegate {  Control.ResignFirstResponder();

                    MessagingCenter.Send<MessageClass>(new MessageClass("cancel"),"Not update");
                   // this.Control.sele
                }),
                new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
               // new UIbar("Done", UIBarButtonItemStyle.Plain,),
                 new UIBarButtonItem("Done", UIBarButtonItemStyle.Plain, delegate {  Control.ResignFirstResponder();
                 MessagingCenter.Send<MessageClass>(new MessageClass("done"),"Not update");

                })



            };



            this.Control.InputAccessoryView = toolbar;

            if (this.Control != null)
            {
                this.Control.Layer.BorderWidth = 0;
                //this.Control.Layer.BorderColor = App.HowlOutBackground.ToCGColor();
                this.Control.TextAlignment = UITextAlignment.Left;
            }

        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
            {
                return;
            }

            CustTimePicker timePickerobj = (CustTimePicker)Element;

            if (e.PropertyName == CustTimePicker.TextColorProperty.PropertyName)
            {
                this.Control.TextColor = timePickerobj.TextColor.ToUIColor();
            }
        }

        void SetBorderStyle(CustTimePicker timePickerobj)
        {
            this.Control.BorderStyle = UIKit.UITextBorderStyle.None;
            //this.Control.BorderStyle = (datePicker.HasBorder == true) ? UIKit.UITextBorderStyle.RoundedRect : UIKit.UITextBorderStyle.None;

        }

        void SetTextColor(CustTimePicker timePickerobj)
        {
            this.Control.TextColor = timePickerobj.TextColor.ToUIColor();
        }

        void setFontSize(CustTimePicker timePickerobj)
        {

        }

    }
}
Comment 1 Paul DiPietro [MSFT] 2017-08-09 13:16:30 UTC
There are no screenshots. Can you please upload a reproduction project and confirm which versions of Forms you have tested against?
Comment 2 Paul DiPietro [MSFT] 2017-08-09 13:16:53 UTC
*** Bug 58666 has been marked as a duplicate of this bug. ***
Comment 4 Phuc Pham 2017-08-30 19:56:00 UTC
Here is the sample code and steps to reproduce this.  

Code : https://github.com/ojasteredesai/ZCMobileDemo.Lite/tree/master/ZCMobileDemo.Lite

Steps : 

Below are the Steps to Reproduce the issue on Android Platform.
1.) Run the app via above repository on Android device
2.) Click on GO TO LOGIN TYPE PAGE button
3.) Click on GO TO LOGIN PAGE button
4.) Click on Text Field that will open Time Picker
5.) Select time and click on OK button it will crash the app

We have also created the bug regarding this issue at Bugzilla and below is URL for that
https://bugzilla.xamarin.com/show_bug.cgi?id=58666

Please let me know if you need more info.
Comment 5 Paul DiPietro [MSFT] 2017-08-30 20:08:22 UTC
Can you please elaborate in full detail as to why you require placing a TimePicker into another TimePicker as opposed to using two separate ones?
Comment 6 Andrew Way 2017-08-30 20:15:04 UTC
*** Bug 56577 has been marked as a duplicate of this bug. ***
Comment 7 Phuc Pham 2017-08-31 17:12:13 UTC
Previously we were using Renderer for Android platform because we were not able to get the OK and Cancel event for both Date and Time Picker.
As a solution of this issue, we have used Renderer for iOS and put OK and Cancel button and handled their events. We have also used the Renderer for Android as well. 
iOS Renderer is working fine but on Android it causes app to crash.

We need your help in getting OK/Cancel button event for both Date and Time Picker. If that works then our problem will get resolved and we will not need the Renderer for Android. 
We have already tried PropertyChanged but using that we are not able to get / select current date(today date).

Or you can give us any example in which Android render will not crash the app. Then we will enhance it further by putting OK and Cancel buttons and handing their events.
Comment 8 Paul DiPietro [MSFT] 2017-09-01 03:37:02 UTC
I don't completely understand the need for the removal of the child from the view (parent.Children.Remove(selectedTime) is what's causing the crash), or rather why the ZCTimePicker is an Entry that controls an actual DatePicker element. The intention here of how this design has to do with the OK or Cancel events is what's confusing.

As it stands, it may be to your benefit to write a completely custom control in this case if you need to access the cancel event, as our renderers use a dialog that has one, for instance. Unfortunately some of the methods are internal and you might have to essentially copy some of the platform specific behavior into your project and renderers; I understand this is inconvenient, but it'd likely be an easier solution, I think. I would suggest looking at the renderer code (for example: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs) and possibly use that as a base to start from to see how they work. I'm sorry if this isn't of as much help as you'd like for the moment.
Comment 9 Rajveer Singh 2017-09-04 07:01:46 UTC
Hello Paul,

Thanks for your valuable feedback.

We are using Android 23 API so it is displaying both OK and Cancel buttons to us. But our problem is to get the click event for both buttons. We have tried PropertyChanged but in that if we select today/current date then we are not getting any date in callback but if we changed the date from current date to other date then it provides selected date to us. We have identified and already resolved that parent.Children.Remove(selectedTime) app crash issue.



But we wants to perform individual actions based on OK and Cancel buttons clicked by user and for that you already suggested that we need to use custom render so we will try your solution and let you know.
Comment 10 Rajveer Singh 2017-09-14 11:24:52 UTC
Hello Pall,

We have tried your suggested Renderer example and now its not making app crash but if you will see the code then you will found that there is not code implementation regarding handing OK and Cancel button in that Render itself. So that thing we will handled by setting current date if we don't have the value in Property change method callback.

Now we are facing another issue in which in Android platform DatePicker / TimePicker does not support Locale.It always displaying the calendar in English.

We have used the below code for setting locale in Android DatePicker Renderer.


protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
	base.OnElementChanged(e);
	if (Control != null)
	{
		Locale locale = new Locale((Thread.CurrentThread.CurrentCulture.Name).ToString().Replace("-", "_"));
		Control.TextLocale = locale;
		Locale.Default = new Locale((Thread.CurrentThread.CurrentCulture.Name).ToString().Replace("-", "_"));
	}
}

Can you please help us on that issue.
Comment 11 Paul DiPietro [MSFT] 2017-10-19 15:36:50 UTC
That sounds like something that is possibly a separate bug, at a glance. If the locale works on other controls but not the custom DatePicker using the latest 2.4.0 stable build, please open a separate issue with a reproduction project. Thank you.