0

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?

Sahil Shokeen
  • 336
  • 3
  • 22

1 Answers1

2

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();
    }
Ahmed Mahmoud
  • 111
  • 1
  • 7
  • i m trying to select only one date at a time and set selected date background color , but its applying color on all selected dates.If select 1 then change its background color but if i select 2 then color should only be applied on 2 and removed from 1 – Erum Feb 24 '17 at 07:33