Skip to content

michael-winkler/EventCalendar

Repository files navigation

EventCalendar

Last commit GitHub all releases API License Apache 2.0

This library uses Android X depencies and is written in Kotlin.

Usage

Add a dependency to your build.gradle file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.michael-winkler:EventCalendar:1.3.6'
}
import com.nmd.eventCalendar.EventCalendarView

Exampe code

You can create a new EventCalendarView inside your XML-Layout like this:

<com.nmd.eventCalendar.EventCalendarView
    android:id="@+id/eventCalendarView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:ecv_count_background_text_color="@android:color/white"
    app:ecv_count_background_tint_color="@android:color/holo_blue_light"
    app:ecv_count_visible="true"
    app:ecv_current_day_background_tint_color="@android:color/holo_red_dark"
    app:ecv_current_day_text_color="@android:color/white"
    app:ecv_disallow_intercept="false"
    app:ecv_edge_to_edge_enabled="true"
    app:ecv_event_item_automatic_text_color="true"
    app:ecv_event_item_text_color="@android:color/black"
    app:ecv_header_visible="true"
    app:ecv_calendar_week_visible="true"/>

Or if you just want the current calendar week you can use this one:

<com.nmd.eventCalendar.EventCalendarSingleWeekView
    android:id="@+id/eventCalendarView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:ecv_count_background_text_color="@android:color/white"
    app:ecv_count_background_tint_color="@android:color/holo_blue_light"
    app:ecv_count_visible="true"
    app:ecv_current_day_background_tint_color="@android:color/holo_red_dark"
    app:ecv_current_day_text_color="@android:color/white"
    app:ecv_event_item_automatic_text_color="true"
    app:ecv_event_item_text_color="@android:color/black"
    app:ecv_header_visible="true"
    app:ecv_calendar_week_visible="true"/>

Here you can see all custom app parameters which you can use:
https://github.com/michael-winkler/EventCalendar/blob/main/library/src/main/res/values/attr.xml

The EventCalendarSingleWeekView can use all app:evc too except ecv_disallow_intercept and ecv_edge_to_edge_enabled. Also the EventCalendarSingleWeekView displays only Events where the date is in range of the current calendar week. You don't need anything to do by yourself. The library will filter automatically the events for the week view by itself.

Now in your class you can get a reference to it like this:

binding.eventCalendarView.addOnDayClickListener(object :
    EventCalendarDayClickListener {
    override fun onClick(day: Day) {
        // val eventList = binding.eventCalendarView.events.filter { it.date == day.date }
        // You can use this to get the events for the selected day
        Log.i("ECV", "TEST 1: " + day.date)
    }
})

The EventCalendarDayClickListener listener works also for the EventCalendarSingleWeekView.

The Day object structure is following:

data class Day(
    /**
     * eg: 31
     */
    val value: String,
    /**
     * eg: true | false
     */
    val isCurrentMonth: Boolean,
    /**
     * eg: true | false
     */
    var isCurrentDay: Boolean,
    /**
     * eg: 31.12.2024
     */
    var date: String,
)

You can also watch to calendar scroll changes.

binding.eventCalendarView.addOnCalendarScrollListener(object : EventCalendarScrollListener {
    override fun onScrolled(month: Int, year: Int) {
        Log.i("ECV", "Scrolled to: $month $year")
    }
})

If you scroll for example to january 2023, the month value will be 1 and the year 2023.


It is really to easy to add events to the calendar. Here is an example code:

binding.eventCalendarView.events = arrayListOf(
    Event(date = "15.04.2023", name = "Vacation", backgroundHexColor = "#4badeb"),
    Event(date = "16.04.2023", name = "Home office", backgroundHexColor = "#e012ad"),
    Event(date = "17.04.2023", name = "Meeting", backgroundHexColor = "#e07912"),
    Event(date = "18.04.2023", name = "Vacation", backgroundHexColor = "#4badeb", data = "Let's go!")
)

The date format have to be in format "dd.MM.yyyy".

The Event object structure is following:

@Parcelize
data class Event(
    val date: String,
    val name: String,
    val backgroundHexColor: String,
    val data: @RawValue Any? = null,
) : Parcelable

The event text color is automatically determined. If the background color is dark, the text color is white. Otherwise, the text color is gray. You can change this behaviour with app:ecv_event_item_automatic_text_color="false". And then with app:ecv_event_item_text_color="@android:color/black" you can set the event item text color.

Javadoc

Each function has also a javadoc documentation.

Edge to edge support

The EventCalendarView does also support edge to edge.

app:ecv_edge_to_edge_enabled="true"

You can enable or disable the edge to edge handling inside your xml configuration. The default value is false.

Screenshots

Sample app

https://github.com/michael-winkler/EventCalendar/releases/download/1.3.6/app-release.apk

License

Copyright Author @NMD [Next Mobile Development - Michael Winkler]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Last words

If you like this library feel free to "star" it:
star

This library has been successfully tested with:
Android Studio Koala | 2024.1.1 Patch 2