DatePicker Placeholder And Border Style Using CustomRenderer

Introduction

In Xamarin.Forms, there is no properties like Placeholder, BorderRadius, BorderColor, and BorderWidth  for DatePicker Control. So in this article, we will learn how to set those properties for DatePicker using CustomRenderer.

Requirements

  • This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  • This article is prepared on a MAC machine.
  • This sample project is Xamarin.Forms PCL project.
  • This sample app is targeted for Android, iOS. And, tested for Android & iOS.

Description

The creation of a Xamarin.Forms project is very simple in Visual Studio for Mac. It will create three projects:

  1. Shared Code
  2. Xamarin.Android
  3. Xamarin.iOS

The Mac system with Visual Studio for Mac doesn't support Windows projects (UWP, Windows, Windows Phone). The following steps will show you how to create Xamarin.Forms projects in a Mac system with Visual Studio.

First, open Visual Studio and click on New Project.

Xamarin

After that, we need to select among Xamarin.Forms or Xamarin.Android or Xamarin.iOS project. If we want to create Xamarin.Forms project, just follow the below screenshot.

Xamarin

Then, we have to give the app name; i.e., DatePickerDefaultTextDemo.

Xamarin

Note
In the above screen, under Shared Code, select Portable Class Library or use Shared Library.

Then, click "Next" and the following screen will be displayed. In that screen, we have to browse the file path where we want to save that application on our PC.

Xamarin

 After clicking on the "Create" button, it will create the DatePickerDefaultTextDemo Xamarin.Forms project like below.

Xamarin

And the project structure will be:

  • DatePickerDefaultTextDemo
    It is for Shared Code

  • DatePickerDefaultTextDemo.Droid
    It is for Android.
  • DatePickerDefaultTextDemo.iOS
    It is for iOS. 
 structure

Now, follow the below steps.

Portable Class Library (PCL)

Step 1

In PCL, create a class name DatePickerCtrl inside the CustomControls folder and it should inherit from DatePicker like below.

DatePickerCtrl.cs

  1. using Xamarin.Forms;  
  2. namespace DatePickerDefaultTextDemo.CustomControls {  
  3.     public class DatePickerCtrl: DatePicker {  
  4.         public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(DatePickerCtrl), defaultValue: default (string));  
  5.         public string Placeholder {  
  6.             get;  
  7.             set;  
  8.         }  
  9.     }  
  10. }  
Added a Bindable property for DatePicker placeholder in the above class.

Step 2

Create your own XAML page named DatePickerTextPage.xaml inside the Views folder and make sure to refer to "DatePickerCtrl" class in XAML by declaring a namespace for its location and using the namespace prefix on the control element. The following code example shows how the "DatePickerCtrl" renderer class can be consumed by an XAML page:

DatePickerTextPage.xaml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
  3. BackgroundColor="White" 
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
  5. xmlns:custom="clr-namespace:DatePickerDefaultTextDemo.CustomControls;assembly=DatePickerDefaultTextDemo" 
  6. x:Class="DatePickerDefaultTextDemo.Views.DatePickerTextPage">  
  7.     <StackLayout VerticalOptions="Start" Padding="25">  
  8.         <custom:DatePickerCtrl x:Name="datePicker" Placeholder="Select Date of birth" HeightRequest="45" HorizontalOptions="FillAndExpand" /> 
  9. </StackLayout>  
  10. </ContentPage> 

Note

The "custom" namespace prefix can be named anything. However, the clr-namespace and assembly values must match the details of the custom renderer class. Once the namespace is declared the prefix is used to reference the custom control.

DatePickerTextPage.xaml.cs

  1. using Xamarin.Forms;  
  2. namespace DatePickerDefaultTextDemo.Views {  
  3.     public partial class DatePickerTextPage: ContentPage {  
  4.         public DatePickerTextPage() {  
  5.             InitializeComponent();  
  6.         }  
  7.     }  

Xamarin.Andriod

In an Android project, create a class and add the code like below

DatePickerCtrlRenderer.cs

  1. using System;  
  2. using Android.Graphics.Drawables;  
  3. using DatyePickerDefaultTextDemo.CustomControls;  
  4. using DatyePickerDefaultTextDemo.Droid;  
  5. using Xamarin.Forms;  
  6. using Xamarin.Forms.Platform.Android;  
  7. [assembly: ExportRenderer(typeof(DatePickerCtrl), typeof(DatePickerCtrlRenderer))]  
  8. namespace DatyePickerDefaultTextDemo.Droid {  
  9.     public class DatePickerCtrlRenderer: DatePickerRenderer {  
  10.             protected override void OnElementChanged(ElementChangedEventArgs < DatePicker > e) {  
  11.                 base.OnElementChanged(e);  
  12.                 
  13.                this.Control.SetTextColor(Android.Graphics.Color.#533f95;   
  14.                this.Control.SetBackgroundColor(Android.Graphics.Color.Transparent);   
  15.                this.Control.SetPadding(20,0,0,0);  
  16.                this.Control.SetBackgroundDrawable(gd);  
  17.                
  18.                GradientDrawable gd = new GradientDrawable(); 
  19.                SetCornerRadius(25); //increase or decrease to changes the corner look
  20.                SetColor(Android.Graphics.Color.Transparent); 
  21.                SetStroke(3, Android.Graphics.Color.#533f95;             
  22.          
  23.                 DatePickerCtrl element = Element as DatePickerCtrl;  
  24.                         if (!string.IsNullOrWhiteSpace(element.Placeholder)) {  
  25.                             Control.Text = element.Placeholder;  
  26.                         }  
  27.                this.Control.TextChanged += (sender, arg) => {  
  28.                             var selectedDate = arg.Text.ToString();  
  29.                             if (selectedDate == element.Placeholder) {  
  30.                                 Control.Text = DateTime.Now.ToString("dd/MM/yyyy");  
  31.                             }  
  32.                         };  
  33.                     }  
  34.                 }  
  35.             }  
The call to the base class's OnElementChanged method instantiates an Android DatePicker, with a reference to the control being assigned to the renderer's property. And the assigned below properties and events are for DatePicker.

Properties

  1. BorderWidth
  2. SetStroke
  3. SetCornerRadius
  4. SetColor
  5. SetPadding
  6. Placeholder text

Events

  1. TextChanged
Output:


Xamarin.iOS

In iOS project, create a class and add the code like below

DatePickerCtrlRenderer.cs

  1. using System;  
  2. using DatyePickerDefaultTextDemo.CustomControls;  
  3. using DatyePickerDefaultTextDemo.iOS;  
  4. using UIKit;  
  5. using Xamarin.Forms;  
  6. using Xamarin.Forms.Platform.iOS;  
  7. [assembly: ExportRenderer(typeof(DatePickerCtrl), typeof(DatePickerCtrlRenderer))]  
  8. namespace DatyePickerDefaultTextDemo.iOS {  
  9.     public class DatePickerCtrlRenderer: DatePickerRenderer {  
  10.         protected override void OnElementChanged(ElementChangedEventArgs < DatePicker > e) {  
  11.             base.OnElementChanged(e);  
  12.             if (this.Control == null
  13.                  return;  
  14.             var element = e.NewElement as DatePickerCtrl;  
  15.             if (!string.IsNullOrWhiteSpace(element.Placeholder)) {  
  16.                 Control.Text = element.Placeholder;  
  17.             }  
  18.            Control. BorderStyle = UITextBorderStyle.RoundedRect;  
  19.            Control.Layer.BorderColor = UIColor.From#533f95.CGColor;   
  20.            Control.Layer.CornerRadius = 10;  
  21.            Control.Layer.BorderWidth = 1f;  
  22.            Control.AdjustsFontSizeToFitWidth = true;  
  23.            Control.TextColor =  UIColor.From#533f95;   

  24.            Control.ShouldEndEditing +=(textField) => {  
  25.                 var seletedDate = (UITextField) textField;  
  26.                 var text = seletedDate.Text;  
  27.                 if (text == element.Placeholder) {  
  28.                     Control.Text = DateTime.Now.ToString("dd/MM/yyyy");  
  29.                 }  
  30.                 return true;  
  31.             };  
  32.         }  
  33.         private void OnCanceled(object sender, EventArgs e) {  
  34.             Control.ResignFirstResponder();  
  35.         }  
  36.         private void OnDone(object sender, EventArgs e) {  
  37.             Control.ResignFirstResponder();  
  38.         }  
  39.     }  
  40. }  
The call to the base class's method OnElementChanged instantiates an iOS control UIDatePicker, with a reference to the control being assigned to the renderer's property Control. And the assigned below properties and Events are for DatePicker.

Properties

  1. BorderStyle
  2. BorderColor
  3. CornerRadius
  4. BorderWidth
  5. Placeholder text

Events

  1. ShouldEndEditing
  2. OnDone
  3. OnCanceled
Output:
 

Note

If you want to set Placeholder text, CornerRadius, and BorderStyle for TimePicker follow the steps like above.

Common properties for every Control in Xamarin.Forms CustomRenderer.

Android

  1. this.Control.SetTextColor(Android.Graphics.Color.#533f95);  
  2. this.Control.SetBackgroundColor(Android.Graphics.Color.Transparent);  
  3. this.Control.SetPadding(20,0,0,0);  
  4. GradientDrawable gd = new GradientDrawable();  
  5. SetCornerRadius(25); //increase or decrease to changes the corner look  
  6. SetColor(Android.Graphics.Color.Transparent);  
  7. SetStroke(3, Android.Graphics.Color.#533f95);  
  8. this.Control.SetBackgroundDrawable(gd);  

iOS

  1. Control.BorderStyle=UITextBorderStyle.RoundRect;
  2. Control.Layer.BorderColor=UIColor.From#533f95.CGColor;
  3. Control.Layer.CornerRadius=10;
  4. Control.Layer.BorderWidth=1f;
  5. Control.AdjustsFontSizeToFitWidth=true;
  6. Control.TextColor=UIColor.From#533f95; 

Please, download the sample from here.


Similar Articles