Search code examples
javaandroidcalendarviewcaldroid

Background drawable for Caldroid


Hi i am using Caldroid library for customizing calendar view in my habit tracker app and there are two hash maps successMap and failureMap representing successful and failed days of a habit. I am setting green circular background drawable for successful days and red circular background drawable for failed days but only one of them is shown which is written later in code. Here's that method for setting up calendar:

private void setCalendar(){
    Map<Date, Drawable> successMap = new HashMap<>();
    Map<Date, Drawable> failureMap = new HashMap<>();
    //For success days
    for(int i=0; i<successDays.size(); i++){
        successMap.put(successDays.get(i), getResources().getDrawable(R.drawable.green_circular));
    }
    //For failure days
    for(int i=0; i<failureDays.size(); i++){
        failureMap.put(failureDays.get(i), getResources().getDrawable(R.drawable.red_circular));
    }
    caldroidFragment.setBackgroundDrawableForDates(successMap);
    caldroidFragment.setBackgroundDrawableForDates(failureMap);
    caldroidFragment.refreshView();
}

In this only that dates are shown which are written later for example here only failed days will be shown.I have checked these arraylists and maps value and they are fine.So how can i resolve this problem?


Solution

  • After Checking the Caldroid Library I found that the Library every time you call setBackgroundDrawableForDates() it clear the previous List

    public void setBackgroundDrawableForDates(
            Map<Date, Drawable> backgroundForDateMap) {
        if (backgroundForDateMap == null || backgroundForDateMap.size() == 0) {
            return;
        }
    
        backgroundForDateTimeMap.clear();
    
        for (Date date : backgroundForDateMap.keySet()) {
            Drawable drawable = backgroundForDateMap.get(date);
            DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
            backgroundForDateTimeMap.put(dateTime, drawable);
        }
    }
    

    So the soluation to your problem is to append the two list togthers and call setBackgroundDrawableForDates() one time only to avoid the reset Like That

    private void setCalendar(){
            Map<Date, Drawable> successMap = new HashMap<>();
            Map<Date, Drawable> failureMap = new HashMap<>();
            //For success days
            for(int i=0; i<successDays.size(); i++){
                successMap.put(successDays.get(i), getResources().getDrawable(R.drawable.green_circular));
            }
            //For failure days
            for(int i=0; i<failureDays.size(); i++){
                failureMap.put(failureDays.get(i), getResources().getDrawable(R.drawable.red_circular));
            }
            successMap.putAll(failureMap);
            caldroidFragment.setBackgroundDrawableForDates(successMap);
            caldroidFragment.refreshView();
        }