Code Monkey home page Code Monkey logo

crunchycalendar's Introduction

Maven Central

alt text

CrunchyCalendar

A light, powerful and easy to use Calendar Widget with a number out of the box features:

  • Infinite vertical scrolling in both directions;
  • Setting date boundaries to restrict scrolling inside of a specific time period;
  • Single / multiple / range dates selection;
  • Pre-selecting dates;
  • Color customization;
  • Displaying color indicators;
  • Setting own custom ItemDecoration;
  • Presented as a View subclass which can be displayed everywhere: in Activity, Fragment or Dialog, or can be integrated into another custom View.

alt text

Dependency

This library is available on Maven Central (Previously on jCenter).

Gradle

implementation 'ru.cleverpumpkin:crunchycalendar:2.6.1'

Maven

<dependency>
  <groupId>ru.cleverpumpkin</groupId>
  <artifactId>crunchycalendar</artifactId>
  <version>2.6.1</version>
  <type>pom</type>
</dependency>

Usage

Here's a basic example of Calendar usage.

First of all, you should declare CalendarView in your layout XML file.

  <ru.cleverpumpkin.calendar.CalendarView 
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Perform Calendar setup in your Activity or Fragment class.

val calendarView = view.findViewById(R.id.calendar_view)
val calendar = Calendar.getInstance()

// Initial date
calendar.set(2018, Calendar.JUNE, 1)
val initialDate = CalendarDate(calendar.time)

// Minimum available date
calendar.set(2018, Calendar.MAY, 15)
val minDate = CalendarDate(calendar.time)

// Maximum available date
calendar.set(2018, Calendar.JULY, 15)
val maxDate = CalendarDate(calendar.time)

// List of preselected dates that will be initially selected
val preselectedDates: List<CalendarDate> = getPreselectedDates()

// The first day of week
val firstDayOfWeek = java.util.Calendar.MONDAY

// Set up calendar with all available parameters
calendarView.setupCalendar(
    initialDate = initialDate, 
    minDate = minDate,
    maxDate = maxDate,
    selectionMode = SelectionMode.NONE,
    selectedDates = preselectedDates,
    firstDayOfWeek = firstDayOfWeek,
    showYearSelectionView = true
)
                

Note: all parameters in setupCalendar() method are optional and have default values.

To handle date click / long click with custom action, you can do this:

// Set date click callback 
calendarView.onDateClickListener = { date ->

    // Do something ... 
    // for example get list of selected dates
    val selectedDates = calendarView.selectedDates 
}

// Set date long click callback 
calendarView.onDateLongClickListener = { date ->

    // Do something ... 
}

Saving and Restoring state

Calendar takes care of saving and restoring its internal state (selected dates, selection mode, etc.), so there's no need to save it manually and call CalendarView.setupCalendar() method every time, when Activity or Fragment is recreated.

If a Calendar was set up with CalendarView.setupCalendar() method before restoring state, previous saved state will be ignored.

Dates Selection

Calendar supports several selection modes: single, multiple and range.

Note: You can restrict selection of some dates by implementing your own filtration logic:

// Here we make weekends unavailable for selection
calendarView.dateSelectionFilter = { date ->
    date.dayOfWeek != Calendar.SATURDAY && date.dayOfWeek != Calendar.SUNDAY
}

Single date selection

Only one date can be selected at a time.

// Set up calendar with SelectionMode.SINGLE
calendarView.setupCalendar(selectionMode = SelectionMode.SINGLE)

...

// Get selected date or null
val selectedDate: CalendarDate? = calendarView.selectedDate

// Get list with single selected date or empty list
val selectedDates: List<CalendarDate> = calendarView.selectedDates

Multiple dates selection

A number of dates can be selected. Pressing an already selected date will unselect it.

// Set up calendar with SelectionMode.MULTIPLE
calendarView.setupCalendar(selectionMode = SelectionMode.MULTIPLE)

...

// Get all selected dates in order they were added or empty list
val selectedDates: List<CalendarDate> = calendarView.selectedDates

Range date selection

Allows you to select a date range. Previous selected range is cleared when you select another one.

// Set up calendar with SelectionMode.RANGE
calendarView.setupCalendar(selectionMode = SelectionMode.RANGE)

... 

// Get all selected dates in range (includes start and end date) or empty list
val selectedDates: List<CalendarDate> = calendarView.selectedDates

Color Indicators

The Calendar is able to display simple color indicators (dots) on the date cell.

Color indicator represents as simple interface, which you can implement in your classes.

interface DateIndicator {
    val date: CalendarDate // indicator date
    val color: Int // indicator color
}

Here's an example of setting indicators to display on the Calendar.

// Set up calendar
calendarView.setupCalendar()


val indicators: List<DateIndicator> = getDatesIndicators()

// Set List of indicators that will be displayed on the calendar 
calendarView.datesIndicators = indicators

To get all indicators for specific date, you can do this:

// Set date click callback 
calendarView.onDateClickListener = { date ->

    // Get all indicators for the date
    val indicatorsForDate = calendarView.getDateIndicators(date)
    
    // do something ... 
}

Additional text on date cell

The Calendar is able to display additional text on the date cell.

Additional text represents as simple interface, which you can implement in your classes.

interface AdditionalText {
    val date: CalendarDate // text's date
    val text: String // text to display
    val color: Int // text's color
}

Here's an example of setting additional text to display on the Calendar.

// Set up calendar
calendarView.setupCalendar()


val additionalTexts: List<AdditionalText> = generateAdditionalTexts()

// Set List of additional texts that will be displayed on the calendar 
calendarView.datesAdditionalTexts = additionalTexts

Using default or root locale on digits

The Calendar is able to display digits using default or root locale.

// Set true for root locale or false for default locale. If nothing is set default locale will be used.
setUseRootLocale(true)

// Then set up calendar
calendarView.setupCalendar()

View Customization

Calendar appearance open for customization.

Styling using XML

Calendar appearance can be customized with XML attributes. Here's an example of applying custom style to change Calendar appearance.

Define your custom style for the Calendar.

<style name="CalendarViewCustomStyle">
    <item name="android:background">@android:color/white</item>
    <item name="calendar_date_background">@drawable/custom_date_bg_selector</item>
    <item name="calendar_date_text_color">@color/custom_date_text_selector</item>
    <item name="calendar_day_bar_background">@color/custom_calendar_days_bar_background</item>
    <item name="calendar_day_bar_text_color">@color/custom_calendar_days_bar_text_color</item>
    <item name="calendar_day_bar_weekend_text_color">@color/custom_calendar_days_bar_text_color</item>
    <item name="calendar_grid_color">@color/custom_calendar_grid_color</item>
    <item name="calendar_grid_on_selected_dates">false</item>
    <item name="calendar_month_text_color">@color/custom_calendar_month_text_color</item>
    <item name="calendar_year_selection_arrows_color">
        @color/custom_calendar_year_selection_arrows_color
    </item>
    <item name="calendar_year_selection_background">
        @color/custom_calendar_year_selection_background
    </item>
    <item name="calendar_year_selection_text_color">
        @color/custom_calendar_year_selection_text_color
    </item>
</style>

Apply your custom style.

<ru.cleverpumpkin.calendar.CalendarView
    android:id="@+id/calendar_view"
    style="@style/CalendarViewCustomStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

To define custom styles for all Calendars in your app at once you can do this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <!-- ...snip... -->
    
    <item name="calendarViewStyle">@style/CalendarViewCustomStyle</item>
    
    <!-- ...etc... -->
</style>

Styling using code

You can also set styles and colors programmatically:

 with(calendarView) {
     setDrawGridOnSelectedDates(drawGrid = true)
     setGridColorRes(R.color.custom_calendar_grid_color)
     setYearSelectionBarBackgroundColorRes(R.color.custom_calendar_year_selection_background)
     setYearSelectionBarArrowsColorRes(R.color.custom_calendar_year_selection_arrows_color)
     setYearSelectionBarTextColorRes(R.color.custom_calendar_year_selection_text_color)
     setDaysBarBackgroundColorRes(R.color.custom_calendar_days_bar_background)
     setDaysBarTextColorRes(R.color.custom_calendar_days_bar_text_color)
     setDaysBarWeekendTextColorRes(R.color.custom_calendar_days_bar_text_color)
     setMonthTextColorRes(R.color.custom_calendar_month_text_color)
     setDateCellBackgroundRes(R.drawable.custom_date_bg_selector)
     setDateCellTextColorRes(R.color.custom_date_text_selector)
}

If you need to add some custom drawing logic for Calendar, you can implement standard RecyclerView.ItemDecoration and add it for Calendar using addCustomItemDecoration() method.

// Set up calendar
calendarView.setupCalendar()

// Some custom decoration logic 
val customItemDecoration = CustomItemDecoration()

// Add custom item decoration for calendar
calendarView.addCustomItemDecoration(customItemDecoration)

There is an abstract helper class AbsDateItemDecoration that you can extend to implement custom drawing logic for specific dates cells.

Sample app

The Sample app is available for download from Google Play.

Sketch file

Wouldn’t it be a real pain for your designer to ignore Calendar View in your apps mockups? Or for them to try and explain to you, which colors you should use by adding them to Jira task in plain text?

That is lame. That’s why we’ve added a .sketch-file to this repository here. Have fun!

License


MIT License

Copyright (c) 2018 CleverPumpkin Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Maintainers

alt text

CrunchyCalendar is maintained and developed by CleverPumpkin.

crunchycalendar's People

Contributors

crabgore avatar imofas avatar itolianezzz avatar ladalarkina avatar limanskaya avatar raseri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crunchycalendar's Issues

Theming

First of all, thanks for the library :)

Please, add more possibilities for theming:

  1. Make properties public, to allow theming from code, not xml only
  2. Do not ignore defStyleAttr from the constructor, right now it just goes to the super classes, but is ignored in the "obtainStyledAttributes" in init block

Sample App not available in Google Play for LineageOS 19.1 (Android 12)

Hi and thanks for the project! The vertical scrolling looks great!

Unfortunately your Sample App https://play.google.com/store/apps/details?id=ru.cleverpumpkin.calendar.sample is shown as "unavailable on your device because it was designed for an older version of Android" in Google Play when accessed from a LineageOS 19.1 (Android 12) device.

I haven't tried to install manually yet, but maybe you would be so kind as to remove this limitation? Thank you!

New logo/icon proposal

Good day sir. I am a graphic designer and i am interested in designing a logo for your good project. I will be doing it as a gift for free. I just need your permission first before I begin my design. Hoping for your positive feedback. Thanks

Performance drawbacks

Try to implement calendar on nested RecyclerViews (outer RV+LinearLayoutManager hold month items & every month has its own inner RV+GridLayoutManager with its days) and compare performance of both implementation. Your variant with single RV+GridLayoutManager is much slower

CalendarView.setOnDateClickLIstener from Java

Hi,
Thanks for your support.

I have been using this function without problems so far using a lambda function with java 1.8.
But suddenly today this is giving an error.
My code use to work like this:

        calendarView.setOnDateClickListener(calendarDate -> {
            MeditationsDialog dialog = new MeditationsDialog(getActivity(), calendarDate.getDate());
            dialog.show();
            return null;
        });

But now I get an error like this:
Function1<? super CalendarDate, Unit> is not a functional interface

Any idea how to fix this?
Thanks

Translation

Is there a way to translate or set the names of the months and days? This is such a great project and I hope someone can help me.

Date scrolling issue

can i change vertically scrolling date to horizontal scroll with move month name in top left in side to year?

Disabled days

Thank you for this awesome library.

Is it possible to define disabled days? I have to select a range, but there may be days in the calendar that cannot be selected for some reason.

Theme Dark problems - Customizing all colors does not work

How can I set the weekend background colors?

If I use a dark theme I get this output out of the box:
image

So I need to set some colors that it looks OK:
Month Text Label Color --> app:calendar_month_text_color --> working
Date Text Color --> app:calendar_date_text_color --> not working --> working with a selector
Date Background --> app:calendar_date_background --> working

Weekend Date Background --> ??

Output is at the moment:
image

Not scrolling to current day.

Hi.
In setupCalendar() if initialDate and minDate are too far apart then it won't scroll to initialDate.
For example.
Set calendar with minDate as 1st of January of this year and initialDate as today (August 1st) then when loaded it won't scroll to today.
moveToDate() also won't scroll to that date.

Now, if we initialize calendar with minDate set as March and initialDate as August then when loaded we will see August on the screen.

I guess it has something to do with how many items recyclerView loads because after we scroll to today it all works fine.

Possible to add a "LongClickListener"

Can you please add a "LongClickListener" for the date selection?

A click selects the date but I have added the indicators and want to show the "Events" on this day with a long click of the user.

So short click selects the date and long click would open a popup with the events.

Adding events after loading JSON data

First of all, I really love this calendar. I have tried a lot of calendars in the past few days but this was by far the best and easiest to implement.

I am currently having an issue with reloading the event indicators after loading JSON from the google Calendar API. The calendar runs the generateCalendarDateIndicators() function before the data is there however when I run it manually after I have the data, no new dots appear. I am currently running the code inside of its own activity instead of a fragment.

I am imagining that I need to have the calendar refresh the UI once I have the JSON data. I have tried a few different ideas for refreshing the UI but they all lead to refreshing the entire page which puts me in a big circle.

Any thoughts or suggestions would be well appreciated.

Is it possible to go to next or previous month instead of year when using the arrows that are next to month?

I mean when pressing the arrow shown in the attached image you would be able to go to next month:

arrowcalendar

Would it be posible to change calendar behavior so it just displays one month at once (user can just scroll for that month) and pressing that arrow would show next month? (For this case let's consider that the shown month would be June 2018 and when the arrow were to be pressed it would show July 2018).

Crash in CalendarDateView

Hello,
Since version 2.2 there is an issue with onCreateDrawableState in CalendarDateView. To be exact in this line

java.lang.NullPointerException: Attempt to invoke virtual method 'int ru.cleverpumpkin.calendar.DateCellSelectedState.ordinal()' on a null object reference at ru.cleverpumpkin.calendar.CalendarDateView.onCreateDrawableState(CalendarDateView.kt:177) at android.view.View.getDrawableState(View.java:23776) at android.view.View.initializeScrollbarsInternal(View.java:6711) at android.view.View.<init>(View.java:6103)

It happens when CalendarDateView is being created with scrollbars. As you can see in stacktrace this triggers onCreateDrawableState() to be called before cellSelectionState is initialized.

Fix suggestion: block scrollbars by setting scrollbars to none for CalendarDateView

Logo Design: CrunchyCalendar

Hello!

I examined your project and I saw that Crunchy Calendar has no logo design yet. I'm a graphic designer and I like to support open source projects. I would like to design a logo for your project if you interested, I will be happy to work with you! :)

Best Regards

Baran Pirinçal

Graphic Designer

Timezone settings

Hello again!

How can we deal with different timezones? Is there any built-in solution or what is the suggested method?

Thank you.

Disabled day cell with custom background color

Thank you for this awesome library again. :)

Is there any way to set the 'disabled days' cell's background color conditionally?
Example: one of them 'reserved' (red), another one is 'processing' (orange) stb..

Thank you.

Date Range Selection and dateSelectionFilter

Hi,

Thanks for the great library.

Is it possible to disallow users to select date ranges if one or more of the days in the selected range is not selectable?

So, let's say, I'm setting my dateSelectionFilter to not allow selecting date 17/06/2019. In that case, user should not be able to select a range like between 15/06/2019 and 19/06/2019 or a wider range because that one not selectable day is in the range.

I checked and couldn't find a solution for it. Is it possible to do that at the moment or do we need an update?

I can still manually achieve this by after user makes a range selection, I can loop through the selected days and check them one-by-one, but just wondered if the CalendarView have this feature already.

How to display a single month?

Is there a way to display a single month, such as
image

?

I want to create an events-like calendar, and I want the user to browse month by month, as I'll display information below the calendar.

Is this possible / planned with Crunchy?

Impossible to customize some colors

Hello!
Why impossible to customize colors:
"calendar_date_selected_background"
and probably "calendar_date_today_text_color"
Hardcoded light blue color not suitable for some color themes.
I don’t understand how to change it.

How to set a specific color background for specific dates?

I have a REST service that returns an array of dates and an int value, depending of that value, the date will have a specific color. Example:

if the value is between 0 and 50, the color will be green.
if the value is between 51 and 100 the color will be yellow.

and so on.

PD. The library is awsome, the only thing I have left to implement is this, help :(.

Thanks!

Feature suggestions

thx for the library i just wanted to share our requirements, maybe it is interesting for you. All come from real business cases:

  • being able to quickly navigate years(e.g. to select birthday without needing to scroll/click thousand times)
  • filter to select what dates should not be allowed. either by manually specifying dates or provide defaults((e.g. weekends, holidays)

create event in calendar

Hello,
From server i got some date and i want to show this date as a event and mark it. I want to use java but i can't find a helpful sample to add event into the calendar.
I am stack on this. say i have a array of date i want to show this date into the calendar with a mark

Thanks in advance

Can I add a selection programmatically?

Hello,

I would like to select all the week (Monday to sunday) when a user clicks on a day.

I'm trying to select all days with onDateClickListener and setupCalendar but it's not work.

Do you have any idea? :/

Entry in month view to reflect time of entry

Hi,

Again thanks for such a good work.

One feature I like about the google calendar app is that the entry's time will change where in the day it appears. For instance an entry early in the day will be shown high up and a entry at noon should be in the middle of the day view and a entry at night should be by the end of the day box.

Thanks!

Font Attributes

Is there a way to change the font family, text size, etc. for the DaysBarView and the header (month/year) of the calendar? I would like to match the calendar text attributes to the rest of my application,

How to set "firstdayofweek"

Thanks for this great library.

I have just added it to my project but I don't find a way to set the first day of week.
How is this possible?

Multiple date range selection

Hi, this library is so cool, but i need to display multiple date range selection on my app, do you think it's possible?

OnDateClickListener Method In Java

Please help me with the OnDateClickListener method in java.... please explain the parameters used in the method.....it is not explained here.

It looks like:

// Set date click callback 
calendarView.onDateClickListener = { date ->

    // Do something ... 
    // for example get list of selected dates
    val selectedDates = calendarView.selectedDates 
}

It is not compatible with java and what are the parameters passed in this method??

Remove Month label/empty space

Hi,
First of all thank you for providing such a nice calendar for the people.

I would like it if it was possible to remove the month label and the extra wasted spaces that defines a new month. It would be nice if the month could be presented transparently behind the month or at some other place.

Thank you

Event indicator types and increased indicator count

I've been working with Crunchy Calendar for a couple days and am fitting it into my use case where I need square event indicators and possibly more than 4 indicators showing on each calendar cell.

Is this something that you might have on the roadmap for this library or would you be willing to look at a PR if I had some suggested changes to have event indicator types? Or would I be better off maintaining my fork separately?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.