You can set the styling of the DatePicker dialog natively (without a renderer) by setting <item name="android:datePickerDialogTheme">
in your Android theme.
DatePicker OK/Cancel buttons not visible on Android
I added DatePicker in my app. However, when I tested it, I found that on Android the calendar's OK/Cancel buttons were colored white and the background was also white. So the OK/Cancel buttons were not visible. Also, when I clicked a date, it was highlighted in white and this made the selected date not visible either. Is there a way to change the color of the buttons and the selected date? Do I need to create a custom renderer for Android date picker?
Developer technologies | .NET | Xamarin
-
Joe Manke 1,091 Reputation points
2021-08-02T14:24:07.87+00:00
1 additional answer
Sort by: Most helpful
-
Anonymous
2021-08-02T07:59:28.7+00:00 Hello,
Welcome to our Microsoft Q&A platform!
Is there a way to change the color of the buttons and the selected date? Do I need to create a custom renderer for Android date picker?
Yes, I create a custom renderer for DatePicker in android to achieve it.
using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using App117.Droid; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(Xamarin.Forms.DatePicker), typeof(FixedDatePickerRenderer))] namespace App117.Droid { public class FixedDatePickerRenderer : Xamarin.Forms.Platform.Android.DatePickerRenderer { public FixedDatePickerRenderer(Context context) : base(context) { } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); //set color of selected date Control.SetTextColor(Android.Graphics.Color.Blue); } protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e) { base.OnElementChanged(e); //Disposing if (e.OldElement != null) { _element = null; } //Creating if (e.NewElement != null) { _element = e.NewElement; } } protected Xamarin.Forms.DatePicker _element; DatePickerDialog dialog; protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day) { dialog = new DatePickerDialog(Context, (o, e) => { _element.Date = e.Date; ((IElementController)_element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false); }, year, month, day); dialog.SetButton((int)DialogButtonType.Positive, Context.Resources.GetString(global::Android.Resource.String.Ok), OnOk); dialog.SetButton((int)DialogButtonType.Negative, Context.Resources.GetString(global::Android.Resource.String.Cancel), OnCancel); dialog.Show(); //set OK/Cancel buttons color dialog.GetButton((int)DialogButtonType.Positive).SetTextColor(Android.Graphics.Color.Green); dialog.GetButton((int)DialogButtonType.Negative).SetTextColor(Android.Graphics.Color.Purple); // CancelButton.SetTextColor(Android.Graphics.Color.HotPink); return dialog; } private void OnCancel(object sender, DialogClickEventArgs e) { _element.Unfocus(); //((FixedDatePicker) _element)?.CallOnCancel(); } private void OnOk(object sender, DialogClickEventArgs e) { //need to set date from native control manually now _element.Date = ((DatePickerDialog)sender).DatePicker.DateTime; _element.Unfocus(); //((FixedDatePicker)_element)?.CallOnOk(); } } }
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.