Custom Calendar View With Events Tutorial

Custom Calendar View With Events Android

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

37 thoughts on “Custom Calendar View With Events Android”

  1. 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

    1. 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.

  2. 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.

  3. 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

    1. 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);).

      1. 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…

  4. Adriane Troy U. Alariao

    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.

  5. 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=?

  6. 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

  7. 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);
    }
    }

  8. 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?

  9. 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.

    1. 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”));
      }

  10. 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?

  11. 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?

  12. 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.

    1. 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

      1. 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

      2. 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?

  13. 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?

  14. 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

  15. Mellissa kamilia

    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

  16. 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

Leave a Comment

Your email address will not be published. Required fields are marked *