Custom Calendar View With Events Android
This tutorial you will learn how to create a custom calendar view with events and notifications.You can use android Calendar Instance, and you can inflate calendar days of the month by using grid layout and linear layout manager. You can use any type of database whether it is local database like SQLite and ROOM database or remote database like MySQL or Firebase database. So the calendar has flexibility to add, update and delete user`s events.
The custom calendar view has been designed to allow the user to navigate through previous and next months. Also it allows the user to click on any day of the month to add new event. Custom calendar view with event also allow the user to long press on any day with added events to show the event previously added. At this point the user can delete events.
Another strong point of Custom Calendar View With Event is event alarm with notification. When the user create new event he can enable and disable the alarm for the event date. Also this the user can set alarm when he show the previously created events.
Thank you
Tutorial you may interested
Hi, Im having an error with the notification.
I followed the 4th video but in the end i have an error on method OnBindViewHolder is this lines:
Calendar datecalendar = Calendar.getInstance();
datecalendar.setTime(ConvertStringToDate(events.getDATE()));
final int alarmYear = datecalendar.get(Calendar.YEAR);
final int alarmMes = datecalendar.get(Calendar.MONTH);
final int alarmDia = datecalendar.get(Calendar.DAY_OF_MONTH);
Calendar timecalendar = Calendar.getInstance();
timecalendar.setTime(ConvertStringToTime(events.getTIME()));???????????????????????????????????????????????
final int alarmHour = timecalendar.get(Calendar.HOUR_OF_DAY);
final int alarmMinute = timecalendar.get(Calendar.MINUTE);
I wrote some interrogation in the line where i have the error, It says that NullPointerException because i cannot use method setTime on a null object reference
that means that convert string ti time returns null you need to make sure that the value of events.getTIME() returns the string time, make sure that the time saved correctly in sqlite database.
I have a problem when i want to show the number of events in under the day of the month.This is my code in getView for that
TextView Day_Number = view.findViewById(R.id.calendar_day);
TextView EventNumber = view.findViewById(R.id.id_event);
Day_Number.setText(String.valueOf(DayNo));
Calendar eventCalendar = Calendar.getInstance();
ArrayList arrayList = new ArrayList();
for (int i = 0;i < events.size();i++){
eventCalendar.setTime(ConvertStringToDate(events.get(i).getDATE()));
if(DayNo == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH)+1
&& displayYear == eventCalendar.get(Calendar.YEAR)){
arrayList.add(events.get(i).getEVENT());
EventNumber.setText(arrayList.size()+" Eventos");
}
}
For some reason it worked at firts but when i did the part of the notification suddenly it didnt work anymore.
Please can you clarify more i want to know if the app crashed or the events number is not appear and if the app crashed send me the errors from logcat.
Hello Good day, I encountered a problem where the CollectEventByDate method isn’t passing any data to display anything in the //recyclerView.setLayoutManager(layoutManager);
I did each step in the video and used try catch error to see if things works and the LongClickListener did where it displayed the event row layout. Did I trace it wrong? I’m not too sure where I messed up. I already made the RecycleView xml as well as the EventRecycler Adapter java step by step. Thanks for the feedback. The error is:
java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference
Make sure the id of the recycler view in the xml file that shows the events is the same in java file (RecyclerView recyclerView = showView.findViewById(R.id.EventsRV);).
Hello again, the id of the recycle view in xml is correct but I’m still getting the same error. I really have no idea why it’s not displaying the data, is it perhaps that the data is not saving in the database? The CollectEventsByDate method may be passing an empty data hence the null…But I’m unsure which part it stops…
Hello I solved the previous problem I had, thank you for that, but I encountered another problem with the Cursor. Here’s the error:
java.lang.IllegalStateException: Couldn’t read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
if there are changes in create query strings upgrade the database version. else make sure the create query strings are right.
It works now, thank you so much!
Hello again, I’m getting an error whenever I set up an Alarm.
Hello again, I’m getting an error whenever I set up an Alarm. Where do I add the column ID?
android.database.sqlite.SQLiteException: no such column: ID (code 1 SQLITE_ERROR): , while compiling: SELECT ID, EVENT_NOTIFY FROM EVENT_LIST WHERE DAY=? AND EVENT_NAME=? AND TIME=?
Thank you
Welldone
hi, when i try to start the app it crashed
is it possible to send the hold project instead?
Please follow the steps of handle errors page, and the full source code is available in the tutorial.
Hi, I think Tutorial is brilliant, you have done a great job,
But I am facing a logical error for quite some time now so could you plz help me.
In MyGridAdapter Java class the condition where you are checking if the DAY, MONTH and YEAR of DATECALENDAR are equal to EVENTCALENDAR is basically true all the time beacause they are both Initialized as getInstace(), so every time SetUpCalendar() is called that loop is adding an event in the all the arrayList, whether that call is for DELETING event, for NEXTBTN or for PREVIOUSBTN an event is added in every arrayList.
i have read the Documentation on you rwebsite and watched this video several time, could you plz look it up.
Thank you
Here is my Grid Adapter Code
public class DateGridAdapter extends ArrayAdapter {
List dates;
Calendar currentDate;
List events;
LayoutInflater inflater;
public DateGridAdapter(@NonNull Context context, List dates, Calendar currentDate, List events) {
super(context, R.layout.single_cell);
this.dates=dates;
this.currentDate=currentDate;
this.events=events;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Date monthDate = dates.get(position);
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(monthDate);
int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
int displayMonth = dateCalendar.get(Calendar.MONTH)+1;
int displayYear = dateCalendar.get(Calendar.YEAR);
int currentMonth = currentDate.get(Calendar.MONTH)+1;
int currentYear = currentDate.get(Calendar.YEAR);
View view = convertView;
if (view ==null){
view = inflater.inflate(R.layout.single_cell,parent,false);
}
if(displayMonth == currentMonth && displayYear == currentYear){
view.setBackgroundColor(getContext().getResources().getColor(R.color.calendarCell));
}
else
{
view.setBackgroundColor(Color.parseColor(“#ffffff”));
}
TextView Day_Number = view.findViewById(R.id.calendar_day);
TextView EventNumber = view.findViewById(R.id.events_id);
Day_Number.setText(String.valueOf(DayNo));
Calendar eventCalendar = Calendar.getInstance();
ArrayList arrayList = new ArrayList();
for (int i = 0;i < events.size();i++){
eventCalendar.setTime(ConvertStringToDate(events.get(i).getDATE()));
if(DayNo == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH)+1
&& displayYear == eventCalendar.get(Calendar.YEAR)){
arrayList.add(events.get(i).getEVENT());
EventNumber.setText(arrayList.size()+" Events");
}
}
return view;
}
private Date ConvertStringToDate(String eventDate){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = null;
try {
date = format.parse(eventDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
@Override
public int getCount() {
return dates.size();
}
@Override
public int getPosition(@Nullable Object item) {
return dates.indexOf(item);
}
@Nullable
@Override
public Object getItem(int position) {
return dates.get(position);
}
}
I keep having an issue with the ReadEventsperMonth method. My logcat says
android.database.sqlite.SQLiteException: no such column: event (code 1 SQLITE_ERROR): , while compiling: SELECT event, time, date, month, year FROM eventstable WHERE month=? and year =?
and I have checked the syntax of the String Selection and it seems to be correct but I still can’t get it to work. Could you help?
You are genius
Thanks, man for this nice tutorial. I want to highlight the current date of the month in the Gridview just like the android calendar. Where I have to add code for the current day.
add variable of current day from current date
int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
then add another conditon
if(displayMonth == currentMonth && displayYear == currentYear){
view.setBackgroundColor(getContext().getResources().getColor(R.color.green));
} else if(DayNo == currentDay){
view.setBackgroundColor(getContext().getResources().getColor(android.R.color.holo_orange_dark));
}
else
{
view.setBackgroundColor(Color.parseColor(“#cccccc”));
}
Hey, so when the app launches the events are on the calendar. If I go to another activity and back the events disappear until the next time the app is launched. How would I correct this?
Very nice setup. However, when I leave the activity and come back the events are gone. Is there a way to fix this without having the user exit the app and go back in?
Hi! I have a problem with the show events activity.
I had working the app but then I made changes in the data base to add new variables and now I can,t see anything in the row_layout when I press the screen. Only a white row.
I read again and again de cde but it seems everything It,s ok.
I also updated the version of the db, but stil not working.
make sure that data saved to the right columns in the database. open emulator and try to add events. the open android device monitor to extract the database file to desktop, install DB browser for SQLite application that helps you to show the data in the database. if events data saved to wrong columns it is expected to collect zero rows.
this video helps you how to extract database.
https://youtu.be/7Ua98YBJF50
Ok thaks I’ll try It yo see If It works.
Thank you!
Looks like the database is empty in sqlite admin. Which could be the error?
It´s my first database and I,m a little lost
I have checked the database with db browser and the columns get the information. But aren`t in order and don´t know why. I think I write the new columns in the right order, right below the existing ones.
Is it possible to reorder the columns without making a new data base? And how can I do that?
Hello, I have followed this tutorial but I have a problem, which is “java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference” This error only occurs when you want to see the events on a day that has events, what can be the problem?
I found the error, it was that the inflate addressed the wrong one, thanks 🙂
I’m glad that the problem was solved!
If you have other questions, just let me know!
sir if this application is running for you, could you please send me the zip file at rimshafaisal.246@gmail.com
my app is getting crashed, i don’t know why, i have followed every step.
i would really appreciate it if you could help me out.
Assalamuallikum,
great video sir,
My app getting crashed
i have copied your code just like in videos,but following are my problems:
at main activity.xml:
after writing this code i cannt got any layout ,i have added all dependency as you have written
and also:
in Customcalenderview:
Log.d(TAG, “getRequestCode: “+code);
i am having error saying,that Tag has not initialized.
please help me
sir if this application is running for you, could you please send me the zip file at rimshafaisal.246@gmail.com
my app is getting crashed, i don’t know why, i have followed every step.
i would really appreciate it if you could help me out.
Hello Good day, I encountered a problem where the CollectEventByDate method isn’t passing any data to display anything in the //recyclerView.setLayoutManager(layoutManager);
I did each step in the video and used try catch error to see if things works and the LongClickListener did where it displayed the event row layout. Did I trace it wrong? I’m not too sure where I messed up. I already made the RecycleView xml as well as the EventRecycler Adapter java step by step. Thanks for the feedback. The error is:
java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference
Hello Good day, I encountered a problem where the CollectEventByDate method isn’t passing any data to display anything in the //recyclerView.setLayoutManager(layoutManager);
I did each step in the video and used try catch error to see if things works and the LongClickListener did where it displayed the event row layout. Did I trace it wrong? I’m not too sure where I messed up. I already made the RecycleView xml as well as the EventRecycler Adapter java step by step. Thanks for the feedback. The error is:
java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference