'전체 글'에 해당되는 글 59건

  1. 00:11:15 Custom Builder of MaterialDatePicker
Mobile Dev/Android2020. 11. 26. 00:11

I have used the MaterialDatePicker in my project, so I made a simple custom builder for the MaterialDatePicker. It can set a constraint of date range, theme and title text. 

MaterialDatePicker를 프로젝느에서 사용한 일이 있어서 간단하게 Builder를 만들어 봤다. 날짜 범위 제한을 설정할 수 있다. 물론 간단하게 테마와 타이틀 설정도 추가 함.

 

 

public class MaterialDatePickerBuilder {

    private MaterialDatePicker.Builder builder;

    /**
     * constractor
     */ 
    public MaterialDatePickerBuilder() {

        builder = MaterialDatePicker.Builder.dateRangePicker();
    }


    /**
     * set date range of constraints
     *
     * @param startDate start date
     * @param endDate end date
     * @return builder
     */
    public MaterialDatePickerBuilder setConstraintsDateRange(Date startDate, Date endDate) {

        long minDate = startDate.getTime();
        long maxDate = endDate.getTime();
        CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();
        constraintsBuilderRange.setStart(minDate);
        constraintsBuilderRange.setEnd(maxDate);
        constraintsBuilderRange.setValidator(new DateRangeValidator(minDate, maxDate));

        builder.setCalendarConstraints(constraintsBuilderRange.build());
        return this;
    }


    /**
     * set title text
     *
     * @param titleText title text
     * @return builder
     */
    public MaterialDatePickerBuilder setTitleText(String titleText) {

        builder.setTitleText(titleText);
        return this;
    }

 

    /**
     * set theme
     *
     * @param themeResId theme resource id
     * @return builder
     */
    public MaterialDatePickerBuilder setTheme(@StyleRes int themeResId) {

        builder.setTheme(themeResId);
        return this;
    }


    /**
     * show MaterialDatePicker
     *
     * @param manager fragment manager
     * @param tag tag
     * @return MaterialDatePicker
     */
    public MaterialDatePicker showMaterialDatePicker(@NonNull FragmentManager manager, @Nullable String tag) {

        MaterialDatePicker picker = builder.build();
        picker.show(manager, tag);
        return picker;
    }


    /**
     * show MaterialDatePicker
     *
     * @param manager fragment manager
     * @param tag tag
     * @param onPositiveButtonClickListener onPositiveButtonClickListener
     * @param onNegativeButtonClickListener onNegativeButtonClickListener
     * @return MaterialDatePicker
     */
    public MaterialDatePicker showMaterialDatePicker(@NonNull FragmentManager manager, @Nullable String tag,
            MaterialPickerOnPositiveButtonClickListener onPositiveButtonClickListener,
            View.OnClickListener onNegativeButtonClickListener) {

        MaterialDatePicker picker = builder.build();
        picker.addOnPositiveButtonClickListener(onPositiveButtonClickListener);
        picker.addOnNegativeButtonClickListener(onNegativeButtonClickListener);
        picker.show(manager, tag);
        return picker;
    }


}

 

public class DateRangeValidator implements CalendarConstraints.DateValidator {

    private long minDate, maxDate;

    public DateRangeValidator(long minDate, long maxDate) {
        this.minDate = minDate;
        this.maxDate = maxDate;
    }

    public DateRangeValidator(Parcel parcel) {
        minDate = parcel.readLong();
        maxDate = parcel.readLong();
    }

    @Override
    public boolean isValid(long date) {
        return !(minDate > date || maxDate < date);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(minDate);
        dest.writeLong(maxDate);
    }

    public static final Parcelable.Creator<DateRangeValidator> CREATOR = new Parcelable.Creator<DateRangeValidator>() {

        @Override
        public DateRangeValidator createFromParcel(Parcel parcel) {
            return new DateRangeValidator(parcel);
        }

        @Override
        public DateRangeValidator[] newArray(int size) {
            return new DateRangeValidator[size];
        }
    };

}

 

Usage example

new MaterialDatePickerBuilder().setConstraintsDateRange(DateUtil.setTime(DateUtil.addDate(-91), 0, 0, 0, 0),
        DateUtil.setTime(DateUtil.addDate(-1), 23, 59, 59, 999))
    .setTitleText(ResourceGetter.getString(getOwnerActivity(), R.string.str_select_date_range))
    .setTheme(R.style.styleDatePicker)
    .showMaterialDatePicker(getOwnerActivity().getSupportFragmentManager(),

        ResourceGetter.getString(getOwnerActivity(), R.string.str_select_date_range),

        new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
            @Override
            public void onPositiveButtonClick(Pair<Long, Long> selection) {
                searchDateStart = new Date(selection.first);
                searchDateEnd = new Date(selection.second);
            }
        }, null);

Posted by Developer Notes as.wind.914

댓글을 달아 주세요