Composables
Docs Login
Tally

Month Calendar

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.composables.tally.theme.CaretLeft
import com.composables.tally.theme.CaretRight
import com.composables.tally.theme.Icons
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.LocalDate
import kotlinx.datetime.YearMonth
import kotlinx.datetime.minus
import kotlinx.datetime.minusMonth
import kotlinx.datetime.plus
import kotlinx.datetime.plusMonth
import kotlinx.datetime.yearMonth

@Composable
fun MonthCalendar(
    month: YearMonth,
    today: LocalDate,
    isDone: (LocalDate) -> Boolean,
    color: Color,
    onMonthChange: (YearMonth) -> Unit,
    modifier: Modifier = Modifier,
) {
    val first = month.firstDay
    val start = first.minus(DatePeriod(days = first.dayOfWeek.ordinal))
    val weeks = (first.dayOfWeek.ordinal + month.numberOfDays + 6) / 7
    Column(modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(Space * 2)) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Column(Modifier.weight(1f)) {
                Text(
                    month.year.toString(),
                    style = Theme.typography.caption,
                    color = Theme.colors.onBackgroundMuted,
                )
                Text(
                    month.month.name.lowercase().replaceFirstChar { it.uppercase() },
                    style = Theme.typography.title,
                )
            }
            IconButton(
                onClick = { onMonthChange(month.minusMonth()) },
                contentDescription = "Previous month",
            ) {
                Icon(Icons.CaretLeft, contentDescription = null)
            }
            IconButton(
                onClick = { onMonthChange(month.plusMonth()) },
                contentDescription = "Next month",
                enabled = month < today.yearMonth,
            ) {
                Icon(Icons.CaretRight, contentDescription = null)
            }
        }
        Row(Modifier.fillMaxWidth()) {
            DayOfWeek.entries.forEach { day ->
                Box(Modifier.weight(1f).padding(vertical = Space), Alignment.Center) {
                    Text(
                        day.name.take(1),
                        style = Theme.typography.caption,
                        color = Theme.colors.onBackgroundMuted,
                    )
                }
            }
        }
        repeat(weeks) { week ->
            Row(Modifier.fillMaxWidth()) {
                repeat(7) { weekday ->
                    val date = start.plus(DatePeriod(days = week * 7 + weekday))
                    Box(Modifier.weight(1f).aspectRatio(1f), contentAlignment = Alignment.Center) {
                        if (date.yearMonth == month)
                            CalendarDay(date, done = isDone(date), today = date == today, color)
                    }
                }
            }
        }
    }
}

@Composable
private fun CalendarDay(date: LocalDate, done: Boolean, today: Boolean, color: Color) {
    val label = "${date.month.name.lowercase().replaceFirstChar { it.uppercase() }} ${date.day}"
    Box(
        Modifier.padding(3.dp)
            .fillMaxWidth()
            .aspectRatio(1f)
            .clip(Theme.shapes.fullyRounded)
            .then(if (done) Modifier.background(color) else Modifier)
            .then(
                if (today) Modifier.border(1.5.dp, Theme.colors.primary, Theme.shapes.fullyRounded)
                else Modifier
            )
            .semantics { contentDescription = if (done) "$label, done" else label },
        contentAlignment = Alignment.Center,
    ) {
        Text(
            date.day.toString(),
            style = Theme.typography.label,
            color = if (done) Theme.colors.onBackground else Theme.colors.onBackgroundMuted,
        )
    }
}

More components from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.