본문 바로가기
App Programming/Android Studio

[Android Studio] Material CalendarView (3)

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

strings

 

달력에서 영어가 아닌 한글로 보여줄 수 있다. res ➔ values ➔ strings.xml에서 다음을 추가한다.

 

<resources>
    <string name="app_name">My Application</string>
    <string-array name="custom_weekdays">
        <item>일</item>
        <item>월</item>
        <item>화</item>
        <item>수</item>
        <item>목</item>
        <item>금</item>
        <item>토</item>
    </string-array>

    <string-array name="custom_months">
        <item>1월</item>
        <item>2월</item>
        <item>3월</item>
        <item>4월</item>
        <item>5월</item>
        <item>6월</item>
        <item>7월</item>
        <item>8월</item>
        <item>9월</item>
        <item>10월</item>
        <item>11월</item>
        <item>12월</item>
    </string-array>

</resources>

 

themes

 

달력에서 textColor를 수정할 수 있다. res ➔ values ➔ themes ➔ themes.xml에서 다음을 추가한다.

 

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Widget.CalendarView.Custom" parent="android:Widget.CalendarView">
        <item name="android:focusedMonthDateColor">@android:color/holo_blue_light</item>
        <item name="android:weekNumberColor">@android:color/holo_red_light</item>
        <item name="android:selectedWeekBackgroundColor">@android:color/holo_blue_light</item>
        <item name="android:selectedDateVerticalBar">@android:color/holo_blue_light</item>
        <item name="android:unfocusedMonthDateColor">@android:color/holo_blue_light</item>
        <item name="android:weekDayTextAppearance">@style/TextAppearance.AppCompat.Medium</item>
        <item name="android:dateTextAppearance">@style/TextAppearance.AppCompat.Medium</item>
    </style>

    <!-- 달력에 사용하는 스타일 -->
    <style name="CustomTextAppearance" parent="TextAppearance.AppCompat.Medium">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textStyle">bold</item>
        <item name="android:includeFontPadding">false</item>
    </style>

    <!-- 요일, 달력 안 숫자들의 폰트 설정 -->
    <style name="CalenderViewCustom" parent="Theme.AppCompat">
        <item name="colorAccent">@android:color/holo_red_light</item>
        <item name="colorPrimary">@android:color/holo_blue_light</item>
        <item name="android:textStyle">bold</item>
    </style>

    <!-- 달력 안의 숫자들에 적용되는 스타일 -->
    <style name="CalenderViewDateCustomText" parent="android:TextAppearance.DeviceDefault.Small">
        <item name="android:textColor">@color/black</item>
        <item name="android:weekNumberColor">@android:color/holo_red_light</item>
    </style>

    <!-- 달력 안의 요일들에 적용되는 스타일 -->
    <style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
        <item name="android:textColor">@color/black</item>
        <item name="android:weekNumberColor">@android:color/holo_red_light</item>
    </style>

    <!-- 연, 월을 표시하는 헤더에 적용되는 스타일 -->
    <style name="CalendarWidgetHeader">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/black</item>
    </style>
</resources>

 

원래 layout의 calendarView xml에서 android:theme="@style/CalenderViewCustom"를 추가한다.

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#012700"
    tools:context=".MainActivity">

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:theme="@style/CalenderViewCustom"
        app:mcv_selectionColor="#739c73"
        app:mcv_showOtherDates="all"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

main

 

package com.example.myapplication;

import android.database.Cursor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.format.ArrayWeekDayFormatter;
import com.prolificinteractive.materialcalendarview.format.MonthArrayTitleFormatter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    String time,kcal,menu;
    private final OneDayDecorator oneDayDecorator = new OneDayDecorator();
    Cursor cursor;
    MaterialCalendarView materialCalendarView;

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

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

        materialCalendarView.state().edit()
                .setFirstDayOfWeek(Calendar.SUNDAY)
                .setMinimumDate(CalendarDay.from(2022, 0, 1)) // 달력의 시작
                .setMaximumDate(CalendarDay.from(2035, 11, 31)) // 달력의 끝
                .setCalendarDisplayMode(CalendarMode.MONTHS)
                .commit();

        materialCalendarView.setTitleFormatter(new MonthArrayTitleFormatter(getResources().getTextArray(R.array.custom_months)));
        materialCalendarView.setWeekDayFormatter(new ArrayWeekDayFormatter(getResources().getTextArray(R.array.custom_weekdays)));

        materialCalendarView.addDecorators(
                new SundayDecorator(),
                new SaturdayDecorator(),
                new MondayDecorator(),
                new TuesdayDecorator(),
                new ThursdayDecorator(),
                new WednesdayDecorator(),
                new FridayDecorator(),
                oneDayDecorator);

        String[] result = {"2022,09,18","2022,09,19","2022,09,20","2022,09,21"}; // 여기에 날짜

        new ApiSimulator(result).executeOnExecutor(Executors.newSingleThreadExecutor());

        materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                int Year = date.getYear();
                int Month = date.getMonth() + 1;
                int Day = date.getDay();

                Log.i("Year test", Year + "");
                Log.i("Month test", Month + "");
                Log.i("Day test", Day + "");

                String shot_Day = Year + "," + Month + "," + Day;

                Log.i("shot_Day test", shot_Day + "");
                materialCalendarView.clearSelection();

                Toast.makeText(getApplicationContext(), shot_Day , Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class ApiSimulator extends AsyncTask<Void, Void, List<CalendarDay>> {

        String[] Time_Result;

        ApiSimulator(String[] Time_Result) {
            this.Time_Result = Time_Result;
        }

        @Override
        protected List<CalendarDay> doInBackground(@NonNull Void... voids) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Calendar calendar = Calendar.getInstance();
            ArrayList<CalendarDay> dates = new ArrayList<>();

            for (int i = 0; i < Time_Result.length; i++) {
                String[] time = Time_Result[i].split(",");

                int year = Integer.parseInt(time[0]);
                int month = Integer.parseInt(time[1]);
                int dayy = Integer.parseInt(time[2]);

                calendar.set(year, month - 1, dayy);
                CalendarDay day = CalendarDay.from(calendar);
                dates.add(day);
            }

            return dates;
        }

        @Override
        protected void onPostExecute(@NonNull List<CalendarDay> calendarDays) {
            super.onPostExecute(calendarDays);

            if (isFinishing()) {
                return;
            }
            materialCalendarView.addDecorator(new EventDecorator(Color.GREEN, calendarDays,MainActivity.this));
        }
    }
}

728x90
반응형
LIST