How to Implement Custom Calendar in Android?
Last Updated : 28 Jan, 2022
Nowadays in most of the apps, we see Calendar in most of the apps for displaying birth dates or for any appointment application. Displaying the date in the app with the help of the Calendar view gives a better user experience. In this article, we are going to make a custom calendar for our android application. In this custom calendar, we have highlighted the current date and other dates which seemed important to us.
What we are going to build in this article?
Here we have added the required dependency for the custom calendar and then worked on it using the calendar view. Here is a sample video of the application which we are going to build. Note that we are going to implement this project in Java Language.
Step by Step Implementation
Step 1: Create a new project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Adding required dependency-
Navigate to Gradle Scripts -> build.gradle(Module) and implement the following dependency in it-
implementation 'org.naishadhparmar.zcustomcalendar:zcustomcalendar:1.0.1'
Step 3: Working on XML files
Working with actvity_main.xml file:
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <org.naishadhparmar.zcustomcalendar.CustomCalendar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/custom_calendar" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
Navigate to app > res > layout > right click > new > layout resource file and name it as absent_view.xml. Use the following code in it-
XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="2dp" android:padding="2dp"> <androidx.cardview.widget.CardView android:layout_width="40dp" android:layout_height="32dp" android:layout_margin="4dp" app:cardCornerRadius="4dp" app:cardBackgroundColor="@android:color/holo_red_light"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/text_view" android:textColor="@color/white" android:gravity="center"/> </androidx.cardview.widget.CardView> </RelativeLayout>
Navigate to app > res > layout > right click > new > layout resource file and name it as present_view.xml. Use the following code in it-
XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="2dp" android:padding="2dp"> <androidx.cardview.widget.CardView android:layout_width="40dp" android:layout_height="32dp" android:layout_margin="4dp" app:cardCornerRadius="4dp" app:cardBackgroundColor="@android:color/holo_green_light"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/text_view" android:textColor="@color/white" android:gravity="center"/> </androidx.cardview.widget.CardView> </RelativeLayout>
Navigate to app > res > layout > right click > new > layout resource file and name it as current_view.xml. Use the following code in it
XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="2dp" android:padding="2dp"> <androidx.cardview.widget.CardView android:layout_width="40dp" android:layout_height="32dp" android:layout_margin="4dp" app:cardCornerRadius="4dp" app:cardBackgroundColor="@android:color/holo_blue_light"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/text_view" android:textColor="@color/white" android:gravity="center"/> </androidx.cardview.widget.CardView> </RelativeLayout>
Navigate to app > res > layout > right click > new > layout resource file and name it as default_view.xml. Use the following code in it-
XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="2dp" android:padding="2dp"> <androidx.cardview.widget.CardView android:layout_width="40dp" android:layout_height="32dp" android:layout_margin="4dp" app:cardCornerRadius="4dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/text_view" android:textColor="@color/black" android:gravity="center"/> </androidx.cardview.widget.CardView> </RelativeLayout>
Step 4: Working with MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java package com.example.customcalendar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import org.naishadhparmar.zcustomcalendar.CustomCalendar; import org.naishadhparmar.zcustomcalendar.OnDateSelectedListener; import org.naishadhparmar.zcustomcalendar.Property; import java.util.Calendar; import java.util.HashMap; public class MainActivity extends AppCompatActivity { // Initialize variable CustomCalendar customCalendar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // assign variable customCalendar=findViewById(R.id.custom_calendar); // Initialize description hashmap HashMap<Object, Property> descHashMap=new HashMap<>(); // Initialize default property Property defaultProperty=new Property(); // Initialize default resource defaultProperty.layoutResource=R.layout.default_view; // Initialize and assign variable defaultProperty.dateTextViewResource=R.id.text_view; // Put object and property descHashMap.put("default",defaultProperty); // for current date Property currentProperty=new Property(); currentProperty.layoutResource=R.layout.current_view; currentProperty.dateTextViewResource=R.id.text_view; descHashMap.put("current",currentProperty); // for present date Property presentProperty=new Property(); presentProperty.layoutResource=R.layout.present_view; presentProperty.dateTextViewResource=R.id.text_view; descHashMap.put("present",presentProperty); // For absent Property absentProperty =new Property(); absentProperty.layoutResource=R.layout.absent_view; absentProperty.dateTextViewResource=R.id.text_view; descHashMap.put("absent",absentProperty); // set desc hashmap on custom calendar customCalendar.setMapDescToProp(descHashMap); // Initialize date hashmap HashMap<Integer,Object> dateHashmap=new HashMap<>(); // initialize calendar Calendar calendar= Calendar.getInstance(); // Put values dateHashmap.put(calendar.get(Calendar.DAY_OF_MONTH),"current"); dateHashmap.put(1,"present"); dateHashmap.put(2,"absent"); dateHashmap.put(3,"present"); dateHashmap.put(4,"absent"); dateHashmap.put(20,"present"); dateHashmap.put(30,"absent"); // set date customCalendar.setDate(calendar,dateHashmap); customCalendar.setOnDateSelectedListener(new OnDateSelectedListener() { @Override public void onDateSelected(View view, Calendar selectedDate, Object desc) { // get string date String sDate=selectedDate.get(Calendar.DAY_OF_MONTH) +"/" +(selectedDate.get(Calendar.MONTH)+1) +"/" + selectedDate.get(Calendar.YEAR); // display date in toast Toast.makeText(getApplicationContext(),sDate, Toast.LENGTH_SHORT).show(); } }); } }
Here is the final output of our application.
Output:
Similar Reads
How to Implement Custom Dialog Maker in Android?
In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional in
5 min read
How to Implement Country Code Picker in Android?
Country Code Picker (CCP) is an android library that helps users to select country codes (country phone codes) for telephonic forms. CCP provided a UI component that helps the user to select country codes, country flags, and many more in an android spinner. It gives well-designed looks to forms on t
3 min read
How to Set Calendar Event in Android?
In most Android devices, a Calendar is an application that displays a calendar and is most commonly used to set reminders or events. Calendar applications in general are very light-weighted and consume less memory. Calendar has a background thread running that keeps a track of when to alert the user
3 min read
How to Implement Date Range Picker in Android?
Date Range Picker is a widely used feature in many popular Android apps and an essential component of Material Design. It allows users to select a range of dates such as a start and end date for various purposes including scheduling, filtering data, and setting time boundaries. Some Of The Real Life
4 min read
How to Implement ClockAnimationView Library in Android?
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
2 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement OTP View in Android?
An OTP View or PinView in android is a widget that allows users to enter their PIN, OTP, etc. They are generally used for two-factor authentication or phone number verification by OTP. A sample video is given below to get an idea about what we are going to do in this article. Note: In order to imple
2 min read
How to Implement Google Map Inside Fragment in Android?
In Android, the fragment is the part of Activity that represents a portion of the User Interface(UI) on the screen. It is the modular section of the android activity that is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on the device screen size. The UI fl
4 min read
How To Install a Custom ROM on Android?
Looking to enhance your Android phone's performance or customize its features? One of the best ways to do that is by installing a custom ROM on Android. A custom ROM allows you to replace your device's original firmware with one that provides additional features, better performance, or a different l
9 min read
How to Create an ImageButton in Android?
Nowadays, image buttons play a big role in making the android application more interactive and user-friendly. Be it any social media app like Instagram or Facebook or any shopping app or streaming app, each application uses this feature widely. In this article, we will take a look at the implementat
3 min read