Android platform inherit View to achieve a custom calendar control.

rendering

Using the step

1. Layout:

<com.arisaid.calendarview.widget.WeekView
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginTop="10dp"
    android:background="@android:color/white"
    app:wv_textColor="#333333"
    app:wv_textSize="14sp"/>
    
<com.arisaid.calendarview.widget.CalendarView
    android:id="@+id/calendarView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/white"
    app:cv_dayBackground="@drawable/bg_day_un_selected"
    app:cv_selectDayBackground="@drawable/bg_day_selected"
    app:cv_selectTextColor="@android:color/white"
    app:cv_textColor="#333333"
    app:cv_textSize="14sp"/>
Copy the code

2. Set the data of selected days (optional operation) :

mCalendarView.setSelectDate(initData());

private List<String> initData() {
    List<String> dates = new ArrayList<>();
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    SimpleDateFormat sdf = new SimpleDateFormat(mCalendarView.getDateFormatPattern(), Locale.CHINA);
    sdf.format(calendar.getTime());
    dates.add(sdf.format(calendar.getTime()));
    return dates;
}
Copy the code

3, Set the listener:

mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(@NonNull CalendarView view, boolean select, int year, int month, Int day) {if(select){toast.makeText (getApplicationContext(), "check: "+ year + year + (the month + 1) +" month "+ day +" day ", Toast. LENGTH_SHORT.) the show (); }else{toast.makeText (getApplicationContext(), "deselect: "+ year + year + (the month + 1) +" month "+ day +" day ", Toast. LENGTH_SHORT.) the show (); }}});Copy the code

Properties & Methods

The property name methods role
cv_textColor setTextColor(@ColorInt int textColor) Sets the default text color
cv_selectTextColor setSelectTextColor(@ColorInt int textColor) Set the selected text color
cv_textSize setTextSize(float textSize) Sets the default text size
cv_selectTextSize setSelectTextSize(float textSize) Set the selected text size
cv_dayBackground setDayBackground(Drawable background) Set the background for the default day
cv_selectDayBackground setSelectDayBackground(Drawable background) Set Select the background the day after tomorrow
cv_dateFormatPattern setDateFormatPattern(String pattern) Set the date formatting format

To contact me