0

I am Using This Code but OnDateChanged method is not called why, where am i doing mistake please help me.

public class DatePickerFragment : DialogFragment,
                              DatePickerDialog.IOnDateSetListener,DatePicker.IOnDateChangedListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();

// Initialize this value to prevent NullReferenceExceptions.
Action<DateTime> _dateSelectedHandler = delegate { };
DateTime currently = DateTime.Now;
public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
{
    DatePickerFragment frag = new DatePickerFragment();
    frag._dateSelectedHandler = onDateSelected;

    return frag;
}

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{

    DatePickerDialog dialog = new DatePickerDialog(Activity,
                                                   this,
                                                   currently.Year,
                                                   currently.Month,
                                                   currently.Day);




    return dialog;
}


public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
    // Note: monthOfYear is a value between 0 and 11, not 1 and 12!
    DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);

    _dateSelectedHandler(selectedDate);
}

public void OnDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
    if (year < currently.Year)
        view.UpdateDate(currently.Year, currently.Month, currently.Day);

    if(monthOfYear < currently.Month && monthOfYear == currently.Year)
        view.UpdateDate(currently.Year, currently.Month, currently.Day);

    if(dayOfMonth< currently.Day && year == currently.Year &&monthOfYear == currently.Month)
        view.UpdateDate(currently.Year, currently.Month, currently.Day);

}

}

I am trying to disable previous dates from date picker but date change listener is not calling . I want to hide Past dates from datepicker can you please help me how can i do that.

1 Answer 1

2

Since DatePickerDialog is a wrapper for a DatePicker, the actual DatePicker widget in this case is not getting OnDateChanged set as its listener and therefore it does not get called.

To disable dates in the DatePicker, just set its MinDate or MaxDate properties. So if you are trying to disable dates before the current date, in OnCreateDialog set the MinDate:

dialog.DatePicker.MinDate = Java.Lang.JavaSystem.CurrentTimeMillis();

You will need to use Java.Lang.JavaSystem.CurrentTimeMillis(); because you need give the date in milliseconds since January 1, 1970

3
  • I modified the Xamarin DatePicker sample with code I mentioned above. See here Can you run this project to see if you still have the same issue? What Android version are you testing on? Does it happen only on that version?
    – jimmgarr
    Jul 20, 2016 at 19:13
  • Its Working Thanks for your replay with example code. Jul 21, 2016 at 9:26
  • i have another question how to hide previous time in time picker. Jul 21, 2016 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.