Change CalendarView style

60,183

Solution 1

In my project I defined the attribute "android:calendarViewStyle" in my theme.

<style name="Theme.Custom" parent="@android:Theme">
  <item name="android:calendarViewStyle">@style/Widget.CalendarView.Custom</item>
</style>

<style name="Widget.CalendarView.Custom" parent="android:Widget.CalendarView">
    <item name="android:focusedMonthDateColor">@color/cs_textcolor</item>
    <item name="android:weekNumberColor">@color/red</item>
    <item name="android:weekDayTextAppearance">@style/TextAppearance.Medium</item>
    <item name="android:dateTextAppearance">@style/TextAppearance.Medium</item>
</style>

All styles possibilities are:

  • @attr ref android.R.styleable#CalendarView_showWeekNumber
  • @attr ref android.R.styleable#CalendarView_firstDayOfWeek
  • @attr ref android.R.styleable#CalendarView_minDate
  • @attr ref android.R.styleable#CalendarView_maxDate
  • @attr ref android.R.styleable#CalendarView_shownWeekCount
  • @attr ref android.R.styleable#CalendarView_selectedWeekBackgroundColor
  • @attr ref android.R.styleable#CalendarView_focusedMonthDateColor
  • @attr ref android.R.styleable#CalendarView_unfocusedMonthDateColor
  • @attr ref android.R.styleable#CalendarView_weekNumberColor
  • @attr ref android.R.styleable#CalendarView_weekSeparatorLineColor
  • @attr ref android.R.styleable#CalendarView_selectedDateVerticalBar
  • @attr ref android.R.styleable#CalendarView_weekDayTextAppearance
  • @attr ref android.R.styleable#CalendarView_dateTextAppearance

note: if showWeekNumber not work as xml style, you can set in the code with setShowWeekNumber(true).

Solution 2

I've been having a lot of trouble as well. While not perfect, and I haven't figured out how to modify every aspect, I got close. This is how I did it, in the project's styles.xml I added these styles:

<style name="CalenderViewCustom" parent="Theme.AppCompat">
    <item name="colorAccent">@color/white_30</item>
</style>

<style name="CalenderViewDateCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/white</item>
</style>

<style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small">
    <item name="android:textColor">@color/white_30</item>
</style>

Then, for the CalendarView I referenced those styles as so:

<CalendarView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/CalenderViewCustom"
    android:dateTextAppearance="@style/CalenderViewDateCustomText"
    android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"/>

You can add the other types of colors (primary/secondary) to CalenderViewCustom above.

Share:
60,183
personne3000
Author by

personne3000

Updated on August 03, 2020

Comments

  • personne3000
    personne3000 almost 4 years

    I'm trying to add a CalendarView in my application, which uses the Theme.Light theme. The problem is, the days numbers of this calendar are rendered in white, so while using a light theme, you can't see them.

    Right now, I have the following code in my XML layout file :

    <CalendarView
        android:id="@+id/calendar1"
        android:layout_width="500dp"
        android:layout_height="300dp"/>
    

    I tried to force the calendar theme like this :

    <CalendarView
        android:id="@+id/calendar1"
        android:layout_width="500dp"
        android:layout_height="300dp"
        android:theme="@android:style/Theme.Light" />
    

    But it doesn't change anything. I think I should do something with the android:dateTextAppearance property, so I tried this :

    <CalendarView
        android:id="@+id/calendar1"
        android:layout_width="500dp"
        android:layout_height="300dp"
        android:dateTextAppearance="@android:style/TextAppearance.Large.Inverse" />
    

    but it doesn't do anything either.

    Any ideas ?

    Thanks !

  • Innovative Thinker
    Innovative Thinker about 10 years
    Is it correct? <android.widget.CalendarView android:id="@+id/calendarView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView1" android:layout_marginTop="10dp" android:theme ="@style/Theme.Custom" />
  • Innovative Thinker
    Innovative Thinker about 10 years
    unable to change the color theme using the above solution
  • Skilo Skilo
    Skilo Skilo over 8 years
    Can you elaborate a little more on this? Where do i use all those "@attr" stuff? I'm not familiar with that at all.
  • naitbrahim
    naitbrahim about 3 years
    Thank you it's still working as of 2021 ^^
  • Aman Deep
    Aman Deep about 2 years
    in my case it did change the style, but not as expected