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

The following examples show how to use android.widget.DatePicker#updateDate() . 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: PickerActions.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Returns a {@link ViewAction} that sets a date on a {@link DatePicker}. */
public static ViewAction setDate(final int year, final int monthOfYear, final int dayOfMonth) {

  // monthOfYear which starts with zero in DatePicker widget.
  final int normalizedMonthOfYear = monthOfYear - 1;

  return new ViewAction() {

    @Override
    public void perform(UiController uiController, View view) {
      final DatePicker datePicker = (DatePicker) view;
      datePicker.updateDate(year, normalizedMonthOfYear, dayOfMonth);
    }

    @Override
    public String getDescription() {
      return "set date";
    }

    @SuppressWarnings("unchecked")
    @Override
    public Matcher<View> getConstraints() {
      return allOf(isAssignableFrom(DatePicker.class), isDisplayed());
    }
  };
}
 
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: 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 4
Source File: EventCaptureActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void showCustomCalendar() {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    WidgetDatepickerBinding widgetBinding = WidgetDatepickerBinding.inflate(layoutInflater);
    final DatePicker datePicker = widgetBinding.widgetDatepicker;

    Calendar c = DateUtils.getInstance().getCalendar();
    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(getContext(), R.style.DatePickerTheme);

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

    widgetBinding.changeCalendarButton.setOnClickListener(calendarButton -> {
        showNativeCalendar();
        dialog.dismiss();
    });
    widgetBinding.clearButton.setOnClickListener(clearButton -> dialog.dismiss());
    widgetBinding.acceptButton.setOnClickListener(acceptButton -> {
        Calendar chosenDate = Calendar.getInstance();
        chosenDate.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        presenter.rescheduleEvent(chosenDate.getTime());
        dialog.dismiss();
    });
    dialog.show();
}
 
Example 5
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();
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: DateTimeWidget.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
public DateTimeWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    mDatePicker = new DatePicker(getContext());
    mDatePicker.setFocusable(!prompt.isReadOnly());
    mDatePicker.setEnabled(!prompt.isReadOnly());

    mTimePicker = new TimePicker(getContext());
    mTimePicker.setFocusable(!prompt.isReadOnly());
    mTimePicker.setEnabled(!prompt.isReadOnly());
    mTimePicker.setPadding(0, 20, 0, 0);
    mTimePicker.setOnTimeChangedListener(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mTimePicker.setSaveFromParentEnabled(false);
        mTimePicker.setSaveEnabled(true);
    }

    String clockType =
            android.provider.Settings.System.getString(context.getContentResolver(),
                    android.provider.Settings.System.TIME_12_24);
    if (clockType == null || clockType.equalsIgnoreCase("24")) {
        mTimePicker.setIs24HourView(true);
    }

    mDateListener = (view, year, month, day) -> {
        if (mPrompt.isReadOnly()) {
            setAnswer();
        } else {
            // handle leap years and number of days in month
            // TODO
            // http://code.google.com/p/android/issues/detail?id=2081
            Calendar c = Calendar.getInstance();
            c.set(year, month, 1);
            int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (day > max) {
                //If the day has fallen out of spec, set it to the correct max
                mDatePicker.updateDate(year, month, max);
            } else {
                if (!(mDatePicker.getDayOfMonth() == day && mDatePicker.getMonth() == month && mDatePicker.getYear() == year)) {
                    //CTS: No reason to change the day if it's already correct?
                    mDatePicker.updateDate(year, month, day);
                }
            }
        }
        widgetEntryChanged();
    };

    // If there's an answer, use it.
    setAnswer();

    setGravity(Gravity.LEFT);
    addView(mDatePicker);
    addView(mTimePicker);

}