Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android calendar view date color

I have a list of dates that I want to color in red on calendarview . how can I do ?

my activity..

public class Calendario extends Activity {

    RelativeLayout rl;
    final Calendar calendar = Calendar.getInstance();
    CalendarView cal;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar);

        rl = (RelativeLayout) findViewById(R.id.rl);
        cal = new CalendarView(Calendario.this);

        rl.addView(cal);
        cal.setOnDateChangeListener(new OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                int dayOfMonth) {
            // TODO Auto-generated method stub

        }
    });
    }


}

What code i've to add in order to color a date ?

like image 771
Massimiliano Smillovich Avatar asked Dec 03 '25 09:12

Massimiliano Smillovich


2 Answers

Your best bet might be to create your own CalendarView class i.e. CalendarViewCustom based on the source code found here.

You could then add an extra method to the LegacyCalendarViewDelegate class similar to the setFocusedMonthDateColor() method to iterate through the weeks and set a date and colour in the WeekView class (might be worth storing these key/value date/colours as an Map collection instance variable in the WeekView class). e.g.

public void setMonthDateColor(Date date, int color) {                
    final int childCount = mListView.getChildCount();
    for (int i = 0; i < childCount; i++) {
        WeekView weekView = (WeekView) mListView.getChildAt(i);
        if (weekView.isDateInWeek(date)) {
            //this method adds the date and colour to a 
            //Map collection in weekView Object 
            weekView.setDateColour(date, color);
        }
    }
}

The above method then needs to be exposed by adding another method to the parent class CalendarViewCustom (similar to its existing methods) which can then be called on an instance of the class i.e.

public int setMonthDateColor(Date date, int color) {
    return mDelegate.getMonthDateColor(date, color);
}

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().

Hope this points you in the right direction.

like image 192
Andy B Avatar answered Dec 04 '25 23:12

Andy B


Try this. Hope this helps

android:weekDayTextAppearance="@style/CalendarWeekDateText"


<style name="CalendarWeekDatText" parent="TextAppearance.AppCompat.Button">
    <item name="android:textColor">#000000</item>
</style>
like image 38
sreejith Avatar answered Dec 05 '25 00:12

sreejith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!