본문 바로가기
App Programming/Android Studio

[Android Studio] Material CalendarView (2)

by goatlab 2022. 9. 14.
728x90
반응형
SMALL

OneDayDecorator

 

오늘 날짜를 커스텀할 수 있다.

 

package com.example.myapplication.decorators;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import java.util.Date;

/**
 * Decorate a day by making the text big and bold
 */
public class OneDayDecorator implements DayViewDecorator {

    private CalendarDay date;

    public OneDayDecorator() {
        date = CalendarDay.today();
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return date != null && day.equals(date);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new StyleSpan(Typeface.BOLD));
        view.addSpan(new RelativeSizeSpan(2.f));
        view.addSpan(new ForegroundColorSpan(Color.GREEN));
    }

    /**
     * We're changing the internals, so make sure to call {@linkplain MaterialCalendarView#invalidateDecorators()}
     */
    public void setDate(Date date) {
        this.date = CalendarDay.from(date);
    }
}

 

Main은 다음과 같다.

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import com.example.myapplication.decorators.FridayDecorator;
import com.example.myapplication.decorators.MondayDecorator;
import com.example.myapplication.decorators.OneDayDecorator;
import com.example.myapplication.decorators.SaturdayDecorator;
import com.example.myapplication.decorators.SundayDecorator;
import com.example.myapplication.decorators.ThursdayDecorator;
import com.example.myapplication.decorators.TuesdayDecorator;
import com.example.myapplication.decorators.WednesdayDecorator;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MaterialCalendarView calendarView = findViewById(R.id.calendarView);
        calendarView.setSelectedDate(CalendarDay.today());

        OneDayDecorator oneDayDecorator = new OneDayDecorator();


        calendarView.addDecorators(
                oneDayDecorator
        );;

        calendarView.addDecorators(
                new SundayDecorator(),
                new SaturdayDecorator(),
                new MondayDecorator(),
                new TuesdayDecorator(),
                new ThursdayDecorator(),
                new WednesdayDecorator(),
                new FridayDecorator()
        );
    }
}

 

EventDecorator

 

특정 날짜에 점을 표시하는 decorator이다.

 

package com.example.myapplication.decorators;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.spans.DotSpan;

import java.util.Collection;
import java.util.HashSet;

/**
 * Decorate several days with a dot
 */
public class EventDecorator implements DayViewDecorator {

    private final int color;
    private final HashSet<CalendarDay> dates;

    public EventDecorator(int color, Collection<CalendarDay> dates) {
        this.color = color;
        this.dates = new HashSet<>(dates);
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return dates.contains(day);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new DotSpan(5, color));
    }
}

 

Main은 다음과 같다.

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.example.myapplication.decorators.EventDecorator;
import com.example.myapplication.decorators.FridayDecorator;
import com.example.myapplication.decorators.MondayDecorator;
import com.example.myapplication.decorators.OneDayDecorator;
import com.example.myapplication.decorators.SaturdayDecorator;
import com.example.myapplication.decorators.SundayDecorator;
import com.example.myapplication.decorators.ThursdayDecorator;
import com.example.myapplication.decorators.TuesdayDecorator;
import com.example.myapplication.decorators.WednesdayDecorator;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MaterialCalendarView calendarView = findViewById(R.id.calendarView);
        calendarView.setSelectedDate(CalendarDay.today());

        OneDayDecorator oneDayDecorator = new OneDayDecorator();

        calendarView.addDecorators(
                oneDayDecorator
        );

        calendarView.addDecorator(new EventDecorator(Color.GREEN, Collections.singleton(CalendarDay.today())));

        calendarView.addDecorators(
                new SundayDecorator(),
                new SaturdayDecorator(),
                new MondayDecorator(),
                new TuesdayDecorator(),
                new ThursdayDecorator(),
                new WednesdayDecorator(),
                new FridayDecorator()
        );
    }
}

728x90
반응형
LIST