Xamarin forms TabbedPage - xamarin

I am using Tabbed page in my xamarin forms project.
I am trying to use OnTabReselected event in MyTabsRenderer Class in android. But the events like OnTabSelected, OnTabReselected and OnTabUnselected are not getting called. Does anyone have any solution for this.
xamarin forms version: 3.2.0.871581
VS version : 15.8.6
Here is my code snippet(MyTabsRenderer Class):
using Android.OS;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;
using MyProject;
using MyProject.Droid;
using Xamarin.Forms.Platform.Android;
using System.ComponentModel;
using Android.Support.V4.View;
using Android.Content.Res;
using Android.Content;
using Android.Support.Design.Widget;
[assembly: ExportRenderer(typeof(TabController), typeof(MyTabsRenderer))]
namespace MyProject.Droid
{
public class MyTabsRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
{
bool setup;
ViewPager viewPager;
TabLayout tabLayout;
public MyTabsRenderer(Context context) : base(context)
{
}
protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
{
base.SetTabIcon(tab, icon);
}
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
//if (setup)
// return;
if (e.PropertyName == "Renderer" || e.PropertyName == "CurrentPage")
{
viewPager = (ViewPager)ViewGroup.GetChildAt(0);
tabLayout = (TabLayout)ViewGroup.GetChildAt(1);
setup = true;
ColorStateList colors = GetTabColor();
for (int i = 0; i < tabLayout.TabCount; i++)
{
var tab = tabLayout.GetTabAt(i);
SetTintColor(tab, colors);
}
tabLayout.SetOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
}
void OnTabReselected(TabLayout.Tab tab)
{
// To have the logic only on he tab on position 1
if (tab == null || tab.Position != 1)
{
return;
}
if (tab.Text == "Play")
{
tab.SetText("Pause");
tab.SetIcon(Resource.Drawable.icon);
// App.pauseCard = false;
}
else
{
tab.SetText("Play");
tab.SetIcon(Resource.Drawable.icon);
// App.pauseCard = true;
}
SetTintColor(tab, GetTabColor());
}
void SetTintColor(TabLayout.Tab tab, ColorStateList colors)
{
var icon = tab?.Icon;
if (icon != null)
{
icon = Android.Support.V4.Graphics.Drawable.DrawableCompat.Wrap(icon);
Android.Support.V4.Graphics.Drawable.DrawableCompat.SetTintList(icon, colors);
}
}
ColorStateList GetTabColor()
{
return ((int)Build.VERSION.SdkInt >= 23)
? Resources.GetColorStateList(Resource.Color.icon_tab, Forms.Context.Theme)
: Resources.GetColorStateList(Resource.Color.icon_tab);
}
}
}

It is probably because you are setting the OnTabSelectedListener wrong.
Try setting the listener to this instead: tabLayout.SetOnTabSelectedListener(this);
Also for inspiration check out TabbedPageRenderer.cs source code,

Related

How to keep soft keyboard always open in Xamarin Forms

I am working in a Xamarin Forms APP, we want to go through different entries (not using TabIndex because we need a custom logic), but we want to keep the keyboard always on during looping through entries. We are using ReturnCommand to Focus the NEXT entry.
Create a CustomEntryRenderer in your Android project :
[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace your namespace
class CustomEntryRenderer:EntryRenderer
{
public CustomEntryRenderer(Context context):base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
}
}
and add this in your MainActivity:
private bool _lieAboutCurrentFocus;
public override bool DispatchTouchEvent(MotionEvent ev)
{
var focused = CurrentFocus;
bool customEntryRendererFocused = focused != null && focused.Parent is CustomEntryRenderer;
_lieAboutCurrentFocus = customEntryRendererFocused;
var result = base.DispatchTouchEvent(ev);
_lieAboutCurrentFocus = false;
return result;
}
public override Android.Views.View CurrentFocus
{
get
{
if (_lieAboutCurrentFocus)
{
return null;
}
return base.CurrentFocus;
}
}

Admob native ads with xamarin: 'Ad failed to load : 0'

This issue only occurs with my implementation of native ads. Banner ads work fine. I adapted the custom renderers from this blog to work with native ads. I used test ad unit IDs.
Here is my custom renderer for Android:
[assembly: ExportRenderer(typeof(NativeAdView), typeof(NativeRendererAndroid))]
namespace XXX.Droid
{
public class NativeRendererAndroid : ViewRenderer<NativeAdView, AdView>
{
public NativeRendererAndroid(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<NativeAdView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && Control == null)
SetNativeControl(CreateAdView());
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(AdView.AdUnitId))
Control.AdUnitId = Element.AdUnitId;
}
private AdView CreateAdView()
{
var adView = new AdView(Context)
{
AdSize = new AdSize(AdSize.FullWidth, AdSize.AutoHeight),
AdUnitId = Element.AdUnitId
};
adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
adView.LoadAd(new AdRequest.Builder().Build());
return adView;
}
}
}
And for iOS:
[assembly: ExportRenderer(typeof(NativeAdView), typeof(NativeRendereriOS))]
namespace XXX.iOS
{
public class NativeRendereriOS : ViewRenderer<NativeAdView, NativeExpressAdView>
{
protected override void OnElementChanged(ElementChangedEventArgs<NativeAdView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(CreateNativeAd());
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(NativeExpressAdView.AdUnitID))
Control.AdUnitID = Element.AdUnitId;
}
private NativeExpressAdView CreateNativeAd()
{
AdSize adSize = new AdSize();
adSize.Size = new CGSize(UIScreen.MainScreen.Bounds.Size.Width, 49);
var nativeAd = new NativeExpressAdView(adSize)
{
AdUnitID = Element.AdUnitId,
RootViewController = GetVisibleViewController()
};
nativeAd.LoadRequest(GetRequest());
Request GetRequest()
{
var request = Request.GetDefaultRequest();
return request;
}
return nativeAd;
}
private UIViewController GetVisibleViewController()
{
var windows = UIApplication.SharedApplication.Windows;
foreach (var window in windows)
{
if (window.RootViewController != null)
{
return window.RootViewController;
}
}
return null;
}
}
}
These custom renderers are very similar to the ones linked in the blog above. Perhaps there is something I need to include for native ads that is not used for banner ads?
Once I get the ad loading correctly, I would like to put it in a ListView, but for now I just have it as a View.
Edit: For clarification, when using the above custom renderers, everything builds successfully, but when the ad tries to load, 'Ad failed to load : 0' gets printed in the debug and the NativeAdView remains transparent. This is both both platforms.

Removing Padding from Entry

So Entry does not have a padding attribute, however there is some definite padding that goes on the Entry.
Example
I have the "Michigan" Entry lined up with the "Select" Label below, however they look misaligned because the entry has some padding to the left. I tried the margin attribute that entry does have, however it did not work.
How do I get rid of that gap/padding?
I'd like to add that adding an offset margin does not working.
You need to make a custom renderer for the entry and set the Android EditText's PaddingLeft to 0 using the SetPadding method.
Excerpt from CustomEntryRenderer on Android:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement == null) return;
Control.SetPadding(0, Control.PaddingTop, Control.PaddingRight, Control.PaddingBottom);
}
For me the custom render that worked was:
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyApp.Views.Controls.CustomEntry), typeof(MyApp.Droid.Views.Controls.CustomRenderer.Android.CustomEntryRenderer))]
namespace MyApp.Droid.Views.Controls
{
namespace CustomRenderer.Android
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Background = new ColorDrawable(Color.Transparent);
Control.SetPadding(0, 0, 0, 0);
Control.Gravity = GravityFlags.CenterVertical | GravityFlags.Left;
Control.TextAlignment = TextAlignment.Gravity;
}
}
}
}
}
I was struggling with the same issue, but I solved it by creating a custom Entry type which adds a Padding property to Xamarin Forms' Entry:
public class CustomEntry : Entry
{
public static readonly BindableProperty PaddingProperty =
BindableProperty.Create(
nameof(Padding),
typeof(Thickness),
typeof(CustomEntry),
new Thickness());
public Thickness Padding
{
get { return (Thickness)this.GetValue(PaddingProperty); }
set { this.SetValue(PaddingProperty, value); }
}
}
Then I render this CustomEntry with a custom renderer, just as Danilow proposed, with the only difference, that I read the PaddingProperty from CustomEntry and apply it in the CustomEntryRenderer.
[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace CrossPlatformLibrary.Forms.Android.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
{
return;
}
if (this.Element is CustomEntry customEntry)
{
var paddingLeft = (int)customEntry.Padding.Left;
var paddingTop = (int)customEntry.Padding.Top;
var paddingRight = (int)customEntry.Padding.Right;
var paddingBottom = (int)customEntry.Padding.Bottom;
this.Control.SetPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
}
}
}
Beware: This code needs to be extended if you want to react on PaddingProperty changes - AND - you will need to write a custom renderer for IOS if you want to support the Padding property there too.
Here's the same thing implemented on iOS for anyone who needs it there too. It's basically setting the left view to have 0 width that does it, but you can also play with the "LeftViewMode" to hide it completely.
this.Control.LeftView = new UIView(new CGRect(0, 0, 0, this.Control.Frame.Height));
Full code
using CoreGraphics;
using UIKit;
using YourNamespace.iOS.CustomRenderers;
using YourNamespace.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(BorderlessEntry), typeof(BorderlessEntryRenderer))]
namespace YourNamespace.iOS.CustomRenderers
{
public class BorderlessEntryRenderer : Xamarin.Forms.Platform.iOS.EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (this.Control != null)
{
this.Control.LeftView = new UIView(new CGRect(0, 0, 0, this.Control.Frame.Height));
this.Control.LeftViewMode = UITextFieldViewMode.Always;
}
}
}
}

How to change Picker font colour and size in Xamarin forms?

I'm new to Xamarin and I'm currently doing a project in Xamarin Forms PCL.
Is there a way to change the font colour and size of Picker?
<Picker x:Name="pkr_color" Grid.Row="4" Grid.Column="1" HorizontalOptions="FillAndExpand"
BackgroundColor="#ededed" Title="Select Color">
<Picker.Items>
<x:String>Red</x:String>
<x:String>Blue</x:String>
<x:String>Green</x:String>
</Picker.Items>
</Picker>
Thanks in advance!
You will need to write a custom renderer for each platform.
using System;
using Project.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof (Picker), typeof (CustomPickerRenderer))]
namespace Project.iOS
{
public class CustomPickerRenderer : PickerRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.TextColor = UIKit.UIColor.White;
}
}
}
}
Here is an example for iOS. This would change the color of the text, you will need to do something similar for Android, and just add your font sizing change as well.
Font size of a picker can be changed with PCL code.
Create MainPage.xaml file
<Picker x:Name="PickerList" Title="Select Any One" IsVisible="False" SelectedIndexChanged="PickerList_SelectedIndexChanged">
<Picker.Items>
<x:String>Option 1</x:String>
<x:String>Option 2</x:String>
<x:String>Option 3</x:String>
</Picker.Items>
</Picker>
<Label x:Name="PickerLabel" Text="Tap to Select Picker" FontSize="14" HorizontalOptions="Start">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
Create MainPage.xaml.cs file
private void PickerList_SelectedIndexChanged(object sender, EventArgs e)
{
PickerLabel.Text = PickerList.Items[PickerList.SelectedIndex];
// PickerLabel.Text = PickerList.SelectedItem.ToString() ;
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
PickerList.Focus();
}
this solves the problem for Android and IOS.
I Hope Below Code Helpful to Get Your TextColor
**In Xaml**
<Picker SelectedIndexChanged="OnColorPickerSelected" TextColor="{Binding TextColor}"/>
**In Code Behind**
private void OnColorPickerSelected(object sender, EventArgs e)
{
((ViewModel)BindingContext).Color= pkr_color.Items[pkr_color.SelectedIndex];
ChooseColorPickerTextColor(((ViewModel)BindingContext).Color, pkr_color);
}
**Implement ChooseColorPickerTextColor Method Here**
private void ChooseColorPickerTextColor(string selectedColor, Picker pickerName)
{
Picker colorPickerTextColor = pickerName;
if (selectedColor == "Red")
{
colorPickerTextColor.TextColor = Color.Red;
}
else if (selectedColor == "Yellow")
{
colorPickerTextColor.TextColor = Color.Yellow;
}
else if (selectedColor == "Green")
{
colorPickerTextColor.TextColor = Color.Green;
}
else if (selectedColor == "Blue")
{
colorPickerTextColor.TextColor = Color.Blue;
}
else if (selectedColor == "Maroon")
{
colorPickerTextColor.TextColor = Color.Maroon;
}
else if (selectedColor == "Pink")
{
colorPickerTextColor.TextColor = Color.Pink;
}
else
{
colorPickerTextColor.TextColor = Color.Aqua;
}
}
By using "WidthRequest" We can Increase size of the picker
For changing typeface, size, underline, text, textcolor, alert dialog button position, button text in Android native numberpicker (xamarin form picker), you can handle it with a custom render like this:
using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;
using Android.Graphics;
[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace Daddy.Droid
{
public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
{
Typeface fontFace = null;
private IElementController ElementController => Element as IElementController;
private AlertDialog _dialog;
public MyPickerRenderer(Context context) : base(context)
{
AutoPackage = false;
}
[Obsolete("This constructor is obsolete as of version 2.5. Please use PickerRenderer(Context) instead.")]
public MyPickerRenderer()
{
AutoPackage = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.NewElement == null || e.OldElement != null || Control == null)
return;
fontFace = Typeface.CreateFromAsset(this.Context.Assets, "somefont.ttf");
GradientDrawable gd = new GradientDrawable();
gd.SetStroke(0, Android.Graphics.Color.Transparent);
Control.SetBackground(gd);
Control.TextSize = 14f;
Control.SetTypeface(fontFace, TypefaceStyle.Normal);
Control.Click += Control_Click;
}
protected override void Dispose(bool disposing)
{
Control.Click -= Control_Click;
base.Dispose(disposing);
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
NumberPicker picker = new NumberPicker(Context);
int count = picker.ChildCount;
for (int i = 0; i < count; i++)
{
Android.Views.View v = picker.GetChildAt(i);
if(v.GetType() == typeof(EditText))
{
Java.Lang.Reflect.Field field = picker.Class.GetDeclaredField("mSelectorWheelPaint");
field.Accessible = true;
((Paint)field.Get(picker)).SetTypeface(fontFace);
((EditText)v).SetTypeface(fontFace, TypefaceStyle.Normal);
picker.Invalidate();
}
}
if (model.Items != null && model.Items.Any())
{
picker.MaxValue = model.Items.Count - 1;
picker.MinValue = 0;
picker.SetDisplayedValues(model.Items.ToArray());
picker.WrapSelectorWheel = false;
picker.DescendantFocusability = DescendantFocusability.BlockDescendants;
picker.Value = model.SelectedIndex;
picker.Visibility = ViewStates.Visible;
}
var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
layout.Visibility = ViewStates.Visible;
layout.AddView(picker);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton("Cancel", (s, a) =>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
_dialog = null;
});
builder.SetPositiveButton("This One", (s, a) =>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
if (Element != null)
{
if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
Control.Text = model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
}
_dialog = null;
});
_dialog = builder.Create();
_dialog.DismissEvent += (ssender, args) =>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
};
_dialog.Show();
Android.Widget.Button nbutton = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
nbutton.SetTypeface(fontFace, TypefaceStyle.Normal);
nbutton.SetTextColor(Android.Graphics.Color.ParseColor("#33b5e5"));
nbutton.TextSize = 16f;
LinearLayout layOut = (LinearLayout)nbutton.Parent;
layOut.SetGravity(GravityFlags.CenterHorizontal);
Android.Views.View v1 = layOut.GetChildAt(1);
v1.Visibility = ViewStates.Gone;
int res = Resources.GetIdentifier("alertTitle", "id", "android");
TextView textView = (TextView)_dialog.FindViewById(res);
textView.SetTextColor(Android.Graphics.Color.Gray);
textView.SetTypeface(fontFace, TypefaceStyle.Normal);
textView.Gravity = GravityFlags.Center;
}
}
}
Put the Label and Picker in the same Grid cell.Don't set Title of the picker instead the Text of the Label will work as Title.
<Label x:Name="PickerLabel" Text="Picker Title" TextColor="Any Color"></Label>
<Picker x:Name="Picker" SelectedIndexChanged="Picker_SelectedIndexChanged" TextColor="Any Color" />
Now make the Text of Label Invisible when an Item is selected from Picker.
void Picker_SelectedIndexChanged(object sender, System.EventArgs e)
{
PickerLabel.IsVisible = false;
}

Xamarin Forms timepicker 24hour

I'm new to Xamarin.Forms and i can't seems to find how to show the dialog in a 24hour format instead of a AM/PM
can someone help me ?
Thank you
This page is really usefull to learn how to format dates in C#:
https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
And with this code I was able to show a 24 hour format:
TimePicker timePicker = new TimePicker
{
Format = "HH:mm"
};
The only solution i found is here:
24h timepicker
Basically a custom renderer within each platform project will be needed.
Xamarin.iOS:
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using PersonalTrainer.iOS.View.Controls;
[assembly: ExportRenderer (typeof (TimePicker), typeof (TimePicker24HRenderer))]
namespace YourNamespace.iOS.View.Controls {
public class TimePicker24HRenderer : TimePickerRenderer {
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e) {
base.OnElementChanged(e);
var timePicker = (UIDatePicker)Control.InputView;
timePicker.Locale = new NSLocale("no_nb");
}
}
}
For Android:
[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePicker24HRenderer))]
namespace YourNamespace.Droid.View.Controls {
public class TimePicker24HRenderer : ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable {
private TimePickerDialog dialog = null;
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e) {
base.OnElementChanged(e);
this.SetNativeControl(new Android.Widget.EditText(Forms.Context));
this.Control.Click += Control_Click;
this.Control.Text = DateTime.Now.ToString("HH:mm");
this.Control.KeyListener = null;
this.Control.FocusChange += Control_FocusChange;
}
void Control_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e) {
if (e.HasFocus) { ShowTimePicker(); }
}
void Control_Click(object sender, EventArgs e) {
ShowTimePicker();
}
private void ShowTimePicker() {
if (dialog == null) {
dialog = new TimePickerDialog(Forms.Context, this, DateTime.Now.Hour, DateTime.Now.Minute, true);
}
dialog.Show();
}
public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute) {
var time = new TimeSpan(hourOfDay, minute, 0);
this.Element.SetValue(TimePicker.TimeProperty, time);
this.Control.Text = time.ToString(#"hh\:mm");
}
}
}
For Windows Phone:
[assembly: ExportRenderer (typeof (MyTimePicker), typeof (TimePicker24HRenderer))]
namespace YourNamespace.WindowsPhone.View.Controls {
public class TimePicker24HRenderer : TimePickerRenderer {
// Override the OnElementChanged method so we can tweak this renderer post-initial setup
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.TimePicker> e) {
base.OnElementChanged (e);
if (Control != null) {
var nativeControl = (Windows.UI.Xaml.Controls.TimePicker)Control;
nativeControl.Foreground = new SolidColorBrush(Colors.Gray);
Control.ClockIdentifier = "24HourClock";
}
}
}
}
In XAML you can set the the 24 hours format string to TimePicker by using the Format property:
<TimePicker Format="HH:mm"/>
Ok. You have to do it plateform specific.. I did something like that.
So create an interface
public interface TimePickerInterface
{
void showTimePicker (ButtonOrLabel lbl);
}
In Android you create your timepicker class
[assembly:Dependency(typeof(yournamespace.MyTimePicker))]
namespace yournamespace
{
public class MyTimePicker:Java.Lang.Object,TimePickerInterface,Android.App.TimePickerDialog.IOnTimeSetListener
{
String originalText="";
LabelOrButton lbl=new LabelOrButton();
public void showTimePicker(LabelOrButton lbl)
{
var c = Forms.Context;
originalText = lbl.Text;
this.lbl = lbl;
var time = DateTime.Now.TimeOfDay;
TimePickerDialog dialog = new TimePickerDialog(c,this,time.Hours,time.Minutes,true);
dialog.SetCanceledOnTouchOutside (true);
dialog.Show ();
}
public void OnTimeSet (Android.Widget.TimePicker view, int hourOfDay, int minute)
{
if(view.IsShown)
lbl.Text = (hourOfDay<=9 ? ("0"+hourOfDay+""):hourOfDay+"")+":"+(minute<=9 ? ("0"+minute+""):minute+"");
}
}
}
And in your xamarin forms you call it in actionlistener of your button or gesturerecognizer of your label:
DependencyService.Get<TimePickerInterface> ().showTimePicker(myButtonOrLabel);
You can do the same thing for DatePicker..
Hope it helps..

Resources