JCalendar is simple Jetpack Compose library with plenty of modifications
You can clone this repository to try out the sample app with library implementation
To get a Git project into your build:
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.andrewafanasenko:jcalendar:$latest_version'
}
Basic implementation will look like:
@Composable
fun MainScreen() {
val calendarState = rememberJCalendarState(
startMonth = YearMonth.now(), // Can be adjusted
endMonth = YearMonth.now(), // Can be adjusted
firstDayOfWeek = DayOfWeek.MONDAY, // Can be adjusted
mode = CalendarMode.MONTH // Can be adjusted
)
// Listening for date selection can be made in following way:
LaunchedEffect(calendarState) {
snapshotFlow { calendarState.selectedDate }
.distinctUntilChanged()
.collect {
// Selected LocalDate will come here
}
}
LaunchedEffect(calendarState) {
snapshotFlow { calendarState.currentMonth }
.distinctUntilChanged()
.collect {
// Current YearMonth will come here
}
}
JCalendar(calendarState = calendarState)
}
If mode == CalendarMode.MONTH
then appearance of calendar will look like:
JCalendar
composable function can take following arguments:
@Composable
fun JCalendar(
modifier: Modifier = Modifier,
calendarState: JCalendarState = rememberJCalendarState(),
dayContent: @Composable (Day) -> Unit = { day: Day ->
DayContent(day) {
calendarState.selectDay(day)
}
},
outDayContent: (@Composable (Day) -> Unit)? = { day: Day ->
DayContent(day = day, defaultTextColor = Color.Gray) {
calendarState.selectDay(day)
}
},
dayOfWeekTitleContent: (@Composable (DayOfWeek) -> Unit)? = { dayOfWeek: DayOfWeek ->
DayOfWeekTitleContent(dayOfWeek)
},
)
-
dayContent
takesDayContent
composable function which represents month/week day and can be customized or replaced with own function. -
outDayContent
takesDayContent
composable function and represents day from previous or next month. Can be also customized or replaced with own function. Setting ofoutDayContent
will have no effect for calendar withCalendarMode.MONTH
mode. IfoutDayContent
is set tonull
- out days will be hidden. -
dayOfWeekTitleContent
takesDayOfWeekTitleContent
composable function and represents day title (such as S (Sunday), M(Monday)...). Can be also customized or replaced with own function. IfdayOfWeekTitleContent
is set tonull
- titles will be hidden
Month calendar without titles
val calendarState = rememberJCalendarState(
...
mode = CalendarMode.MONTH
)
JCalendar(
calendarState = calendarState,
dayOfWeekTitleContent = null
)
Week calendar without titles
val calendarState = rememberJCalendarState(
...
mode = CalendarMode.WEEK
)
JCalendar(
calendarState = calendarState,
dayOfWeekTitleContent = null
)
Month calendar without out days
val calendarState = rememberJCalendarState(
...
mode = CalendarMode.MONTH
)
JCalendar(
calendarState = calendarState,
outDayContent = null
)
Month calendar with customized days, out days, titles
@Composable
fun MonthCalendarWithModifiedDay() {
var selectedMonth by remember { mutableStateOf(YearMonth.now()) }
val currentMonth = YearMonth.now()
val calendarState = rememberJCalendarState(
startMonth = currentMonth.minusMonths(3),
endMonth = currentMonth.plusMonths(3),
mode = CalendarMode.MONTH
)
LaunchedEffect(calendarState) {
snapshotFlow { calendarState.currentMonth }
.distinctUntilChanged()
.collect {
selectedMonth = it
}
}
val emptyDates = listOf<LocalDate>(
LocalDate.now().minusDays(5),
LocalDate.now().minusDays(6),
LocalDate.now().minusDays(7),
LocalDate.now().plusDays(1),
LocalDate.now().plusDays(2),
LocalDate.now().plusDays(8),
LocalDate.now().plusDays(10),
)
@Composable
fun dayModifier(day: Day) = Modifier
.background(
color = if (day.isSelected) {
Color(0xFF006C4C)
} else {
MaterialTheme.colors.surface
},
shape = RoundedCornerShape(8.dp)
)
Column(
modifier = Modifier.background(Color.White)
) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.secondary)
.padding(vertical = 8.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "Month calendar with modified day",
style = MaterialTheme.typography.h6
)
}
Row(
modifier = Modifier
.fillMaxWidth()
.height(56.dp)
.padding(bottom = 8.dp)
.background(Color(0xFF89F8C7)),
verticalAlignment = Alignment.CenterVertically
) {
IconButton(
onClick = { calendarState.scrollBack() }
) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = null
)
}
Text(
modifier = Modifier.weight(1f),
text = selectedMonth.format(DateTimeFormatter.ofPattern("MMMM yyyy")),
style = MaterialTheme.typography.h5,
fontWeight = FontWeight.SemiBold,
textAlign = TextAlign.Center
)
IconButton(
onClick = { calendarState.scrollForward() }
) {
Icon(
imageVector = Icons.Default.ArrowForward,
contentDescription = null
)
}
}
JCalendar(
calendarState = calendarState,
dayContent = { day: Day ->
DayContent(
day = day,
modifier = dayModifier(day),
defaultTextColor = if (emptyDates.contains(day.date)) {
Color.Red
} else {
Color.DarkGray
},
selectedTextColor = Color.White,
textStyle = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Bold
),
size = 40.dp,
onClick = {
calendarState.selectDay(day)
}
)
},
outDayContent = { day: Day ->
DayContent(
day = day,
modifier = dayModifier(day),
defaultTextColor = Color.LightGray,
selectedTextColor = Color.White,
textStyle = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.Bold
),
size = 40.dp,
onClick = {
calendarState.selectDay(day)
}
)
},
dayOfWeekTitleContent = { dayOfWeek: DayOfWeek ->
DayOfWeekTitleContent(
dayOfWeek = dayOfWeek,
modifier = Modifier.padding(bottom = 8.dp),
dayOfWeekStyle = java.time.format.TextStyle.SHORT,
textColor = Color(0xFF002114),
textStyle = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight.SemiBold
)
)
},
)
}
}
MIT License
Copyright (c) 2023 Andrew Afanasenko
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.