Reputation: 2967
I'm working on a Xamarin Android app in C#, and I'm trying to implement a TimePicker dialog, in which the user chooses a given time, and they will be notified every day (or the days in the week that he chooses).
I understand There is a TimePickerDialog Control that was deprecated, and that we need to use now a Fragment Dialog. If anyone can help me, I'd very much appreciate it.
Thanks!
Upvotes: 0
Views: 3746
Reputation: 21
Here is a sample of how I implemented the TimePickerDialog
Declare the EditText Control (you can just use a TextView)
EditText startTimeInput = FindViewById(Resource.Id.newEventStartTimeInput);
Create a TimePickerFragment by extending the DialogFragment and handle OnCreate and OnTimeSet methods
//Time Picker
public class TimePickerFragment : DialogFragment,
TimePickerDialog.IOnTimeSetListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "Y:" + typeof(TimePickerFragment).Name.ToUpper();
// Initialize this value to prevent NullReferenceExceptions.
Action<TimeSpan> _timeSelectedHandler = delegate { };
public static TimePickerFragment NewInstance(Action<TimeSpan> onTimeSet)
{
TimePickerFragment frag = new TimePickerFragment();
frag._timeSelectedHandler = onTimeSet;
return frag;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
Calendar c = Calendar.Instance;
int hour = c.Get(CalendarField.HourOfDay);
int minute = c.Get(CalendarField.Minute);
bool is24HourView = true;
TimePickerDialog dialog = new TimePickerDialog(Activity,
this,
hour,
minute,
is24HourView);
return dialog;
}
public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
{
//Do something when time chosen by user
TimeSpan selectedTime = new TimeSpan(hourOfDay, minute, 00);
Log.Debug(TAG, selectedTime.ToString());
_timeSelectedHandler(selectedTime);
}
}
Show Fragment
private void StartTimeInput_Click(object sender, EventArgs e)
{
TimePickerFragment frag = TimePickerFragment.NewInstance(delegate (TimeSpan time)
{
startTimeInput.Text = time.ToString();
});
frag.Show(FragmentManager, TimePickerFragment.TAG);
}
Hope this helps!
Upvotes: 1
Reputation: 30883
Here is sample code from project:
class TimePickerFragment : DialogFragment
{
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
return new TimePickerDialog(Activity, (sender, args) =>
{
//args contains new time
}, DateTime.Now.Hour, DateTime.Now.Minute, true);
}
}
Upvotes: 3
Reputation: 13176
Your best bet would be to extend DialogFragment
and create your own TimePickerFragment
from it. Android even has a whole doc on the topic:
http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker
(Note: You will have to convert the respective Java -> C# but that shouldn't be too much of an issue)
public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
Calendar c = Calendar.Instance;
int hour = c.Get(CalendarField.HourOfDay);
int minute = c.Get(CalendarField.Minute);
return new TimePickerDialog(Activity, this, hour, minute, DateFormat.Is24HourFormat(Activity));
}
public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
{
//Do something when time chosen by user
}
}
Upvotes: 3