Kotlin CalendarView change month and day texts

386 Views Asked by At

I'm using CalendarView in my project. But in my language, month's and day's names are different. And already i want to make my app compatible with other languages. But i cant see anything in calendar view about 'changing texts'. Actually i was trying to do an event app. You know its classic, user selects a date and the date that selected one turns a different color. I downloaded a library for this but i delete it because of this text problem. I even tried to download the project as module and change its string values. I made it too but this time i cant use it instead of original library. Please if you know a way to change its text or an easy usable library, tell me.

1

There are 1 best solutions below

1
Youssef Wagih On

Here's a great library for calendar view kizitonwose/calendar You can customize freely the days label based on user locale also.

You should also use the daysOfWeek list values to set up the weekday titles, this way it matches what is shown on the CalendarView.

To set up the day of week titles, you can either use the month header which would show the titles on every month and allow the titles to scroll with the month, or you can show the title on a static view above the calendar. Both ways are covered below:

Setup days of week using a static view:

Step 1 Create your title text view in res/layout/calendar_day_title_text.xml.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center" />

Create a container resource to use the text in res/layout/calendar_day_titles_container.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="7"
    android:orientation="horizontal">
    
    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

    <include layout="@layout/calendar_day_title_text" />

</LinearLayout>

Add the titles container in the same layout as the CalendarView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/titlesContainer"
        layout="@layout/calendar_day_titles_container" />

    <com.kizitonwose.calendar.view.CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cv_dayViewResource="@layout/calendar_day_layout" />

</LinearLayout>

Step 2 Now you can set up the titles using the daysOfWeek list discussed previously:

val titlesContainer = findViewById<ViewGroup>(R.id.titlesContainer)
titlesContainer.children
    .map { it as TextView }
    .forEachIndexed { index, textView ->
        val dayOfWeek = daysOfWeek[index]
        val title = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.getDefault())
        textView.text = title
    }

for more reference please follow custom weekdays labels