Time Picker In a 24 Hour Format In Xamarin.Forms

Introduction

This article describes how we can set a 24 hour format for TimePicker Control. Sometimes, we may get the requirement to set 24 hours for TimePicker. So in this article, we can learn how to achieve this functionality using CustomRenderer.

Requirements

  • This article 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 for Mac. And click on New Project.

Xamarin

After that, we need to select whether you're doing 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., TimePickerDemo.

Xamarin

Note

In the above screen under Shared Code, select Portable class Library or Use Shared Library.

Then, click on Next Button and the following screenshot 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 RoundedCornerViewDemo Xamarin.Forms project like below.

Xamarin

And the project structure will be:

  • TimePickerDemo:  It is for Shared Code
  • TimePickerDemo.Droid:  It is for Android.
  • iTimePickerDemo.OS:  It is for iOS
Xamarin

We need to follow the below steps to set 24 hours for TimePicker.

Portable Class Library (PCL)

Step 1

In PCL, create a class name CustomTimePicker24H which should inherit any TimePicker like below.

CustomTimePicker24H.cs

  1. using System;  
  2. using Xamarin.Forms;  
  3. namespace TimePickerDemo  
  4. {  
  5. public class CustomTimePicker24H : TimePicker  
  6. {

  7. }   
Step 2  

Create your own Xaml page named TimePicker24HPage.xaml, and make sure to refer to  "CustomTimePicker24H" 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 "CustomTimePicker24H" renderer class can be consumed by a Xaml page:

TimePicker24HPage.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:TimePickerDemo.CustomControls;assembly=TimePickerDemo" 
  6. x:Class="TimePickerDemo.Views.TimePicker24HPage">  
  7.     <ContentPage.Content>  
  8.         <StackLayout Padding="30,80,30,0" Spacing="40" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">  
  9.             <Label Text="TimePicker 24H Format" HorizontalOptions="CenterAndExpand" TextColor="Gray" FontSize="20" />  
  10.             <custom:CustomTimePicker24H TextColor="Gray" Format="HH:mm" HeightRequest="40" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" /> </StackLayout>  
  11.     </ContentPage.Content>  
  12. </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.

TimePicker24HPage.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Xamarin.Forms;  
  4. namespace TimePickerDemo.Views {  
  5.     public partial class TimePicker24HPage: ContentPage {  
  6.         public TimePicker24HPage() {  
  7.             InitializeComponent();  
  8.         }  
  9.     }  

Xamarin.Andriod

CustomTimePicker24HRenderer.cs

  1. using System;  
  2. using Android.Widget;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using Android.App;  
  6. using Android.Runtime;  
  7. using TimePickerDemoDroid;  
  8. using TimePickerDemoDroid.CustomControls;  
  9. [assembly: ExportRenderer(typeof(CustomTimePicker24H), typeof(CustomTimePicker24HRenderer))]  
  10. namespace TimePickerDemoDroid.Droid {  
  11.     public class CustomTimePicker24HRenderer: ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText> , TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable {  
  12.         private TimePickerDialog dialog = null;  
  13.         protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e) {  
  14.             base.OnElementChanged(e);  
  15.             this.SetNativeControl(new Android.Widget.EditText(Forms.Context));  
  16.             this.Control.Click += Control_Click;  
  17.             this.Control.Text = DateTime.Now.ToString("HH:mm");  
  18.             this.Control.KeyListener = null;  
  19.             this.Control.FocusChange += Control_FocusChange;  
  20.         }  
  21.         void Control_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e) {  
  22.             if (e.HasFocus) 
  23.                ShowTimePicker();  
  24.         }  
  25.         void Control_Click(object sender, EventArgs e) {  
  26.             ShowTimePicker();  
  27.         }  
  28.         private void ShowTimePicker() {  
  29.             if (dialog == null) {  
  30.                 dialog = new TimePickerDialog(Forms.Context, this, DateTime.Now.Hour, DateTime.Now.Minute, true);  
  31.             }  
  32.             dialog.Show();  
  33.         }  
  34.         public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute) {  
  35.             var time = new TimeSpan(hourOfDay, minute, 0);  
  36.             this.Element.SetValue(Xamarin.Forms.TimePicker.TimeProperty, time);  
  37.             this.Control.Text = time.ToString(@ "hh\:mm");  
  38.         }  
  39.     }  

Here OnElementChanged method instantiates an Android UI Layout  and in that I assigned the current time to TimePicker and set 24h format.

Xamarin.iOS

In iOS project, create a class name is CustomTimePicker24HRenderer and make sure to add renderer registration for our CustomTimePicker24H class in above of the namespace:

CustomTimePicker24HRenderer.cs

  1. using System;  
  2. using Xamarin.Forms.Platform.iOS;  
  3. using UIKit;  
  4. using Xamarin.Forms;  
  5. using TimePickerDemo.iOS;  
  6. using Foundation;  
  7. using TimePickerDemo.CustomControls;  
  8. [assembly: ExportRenderer(typeof(CustomTimePicker24H), typeof(CustomTimePicker24HRenderer))]  
  9. namespaceiOS {  
  10.     public class CustomTimePicker24HRenderer: TimePickerRenderer {  
  11.         protected override void OnElementChanged(ElementChangedEventArgs < TimePicker > e) {  
  12.             base.OnElementChanged(e);  
  13.             var timePicker = (UIDatePicker)Control.InputView;  
  14.             timePicker.Locale = new NSLocale("no_nb");  
  15.             if (Control != null) {  
  16.                 Control.Text = DateTime.Now.ToString("HH:mm");  
  17.             }  
  18.         }  
  19.     }  
  20. }  

Here, the OnElementChanged method instantiates an iOS UI, in that I assigned current time to the Control and set a 24 hour format using NSLocale.

Output:

Please, download the source code from here. 
 
       


Similar Articles