The past few days I’ve been experimenting with time pickers in Xamarin.Android. I've followed these two links:
How to update text-view with multiple datepickers
http://blog.falafel.com/31-days-of-xamarin-android-day-15-controls-datetime-controls/
But I’m now a little stuck.
I’ve two time pickers in the same activity. I want to choose a start time and an end time. But when I set the time of one of the time picker in the activity it automatically updates the other one.
What I want is set one time picker without updating the other one. Below is what I’ve tried:
ReservationHoursActivity.cs
public class ReservationHoursActivity : Activity, TimePickerDialog.IOnTimeSetListener
{
private TextView time_display_Start;
private Button pick_button_Start;
private TextView time_display_End;
private Button pick_button_End, submitButtonMyslot;
private int hour;
private int minute;
const int START_TIME_DIALOG_ID = 0;
const int END_TIME_DIALOG_ID = 1;
const int TIME_PICKER_TO = 0;
const int TIME_PICKER_FROM = 1;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.ReservationHoursLayout);
time_display_Start = FindViewById<TextView> (Resource.Id.timeDisplayStart);
pick_button_Start = FindViewById<Button> (Resource.Id.btnPickTimeStart);
time_display_End = FindViewById<TextView> (Resource.Id.timeDisplayEnd);
pick_button_End = FindViewById<Button> (Resource.Id.btnPickTimeEnd);
submitButtonMyslot = FindViewById<Button> (Resource.Id.btnSubmitSlot);
submitButtonMyslot.Click += SubmitButtonMyslot_Click;
pick_button_Start.Click += delegate {
ShowTimePickerDialog (START_TIME_DIALOG_ID);
};
pick_button_End.Click += delegate {
ShowTimePickerDialog (END_TIME_DIALOG_ID);
};
hour = DateTime.Now.Hour;
minute = DateTime.Now.Minute;
UpdateDisplayStart (hour, minute);
UpdateDisplayEnd (hour, minute);
}
void ShowTimePickerDialog (int pickerID)
{
switch (pickerID) {
case TIME_PICKER_FROM:
var dialogStart = new TimePickerFragment (this, hour, minute, this);
dialogStart.Show (FragmentManager, null);
break;
case TIME_PICKER_TO:
var dialogEnd = new TimePickerFragment (this, hour, minute, this);
dialogEnd.Show (FragmentManager, null);
break;
}
}
public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
{
UpdateDisplayEnd (hourOfDay, minute);
UpdateDisplayStart (hourOfDay, minute);
}
void UpdateDisplayStart (int selectedHours, int selectedMinutes)
{
time_display_Start.Text = selectedHours + ":" + selectedMinutes;
}
void UpdateDisplayEnd (int selectedHours, int selectedMinutes)
{
time_display_End.Text = selectedHours + ":" + selectedMinutes;
}
}
TimePickerFragment.cs
public class TimePickerFragment:DialogFragment
{
private readonly Context context;
private int hour;
private int minute;
private readonly TimePickerDialog.IOnTimeSetListener listener;
public TimePickerFragment (Context context, int hour, int minute, TimePickerDialog.IOnTimeSetListener listener)
{
this.context = context;
this.hour = hour;
this.minute = minute;
this.listener = listener;
}
public override Dialog OnCreateDialog (Bundle savedState)
{
var dialog = new TimePickerDialog (context, listener, hour, minute, false);
return dialog;
}
}
I don’t know how I can update the two time pickers separately. I’ve also tried to put the OnTimeSet method inside this:
private TimePickerDialog.IOnTimeSetListener From_Time_Listener = new TimePickerDialog.IOnTimeSetListener()
{
public void OnTimeSet (TimePicker view, int hourOfDay, int minute)
{
UpdateDisplayEnd (hourOfDay, minute);
}
};
But that doesn’t work. It is not allowing me to put that method inside. Besides, other variables and methods will be unexpected if I do that.