Display the List of Events Within the Calendar?
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);
}
}
Android CalendarView with events
You can use existing library as you can refer here http://www.viralandroid.com/2015/11/android-calendarview-example.html
for more customize you can use this library
https://github.com/prolificinteractive/material-calendarview
Android CalendarView for Showing Events
The built in CalendarView widget doesn't expose the ability to change the color of the calendar cells in month view. See the source.
You might try the following approaches:
Extend from CalendarView and implement onDraw yourself. You'll have to reimplement all the existing drawing behavior, including these 4 methods:
protected void onDraw(Canvas canvas) {
drawBackground(canvas);
drawWeekNumbersAndDates(canvas);
drawWeekSeparators(canvas);
drawSelectedDateVerticalBars(canvas);
}Implement you own CalendarView that allows customizing cells. I suggest this project as a good place to get started, since it already supports custom cells.
How do I display events to a specific date and time in a MaterialCalendarView?
The example in the repo filters the events using the months. You need to filter events by calendar day and update listview in onDateSelected
to update listview whenever user selects a different date. You should get something like this:
public class CalendarFragment extends Fragment implements OnDateSelectedListener {
private MaterialCalendarView calendarView;
private HashMap<CalendarDay,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),new ArrayList<Event>());
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
calendarView.setSelectedDate(LocalDate.now());
calendarView.setOnDateChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
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");
LocalDate date = LocalDate.parse(StartDate, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss",Locale.US));
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
if(!map.containsKey(day))
{
List<Event> events = new ArrayList<>();
events.add(event);
map.put(day,events);
}else
{
List<Event> events = map.get(day);
events.add(event);
map.put(day,events);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
List<Event> event = map.get(CalendarDay.from(LocalDate.now()));
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
//add small dots on event days
EventDecorator eventDecorator = new EventDecorator(Color.RED, map.keySet());
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 onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
List<Event> event = map.get(date);
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
}
}
Android - Show events by hour in calendarView?
Have you look around this library?
Add event to CalendarView
You can't add events in default CalendarView
. either you need to make it custom or you need to use some library.
I have used Caldroid library many times.it is easy to implement and robust.
Related Topics
Google Play Game Services - Unable to Sign In
How to Make an Imageview in Circular Shape
Android M Permissions:Confused on the Usage of Shouldshowrequestpermissionrationale() Function
Creating a Custom Layout for Preferences
Android Studio:How to Uninstall APK (Or Execute Adb Command) Automatically Before Run or Debug
How to Fix "Unexpected Element <Queries> Found in <Manifest>" Error
Blur or Dim Background When Android Popupwindow Active
Android; Check If File Exists Without Creating a New One
Making Data Persistent in Android
Listview with Add and Delete Buttons in Each Row in Android
Restrict API Requests to Only My Own Mobile App
Android Studio How to Package Single Aar from Multiple Library Projects
Firebase (Fcm) How to Get Token
How to Implement Filterable in Realmrecyclerviewadapter