Community,
for quite some time now i've been trying to disable the positive Button of a TimePicker in Xamarin until the user picked a valid time. I figured that i'll have to use a custom renderer to archive this. At the moment I'm trying to get it to work on android, however since I'll have to implement it on IOs aswell solutions for both platforms would be much appreciated (so far I only researched for android solutions though since I can't test it on IOs at the moment anyway).
I do now how I can disable the positive Button of a dialog in android, however I can't find the Timepicker dialog inside TimePickerRenderer. I also tried using this code where I can easily disable the positive Button. The problem here is that once the user picks an hour only the minutes are changable anymore. So the first time the Timepicker is used it looks just normal but afterwards one can only change the minutes anymore.
Code:
public class MyTimePickerRenderer : 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.SetOnShowListener(new OnTimeShowListener());
}
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");
}
}
OnShowListener():
public class OnTimeShowListener : Java.Lang.Object, IDialogInterfaceOnShowListener
{
public void OnShow(IDialogInterface dialog)
{
((TimePickerDialog)dialog).GetButton((int)DialogButtonType.Positive).Enabled = false;
}
}
If anyone could help me that would be much appreciated!
Best regards, Tobias