Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Implement Date Range Picker in Android?
Next article icon

How to Implement Custom Calendar in Android?

Last Updated : 28 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 


Next Article
How to Implement Date Range Picker in Android?
author
encrypter09
Improve
Article Tags :
  • Java
  • Android
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences