You need to add a DotSpan. Also i had to read the library docs cause i haven't used this myself.
Step 1: In your Fragment class
private List<CalendarDay> events = new ArrayList<>();
Step 2 :
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
// i copied your json to load form assets folder
// in our case you can get the json from the server
// or any other location of your choice
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
events.add(day);
}
} catch (Exception e) {
e.printStackTrace();
}
EventDecorator eventDecorator = new EventDecorator(Color.RED, events);
calendarView.addDecorator(eventDecorator);
}
Step 3: Change your EventDecorator class
public class EventDecorator implements DayViewDecorator {
private int color;
private HashSet<CalendarDay> dates;
public EventDecorator(int color, Collection<CalendarDay> dates) {
this.color = color;
this.dates = new HashSet<>(dates);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}
@Override
public void decorate(DayViewFacade view) {
view.addSpan(new DotSpan(5, color));
}
}
You can look at the DotSpan and customize it your self. Its a LinearBackgroundSpan
Screen shot on my emulator
Edit:
And specific month should list specific month event rather than all
the event ,which is the one i am getting on Currently.
You need to check the month that is currently displayed and make sure you sync your listview to display events accordingly.Clear the list data that is used to display events in list based on the month the calendar displays and refresh your list.
Or you could just display the events for the day on click of date.
How you do it is left to you. You can have a look at this https://github.com/memfis19/Cadar library which should give an insight or help you modify your code accordingly. Based on the month displayed the list displays events.
Edit 2:
My Adapter class
public class MyAdapter extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<Event> list) {
super(context, R.layout.row, list);
this.mInflater = LayoutInflater.from(context);
this.list = list;
}
static class ViewHolder {
TextView text;
}
public void addItems(List<Event> list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent,false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getEvents());
return convertView;
}
}
My CalendarFragment
public class CalendarFragment extends Fragment implements OnMonthChangedListener {
private MaterialCalendarView calendarView;
private List<CalendarDay> calevents = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private HashMap<Integer,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
private Calendar cal;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = (ListView)view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),eventList);
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
final Calendar calendar = Calendar.getInstance();
calendarView.setSelectedDate(calendar.getTime());
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
}
});
calendarView.setOnMonthChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
Date date = simpleDateFormat.parse(StartDate);
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
if(!map.containsKey(month))
{
List<Event> events = new ArrayList<>();
events.add(event);
map.put(month,events);
}else
{
List<Event> events = map.get(month);
events.add(event);
map.put(month,events);
}
calevents.add(day);
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
EventDecorator eventDecorator = new EventDecorator(Color.RED, calevents);
calendarView.addDecorator(eventDecorator);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("testjson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
}
}