Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the text color of calendar view month name

Tags:

java

android

In android , i am using an default CalendarView. I have set the background to light gray color. But can i change the color of month view of calendar view?

like image 206
Sandeep Dhull Avatar asked Nov 16 '12 06:11

Sandeep Dhull


People also ask

How do I change the color of calendar dates?

All you need to do then is draw the listed colours on the canvas in the WeekView class method called drawWeekNumbersAndDates() for the specified dates using the existing for loop ( for (; i < nDays; i++) ) and iterate over the Map and change the paint colour for the date text i.e. mMonthNumDrawPaint. setColor().

How do I change the calendar color on my Android?

Change the Google Calendar Default Color on Mobile The steps are the same for the Google Calendar app on both Android and iOS. Tap the menu button on the top left and select Settings near the bottom. Below the calendar you want to change, tap Events. Tap Color at the top and pick a new color.


1 Answers

As you've found out, the TextView is private, and there does not seem to be any methods for accessing it.

Although I wouldn't recommend it, you can accomplish this using the java.lang.reflect package:

    try
    {
        CalendarView cv = (CalendarView) this.findViewById(R.id.calendarView1);
        Class<?> cvClass = cv.getClass();
        Field field = cvClass.getDeclaredField("mMonthName");
        field.setAccessible(true);

        try
        {
            TextView tv = (TextView) field.get(cv);
            tv.setTextColor(Color.RED);
        } 
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
    catch (NoSuchFieldException e)
    {
        e.printStackTrace();
    }
like image 60
Ryan Avatar answered Oct 06 '22 02:10

Ryan