MaterialDateTimePicker bindings for Xamarin.Android apps
Bindings for MaterialDateTimePicker for Xamarin.Android apps
One of the good libraries for Android apps made in Material design is the “Material DateTime Picker” from @wdullaer. Here is a short article how to create Xamarin.Android bindings library to consume that awesome picker in your Xamarin.Android applications.
Creating java library bindings for Xamarin.Android
There is an official guide from Xamarin: Binding a Java Library - Consuming Java libraries from C#
First, we need to get the JAR/AAR file. Looking at github page, we can see Maven banner. It means that we can grab a binary from Maven so let’s go there and do it:
Next, following the Xamarin guide, we will see 3 errors:
Vendor/MaterialDateTimePickerBinding/obj/Debug/generated/src/Com.Wdullaer.Materialdatetimepicker.Date.MonthView.cs(34,34): Error CS0533: `Com.Wdullaer.Materialdatetimepicker.Date.MonthView.MonthViewTouchHelper.GetVisibleVirtualViews(System.Collections.Generic.IList<Java.Lang.Integer>)' hides inherited abstract member `Android.Support.V4.Widget.ExploreByTouchHelper.GetVisibleVirtualViews(System.Collections.Generic.IList<Java.Lang.Integer>)' (CS0533) (MaterialDateTimePickerBinding)
Vendor/MaterialDateTimePickerBinding/obj/Debug/generated/src/Com.Wdullaer.Materialdatetimepicker.Date.MonthView.cs(36,36): Error CS0534: `Com.Wdullaer.Materialdatetimepicker.Date.MonthView.MonthViewTouchHelper' does not implement inherited abstract member `Android.Support.V4.Widget.ExploreByTouchHelper.GetVisibleVirtualViews(System.Collections.Generic.IList<Java.Lang.Integer>)' (CS0534) (MaterialDateTimePickerBinding)
Vendor/MaterialDateTimePickerBinding/obj/Debug/generated/src/Com.Wdullaer.Materialdatetimepicker.Time.Timepoint.cs(23,23): Error CS0535: `Com.Wdullaer.Materialdatetimepicker.Time.Timepoint' does not implement interface member `Java.Lang.IComparable.CompareTo(Java.Lang.Object)' (CS0535) (MaterialDateTimePickerBinding)
1st and 2nd are pretty simple. As these methods most definitely will not be consumed from C# side, we can just remove these methods from bindings.
For that we use
<remove-node /> instruction in Metadata.xml.
To solve 3rd problem we need to add a partial class to Additions folder:
Solution to this issue I found on Xamarin Forums.
As usual, very helpful were comments and examples from brendanzagaeski with different approaches to different java-to-csharp binding problems. Links to these examples are in the end of the page.
This library we are binding now has only one dependency: https://github.com/wdullaer/MaterialDateTimePicker/blob/master/library/build.gradle#L23
It is a
com.android.support:support-v4:23.1.1 which translates to Xamarin Nuget package called Xamarin.Android.Support.v4 of version 23.1.1.
So we will just add this Nuget package to our bindings project and that’s it. Clean and Build the project and you should see 0 errors. Yaay!
How to use this library (examples)
On C# side what you need to do is following:
- reference the bindings project in your app project
- in your activity implement interface OnTimeSetListener/OnDateSetListener
- create a datepicker and show it
These steps are described in the Readme.md file of the original library
Make sure you are using the same version of Xamarin.Android.Support.v4 package in your application project.
See Xamarin.Wdullaer.MaterialDateTimePicker github repository. Clone it and use it or just add as a submodule.
One thing definitely open for improvement is parameter names. Right now in many places it’s just p0, p1, p2, p3.
Naming them hour, minute, second, isAmPm would make code cleaner and easier to understand.
So if you feel like fixing something feel free to submit pull request. (Or if you know how to fix these names automaticall - let me know, please).
My other Xamarin.Android bindings:
brendanzagaeski Metadata.xml examples:
https://gist.github.com/brendanzagaeski/c32d65c21e152799af69 https://gist.github.com/brendanzagaeski/6d1052a8b76f9067548e https://gist.github.com/brendanzagaeski/69f490e31ca6a71136ff https://gist.github.com/brendanzagaeski/3868e30b85a1feb1181b https://gist.github.com/brendanzagaeski/9834034 https://gist.github.com/brendanzagaeski/9607158