Java Code Examples for android.widget.DatePicker#setMaxDate()

The following examples show how to use android.widget.DatePicker#setMaxDate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateDialogNormalizer.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void setLimits(DatePicker picker, long min, long max) {
    // DatePicker intervals are non inclusive, the DatePicker will throw an
    // exception when setting the min/max attribute to the current date
    // so make sure this never happens
    if (max <= min) {
        return;
    }
    Calendar minCal = trimToDate(min);
    Calendar maxCal = trimToDate(max);
    int currentYear = picker.getYear();
    int currentMonth = picker.getMonth();
    int currentDayOfMonth =  picker.getDayOfMonth();
    picker.updateDate(maxCal.get(Calendar.YEAR),
            maxCal.get(Calendar.MONTH),
            maxCal.get(Calendar.DAY_OF_MONTH));
    picker.setMinDate(minCal.getTimeInMillis());
    picker.updateDate(minCal.get(Calendar.YEAR),
            minCal.get(Calendar.MONTH),
            minCal.get(Calendar.DAY_OF_MONTH));
    picker.setMaxDate(maxCal.getTimeInMillis());

    // Restore the current date, this will keep the min/max settings
    // previously set into account.
    picker.updateDate(currentYear, currentMonth, currentDayOfMonth);
}
 
Example 2
Source File: DateDialogNormalizer.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void setLimits(DatePicker picker, long min, long max) {
    // DatePicker intervals are non inclusive, the DatePicker will throw an
    // exception when setting the min/max attribute to the current date
    // so make sure this never happens
    if (max <= min) {
        return;
    }
    Calendar minCal = trimToDate(min);
    Calendar maxCal = trimToDate(max);
    int currentYear = picker.getYear();
    int currentMonth = picker.getMonth();
    int currentDayOfMonth =  picker.getDayOfMonth();
    picker.updateDate(maxCal.get(Calendar.YEAR),
            maxCal.get(Calendar.MONTH),
            maxCal.get(Calendar.DAY_OF_MONTH));
    picker.setMinDate(minCal.getTimeInMillis());
    picker.updateDate(minCal.get(Calendar.YEAR),
            minCal.get(Calendar.MONTH),
            minCal.get(Calendar.DAY_OF_MONTH));
    picker.setMaxDate(maxCal.getTimeInMillis());

    // Restore the current date, this will keep the min/max settings
    // previously set into account.
    picker.updateDate(currentYear, currentMonth, currentDayOfMonth);
}
 
Example 3
Source File: CreditCardExpirationDatePickerView.java    From android-AutofillFramework with Apache License 2.0 6 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog dialog = new DatePickerDialog(getActivity(),
            R.style.CustomDatePickerDialogTheme, this, mParent.mYear, mParent.mMonth, 1);

    DatePicker datePicker = dialog.getDatePicker();

    // Limit range.
    Calendar c = mParent.getCalendar();
    datePicker.setMinDate(c.getTimeInMillis());
    c.set(Calendar.YEAR, mParent.mYear + CC_EXP_YEARS_COUNT - 1);
    datePicker.setMaxDate(c.getTimeInMillis());

    // Remove day.
    datePicker.findViewById(getResources().getIdentifier("day", "id", "android"))
            .setVisibility(View.GONE);
    return dialog;
}
 
Example 4
Source File: CreditCardExpirationDatePickerView.java    From input-samples with Apache License 2.0 6 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog dialog = new DatePickerDialog(getActivity(),
            R.style.CustomDatePickerDialogTheme, this, mParent.mYear, mParent.mMonth, 1);

    DatePicker datePicker = dialog.getDatePicker();

    // Limit range.
    Calendar c = mParent.getCalendar();
    datePicker.setMinDate(c.getTimeInMillis());
    c.set(Calendar.YEAR, mParent.mYear + CC_EXP_YEARS_COUNT - 1);
    datePicker.setMaxDate(c.getTimeInMillis());

    // Remove day.
    datePicker.findViewById(getResources().getIdentifier("day", "id", "android"))
            .setVisibility(View.GONE);
    return dialog;
}
 
Example 5
Source File: OBSetupMenu.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
void showPickDateDialog (final DatePickerDialog.OnDateSetListener listener, Date startDate)
{
    Calendar currentCalendar = Calendar.getInstance();
    if (startDate != null)
    {
        currentCalendar.setTime(startDate);
    }
    final Calendar calendar = currentCalendar;
    DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    //
    d.setCancelable(false);
    d.setCanceledOnTouchOutside(false);
    //
    LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.getWindow().clearFlags(Window.FEATURE_ACTION_BAR);
    d.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    d.setCustomTitle(linearLayout);
    //
    d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick (DialogInterface dialog, int which)
        {
            MainActivity.log("OBSetupMenu:showPickDateDialog:cancelled!");
            loadHomeScreen();
        }
    });
    //
    DatePicker datePicker = d.getDatePicker();
    calendar.clear();
    calendar.set(2017, Calendar.JANUARY, 1);
    datePicker.setMinDate(calendar.getTimeInMillis());
    calendar.clear();
    calendar.set(2025, Calendar.DECEMBER, 31);
    datePicker.setMaxDate(calendar.getTimeInMillis());
    //
    d.show();
}
 
Example 6
Source File: CVDatePreference.java    From callmeter with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected View onCreateDialogView() {
    dp = new DatePicker(getContext());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        dp.setCalendarViewShown(true);
        dp.setSpinnersShown(false);
        dp.setMaxDate(System.currentTimeMillis());
    }
    updateDialog();
    return dp;
}
 
Example 7
Source File: DateDialogNormalizer.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static void setLimits(DatePicker picker, long currentMillisForPicker,
        long minMillisForPicker, long maxMillisForPicker) {
    // On Lollipop only (not KitKat or Marshmallow), DatePicker has terrible performance for
    // large date ranges. This causes problems when the min or max date isn't set in HTML, in
    // which case these values default to the min and max possible values for the JavaScript
    // Date object (1CE and 275760CE). As a workaround, limit the date range to 5000 years
    // before and after the current date. In practice, this doesn't limit users since scrolling
    // through 5000 years in the DatePicker is highly impractical anyway. See
    // http://crbug.com/441060
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
            || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
        final long maxRangeMillis = 5000L * 365 * 24 * 60 * 60 * 1000;
        minMillisForPicker = Math.max(minMillisForPicker,
                currentMillisForPicker - maxRangeMillis);
        maxMillisForPicker = Math.min(maxMillisForPicker,
                currentMillisForPicker + maxRangeMillis);
    }

    // On KitKat and earlier, DatePicker requires the minDate is always less than maxDate, even
    // during the process of setting those values (eek), so set them in an order that preserves
    // this invariant throughout.
    if (minMillisForPicker > picker.getMaxDate()) {
        picker.setMaxDate(maxMillisForPicker);
        picker.setMinDate(minMillisForPicker);
    } else {
        picker.setMinDate(minMillisForPicker);
        picker.setMaxDate(maxMillisForPicker);
    }
}
 
Example 8
Source File: DateRangeDialogFragment.java    From Hews with MIT License 5 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Inflate your custom layout containing 2 DatePickers
    View customView = getActivity().getLayoutInflater().inflate(R.layout.dialog_time_range, null);
    builder.setView(customView); // Set the view of the dialog to your custom layout
    // Define your date pickers
    final DatePicker dpStart = (DatePicker) customView.findViewById(R.id.dpStartDate);
    final DatePicker dpEnd = (DatePicker) customView.findViewById(R.id.dpEndDate);
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
    long curTime = c.getTimeInMillis();
    dpStart.setMaxDate(curTime);
    dpEnd.setMaxDate(curTime);
    // 1160418110000 = 10/10/2006, the time when the first post has been submitted
    dpStart.setMinDate(1160418110000l);
    dpEnd.setMinDate(1160418110000l);
    builder.setTitle("Select start and end date");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startYear = dpStart.getYear();
            startMonth = dpStart.getMonth();
            startDay = dpStart.getDayOfMonth();
            endYear = dpEnd.getYear();
            endMonth = dpEnd.getMonth();
            endDay = dpEnd.getDayOfMonth();
            mListener.onDateSet(startYear, startMonth, startDay, endYear, endMonth, endDay);
            dialog.dismiss();
        }
    });
    return builder.create();
}
 
Example 9
Source File: SimpleDateDialog.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
protected View onCreateContentView(Bundle savedInstanceState) {

    picker = new DatePicker(getContext());

    Calendar c = Calendar.getInstance();
    if (savedInstanceState != null){
        c.setTimeInMillis(savedInstanceState.getLong(DATE));
    } else if (getArguments().containsKey(DATE)) {
        c.setTimeInMillis(getArguments().getLong(DATE));
    }

    picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), this);

    if (getArguments().containsKey(MIN_DATE)) {
        picker.setMinDate(getArguments().getLong(MIN_DATE));
    }
    if (getArguments().containsKey(MAX_DATE)) {
        picker.setMaxDate(getArguments().getLong(MAX_DATE));
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && getArguments().containsKey(FIRST_DAY_OF_WEEK)) {
        picker.setFirstDayOfWeek(getArguments().getInt(FIRST_DAY_OF_WEEK));
    }



    return picker;
}
 
Example 10
Source File: SimpleDateDialog.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
protected View onCreateContentView(Bundle savedInstanceState) {

    picker = new DatePicker(getContext());

    Calendar c = Calendar.getInstance();
    if (savedInstanceState != null){
        c.setTimeInMillis(savedInstanceState.getLong(DATE));
    } else if (getArguments().containsKey(DATE)) {
        c.setTimeInMillis(getArguments().getLong(DATE));
    }

    picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), this);

    if (getArguments().containsKey(MIN_DATE)) {
        picker.setMinDate(getArguments().getLong(MIN_DATE));
    }
    if (getArguments().containsKey(MAX_DATE)) {
        picker.setMaxDate(getArguments().getLong(MAX_DATE));
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && getArguments().containsKey(FIRST_DAY_OF_WEEK)) {
        picker.setFirstDayOfWeek(getArguments().getInt(FIRST_DAY_OF_WEEK));
    }



    return picker;
}
 
Example 11
Source File: OCM_ChildMenu.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
void showPickDateDialog (final DatePickerDialog.OnDateSetListener listener, Date startDate)
{
    Calendar currentCalendar = Calendar.getInstance();
    if (startDate != null)
    {
        currentCalendar.setTime(startDate);
    }
    final Calendar calendar = currentCalendar;
    DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    //
    d.setCancelable(false);
    d.setCanceledOnTouchOutside(false);
    //
    LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.getWindow().clearFlags(Window.FEATURE_ACTION_BAR);
    d.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    d.setCustomTitle(linearLayout);
    //
    d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick (DialogInterface dialog, int which)
        {
            // do nothing
        }
    });
    //
    DatePicker datePicker = d.getDatePicker();
    calendar.clear();
    calendar.set(2017, Calendar.JANUARY, 1);
    datePicker.setMinDate(calendar.getTimeInMillis());
    calendar.clear();
    calendar.set(2025, Calendar.DECEMBER, 31);
    datePicker.setMaxDate(calendar.getTimeInMillis());
    //
    d.show();
}
 
Example 12
Source File: OBSystemsManager.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public Dialog createDateSetDialog(String message, Boolean cancelable, OBUtils.RunLambda completionBlock)
{
    dateSetCompletionBlock = completionBlock;
    final Calendar calendar = Calendar.getInstance();
    DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    //
    d.setCancelable(cancelable);
    d.setCanceledOnTouchOutside(cancelable);
    //
    if (message == null)
    {
        LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d.setCustomTitle(linearLayout);
    } else
    {
        d.setMessage(message + "\n");
    }
    //
    d.setButton(DatePickerDialog.BUTTON_NEGATIVE, null, (DialogInterface.OnClickListener) null);
    //
    DatePicker datePicker = d.getDatePicker();
    calendar.clear();
    calendar.set(2016, Calendar.JANUARY, 1);
    datePicker.setMinDate(calendar.getTimeInMillis());
    calendar.clear();
    calendar.set(2020, Calendar.DECEMBER, 31);
    datePicker.setMaxDate(calendar.getTimeInMillis());
    return d;
}
 
Example 13
Source File: DatePickerDialogFragment.java    From react-native-GPay with MIT License 4 votes vote down vote up
static Dialog createDialog(
    Bundle args, Context activityContext, @Nullable OnDateSetListener onDateSetListener) {
  final Calendar c = Calendar.getInstance();
  if (args != null && args.containsKey(DatePickerDialogModule.ARG_DATE)) {
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_DATE));
  }
  final int year = c.get(Calendar.YEAR);
  final int month = c.get(Calendar.MONTH);
  final int day = c.get(Calendar.DAY_OF_MONTH);

  DatePickerMode mode = DatePickerMode.DEFAULT;
  if (args != null && args.getString(DatePickerDialogModule.ARG_MODE, null) != null) {
    mode = DatePickerMode.valueOf(args.getString(DatePickerDialogModule.ARG_MODE).toUpperCase(Locale.US));
  }

  DatePickerDialog dialog = null;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    switch (mode) {
      case CALENDAR:
        dialog = new DismissableDatePickerDialog(activityContext,
          activityContext.getResources().getIdentifier("CalendarDatePickerDialog", "style", activityContext.getPackageName()),
          onDateSetListener, year, month, day);
        break;
      case SPINNER:
        dialog = new DismissableDatePickerDialog(activityContext,
          activityContext.getResources().getIdentifier("SpinnerDatePickerDialog", "style", activityContext.getPackageName()),
          onDateSetListener, year, month, day);
        break;
      case DEFAULT:
        dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);
        break;
    }
  } else {
    dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);

    switch (mode) {
      case CALENDAR:
        dialog.getDatePicker().setCalendarViewShown(true);
        dialog.getDatePicker().setSpinnersShown(false);
        break;
      case SPINNER:
        dialog.getDatePicker().setCalendarViewShown(false);
        break;
    }
  }

  final DatePicker datePicker = dialog.getDatePicker();

  if (args != null && args.containsKey(DatePickerDialogModule.ARG_MINDATE)) {
    // Set minDate to the beginning of the day. We need this because of clowniness in datepicker
    // that causes it to throw an exception if minDate is greater than the internal timestamp
    // that it generates from the y/m/d passed in the constructor.
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MINDATE));
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    datePicker.setMinDate(c.getTimeInMillis());
  } else {
    // This is to work around a bug in DatePickerDialog where it doesn't display a title showing
    // the date under certain conditions.
    datePicker.setMinDate(DEFAULT_MIN_DATE);
  }
  if (args != null && args.containsKey(DatePickerDialogModule.ARG_MAXDATE)) {
    // Set maxDate to the end of the day, same reason as for minDate.
    c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MAXDATE));
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    datePicker.setMaxDate(c.getTimeInMillis());
  }

  return dialog;
}
 
Example 14
Source File: DatePickerUtils.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static Dialog buildDialog(Context context,
                                  Calendar c,
                                  String title,
                                  @Nullable Date minDate,
                                  @Nullable Date maxDate,
                                  boolean allowFutureDates,
                                  OnDatePickerClickListener buttonListener) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    WidgetDatepickerBinding widgetBinding = WidgetDatepickerBinding.inflate(layoutInflater);
    final DatePicker datePicker = widgetBinding.widgetDatepicker;
    final DatePicker calendarPicker = widgetBinding.widgetDatepickerCalendar;

    datePicker.updateDate(
            c.get(Calendar.YEAR),
            c.get(Calendar.MONTH),
            c.get(Calendar.DAY_OF_MONTH));

    calendarPicker.updateDate(
            c.get(Calendar.YEAR),
            c.get(Calendar.MONTH),
            c.get(Calendar.DAY_OF_MONTH));

    if (!allowFutureDates) {
        datePicker.setMaxDate(System.currentTimeMillis());
        calendarPicker.setMaxDate(System.currentTimeMillis());
    }

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.DatePickerTheme);
    if (title != null)
        alertDialog.setTitle(title);

    alertDialog.setView(widgetBinding.getRoot());
    Dialog dialog = alertDialog.create();

    widgetBinding.changeCalendarButton.setOnClickListener(calendarButton -> {
        datePicker.setVisibility(datePicker.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        calendarPicker.setVisibility(datePicker.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
    });
    widgetBinding.clearButton.setOnClickListener(clearButton -> {
        buttonListener.onNegativeClick();
        dialog.dismiss();
    });
    widgetBinding.acceptButton.setOnClickListener(acceptButton -> {
        buttonListener.onPositiveClick(datePicker.getVisibility() == View.VISIBLE ? datePicker : calendarPicker);
        dialog.dismiss();
    });

    return dialog;
}
 
Example 15
Source File: DatePickerDialogFragment.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Dialog showCustomCalendar() {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View datePickerView = layoutInflater.inflate(R.layout.widget_datepicker, null);
    final DatePicker datePicker = datePickerView.findViewById(R.id.widget_datepicker);
    final ImageButton changeCalendarButton = datePickerView.findViewById(R.id.changeCalendarButton);
    final Button clearButton = datePickerView.findViewById(R.id.clearButton);
    final Button acceptButton = datePickerView.findViewById(R.id.acceptButton);
    if (fromOtherPeriod())
        clearButton.setText(context.getString(R.string.sectionSelectorNext));

    Calendar c = Calendar.getInstance();
    if (initialDate != null) c.setTime(initialDate);

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    datePicker.updateDate(year, month, day);

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.DatePickerTheme);

    changeCalendarButton.setOnClickListener(view -> {
        showNativeCalendar().show();
        dialog.dismiss();
    });
    clearButton.setOnClickListener(view -> {
        if (onDateSetListener != null)
            onDateSetListener.onClearDate();
        dialog.dismiss();
    });

    acceptButton.setOnClickListener(view -> {
        Calendar chosenDate = Calendar.getInstance();
        chosenDate.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        if (onDateSetListener != null) {
            onDateSetListener.onDateSet(chosenDate.getTime());
        }
        dialog.dismiss();
    });

    if (openingDate != null)
        datePicker.setMinDate(openingDate.getTime());

    if (closingDate == null && !isAllowDatesInFuture()) {
        datePicker.setMaxDate(System.currentTimeMillis());
    } else if (closingDate != null && !isAllowDatesInFuture()) {
        if (closingDate.before(new Date(System.currentTimeMillis()))) {
            datePicker.setMaxDate(closingDate.getTime());
        } else {
            datePicker.setMaxDate(System.currentTimeMillis());
        }
    } else if (closingDate != null && isAllowDatesInFuture()) {
        datePicker.setMaxDate(closingDate.getTime());
    }

    alertDialog.setView(datePickerView);
    if (getArguments().getString(ARG_TITLE) != null)
        alertDialog.setTitle(getArguments().getString(ARG_TITLE));

    dialog = alertDialog.create();
    return dialog;
}
 
Example 16
Source File: TeiProgramListInteractor.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void showCustomCalendar(String programUid, String uid, OrgUnitDialog orgUnitDialog) {
        LayoutInflater layoutInflater = LayoutInflater.from(view.getContext());
//        View datePickerView = layoutInflater.inflate(R.layout.widget_datepicker, null);
        WidgetDatepickerBinding binding = WidgetDatepickerBinding.inflate(layoutInflater);
        final DatePicker datePicker = binding.widgetDatepicker;

        Calendar c = Calendar.getInstance();
        datePicker.updateDate(
                c.get(Calendar.YEAR),
                c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH));

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext(), R.style.DatePickerTheme);

        Program selectedProgram = getProgramFromUid(programUid);
        if (selectedProgram != null && !selectedProgram.selectEnrollmentDatesInFuture()) {
            datePicker.setMaxDate(System.currentTimeMillis());
        }

        if (selectedProgram != null) {
            alertDialog.setTitle(selectedProgram.enrollmentDateLabel());
        }

        alertDialog.setView(binding.getRoot());
        Dialog dialog = alertDialog.create();

        binding.changeCalendarButton.setOnClickListener(changeButton -> {
            showNativeCalendar(programUid, uid, orgUnitDialog);
            dialog.dismiss();
        });

        binding.clearButton.setOnClickListener(clearButton -> dialog.dismiss());
        binding.acceptButton.setOnClickListener(acceptButton -> {
            Calendar selectedCalendar = Calendar.getInstance();
            selectedCalendar.set(Calendar.YEAR, datePicker.getYear());
            selectedCalendar.set(Calendar.MONTH, datePicker.getMonth());
            selectedCalendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
            selectedCalendar.set(Calendar.HOUR_OF_DAY, 0);
            selectedCalendar.set(Calendar.MINUTE, 0);
            selectedCalendar.set(Calendar.SECOND, 0);
            selectedCalendar.set(Calendar.MILLISECOND, 0);
            selectedEnrollmentDate = selectedCalendar.getTime();

            compositeDisposable.add(getOrgUnits(programUid)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            allOrgUnits -> {
                                ArrayList<OrganisationUnit> orgUnits = new ArrayList<>();
                                for (OrganisationUnit orgUnit : allOrgUnits) {
                                    boolean afterOpening = false;
                                    boolean beforeClosing = false;
                                    if (orgUnit.openingDate() == null || !selectedEnrollmentDate.before(orgUnit.openingDate()))
                                        afterOpening = true;
                                    if (orgUnit.closedDate() == null || !selectedEnrollmentDate.after(orgUnit.closedDate()))
                                        beforeClosing = true;
                                    if (afterOpening && beforeClosing)
                                        orgUnits.add(orgUnit);
                                }
                                if (orgUnits.size() > 1) {
                                    orgUnitDialog.setOrgUnits(orgUnits);
                                    if (!orgUnitDialog.isAdded())
                                        orgUnitDialog.show(view.getAbstracContext().getSupportFragmentManager(), "OrgUnitEnrollment");
                                } else if (!orgUnits.isEmpty())
                                    enrollInOrgUnit(orgUnits.get(0).uid(), programUid, uid, selectedEnrollmentDate);
                                else
                                    view.displayMessage(view.getContext().getString(R.string.no_org_units));
                            },
                            Timber::d
                    ));
        });

        dialog.show();
    }
 
Example 17
Source File: SearchTEPresenter.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void showCustomCalendar(OrganisationUnit selectedOrgUnit, String programUid, String uid) {

        if (dialogDisplayed == null || !dialogDisplayed.isShowing()) {
            LayoutInflater layoutInflater = LayoutInflater.from(view.getContext());
            WidgetDatepickerBinding binding = WidgetDatepickerBinding.inflate(layoutInflater);
            final DatePicker datePicker = binding.widgetDatepicker;

            Calendar c = Calendar.getInstance();
            datePicker.updateDate(
                    c.get(Calendar.YEAR),
                    c.get(Calendar.MONTH),
                    c.get(Calendar.DAY_OF_MONTH));

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext(), R.style.DatePickerTheme)
                    .setTitle(selectedProgram.enrollmentDateLabel());

            if (selectedOrgUnit.openingDate() != null)
                datePicker.setMinDate(selectedOrgUnit.openingDate().getTime());

            if (selectedOrgUnit.closedDate() == null && !selectedProgram.selectEnrollmentDatesInFuture()) {
                datePicker.setMaxDate(System.currentTimeMillis());
            } else if (selectedOrgUnit.closedDate() != null && !selectedProgram.selectEnrollmentDatesInFuture()) {
                if (selectedOrgUnit.closedDate().before(new Date(System.currentTimeMillis()))) {
                    datePicker.setMaxDate(selectedOrgUnit.closedDate().getTime());
                } else {
                    datePicker.setMaxDate(System.currentTimeMillis());
                }
            } else if (selectedOrgUnit.closedDate() != null && selectedProgram.selectEnrollmentDatesInFuture()) {
                datePicker.setMaxDate(selectedOrgUnit.closedDate().getTime());
            }

            alertDialog.setView(binding.getRoot());
            dialogDisplayed = alertDialog.create();

            binding.changeCalendarButton.setOnClickListener(changeButton -> {
                showNativeCalendar(selectedOrgUnit, programUid, uid);
                dialogDisplayed.dismiss();
            });
            binding.clearButton.setOnClickListener(clearButton -> dialogDisplayed.dismiss());
            binding.acceptButton.setOnClickListener(acceptButton -> {
                Calendar selectedCalendar = Calendar.getInstance();
                selectedCalendar.set(Calendar.YEAR, datePicker.getYear());
                selectedCalendar.set(Calendar.MONTH, datePicker.getMonth());
                selectedCalendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                selectedCalendar.set(Calendar.HOUR_OF_DAY, 0);
                selectedCalendar.set(Calendar.MINUTE, 0);
                selectedCalendar.set(Calendar.SECOND, 0);
                selectedCalendar.set(Calendar.MILLISECOND, 0);
                selectedEnrollmentDate = selectedCalendar.getTime();

                enrollInOrgUnit(selectedOrgUnit.uid(), programUid, uid, selectedEnrollmentDate);
                dialogDisplayed.dismiss();
            });
            dialogDisplayed.show();
        }
    }
 
Example 18
Source File: EventInitialActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void showCustomCalendar(DatePickerDialog.OnDateSetListener listener) {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    WidgetDatepickerBinding widgetBinding = WidgetDatepickerBinding.inflate(layoutInflater);
    final DatePicker datePicker = widgetBinding.widgetDatepicker;

    Calendar calendar = Calendar.getInstance();

    if (selectedDate != null)
        calendar.setTime(selectedDate);

    if (eventCreationType == EventCreationType.SCHEDULE)
        calendar.add(Calendar.DAY_OF_YEAR, eventScheduleInterval);

    datePicker.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    if (program.expiryPeriodType() != null) {
        Date minDate = DateUtils.getInstance().expDate(null, program.expiryDays() == null ? 0 : program.expiryDays(), program.expiryPeriodType());
        datePicker.setMinDate(minDate.getTime());
    }

    switch (eventCreationType) {
        case ADDNEW:
        case DEFAULT:
            datePicker.setMaxDate(System.currentTimeMillis() - 1000);
            break;
        case REFERAL:
        case SCHEDULE:
            break;
    }

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(), R.style.DatePickerTheme);

    alertDialog.setView(widgetBinding.getRoot());
    Dialog dialog = alertDialog.create();

    widgetBinding.changeCalendarButton.setOnClickListener(calendarButton -> {
        showNativeCalendar(listener);
        dialog.dismiss();
    });
    widgetBinding.clearButton.setOnClickListener(clearButton -> dialog.dismiss());
    widgetBinding.acceptButton.setOnClickListener(acceptButton -> {
        listener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        dialog.dismiss();
    });

    dialog.show();
}