This article shows how to create a CalenderPickerView using TimeSquare Library. We have seen the use of CalenderPickerView to select a date in many applications. With the help of this library, we can easily add a calendar in our app.

Approach:
-
Add the support Library in build.gradle file and add dependency in the dependencies section. This library provides the inbuilt calendar widget and various functions such as to select a particular date, etc.
dependencies {implementation 'com.squareup:android-times-square:1.6.5@aar'}chevron_rightfilter_none - Now add the following code in the activity_main.xml file. This will add the CalendarPickerView Layout in the app.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.squareup.timessquare.CalendarPickerViewandroid:id="@+id/calendar"android:layout_width="match_parent"android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout>chevron_rightfilter_none - Now add the following code in the MainActivity.java file. This will show the calender of next one year from the current day’s date. A setOnDateSelectedListener is added in the calendar which is invoked when the user clicks on any date. The function Toasts the selected day’s date on the screen.
MainActivity.java
packageorg.geeksforgeeks.gfgCalenderPickerView;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;importcom.squareup.timessquare.CalendarPickerView;importjava.text.DateFormat;importjava.util.Calendar;importjava.util.Date;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// This will return us today dateDate today =newDate();Calendar nextYear= Calendar.getInstance();// This will help us// to show date from// today to next yearnextYear.add(Calendar.YEAR,1);CalendarPickerViewdatePicker= findViewById(R.id.calendar);// we have to initialize// our calender picker view// so we select min date as today// max date as next year// we call getTime() method// because we want to// retrieve date from it.datePicker.init(today, nextYear.getTime()).inMode(CalendarPickerView.SelectionMode.RANGE).withSelectedDate(today);// when the user select// or un select any date then// this method called automatically.datePicker.setOnDateSelectedListener(newCalendarPickerView.OnDateSelectedListener() {@OverridepublicvoidonDateSelected(Date date){// we have to format our date// object that's why we are// using DateFormat class.String selectedDate= DateFormat.getDateInstance(DateFormat.FULL).format(date);Toast.makeText(MainActivity.this,selectedDate,Toast.LENGTH_SHORT).show();}@OverridepublicvoidonDateUnselected(Date date){}});}}chevron_rightfilter_none
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to create a Circular image view in Android without using any library?
- How to create popup message using Alerter Library in android
- How to Create AlertDialog Box Using SweetAlert Dialog Library?
- How to Create a CircularImageView in Android using hdodenhof Library?
- Java program to check palindrome (using library methods)
- How to add Cutsom Switch using IconSwitch Library in android?
- Image loading and caching library for Android | Set 1
- Image loading and caching library for Android | Set 2
- Guava Library in Java
- Volley Library in Android
- Java SAX Library
- How to add Slidr Library in Android?
- JSwing | Create a Magnifying tool using Java Robot
- How to create a Facebook login using an Android App?
- How to create a Stopwatch App using Android Studio
- How to create a REST API using Java Spring Boot
- How to create a Face Detection Android App using Machine Learning KIT on Firebase
- Does JVM create object of Main class (the class with main())?
- How to create Immutable class in Java?
- ProcessBuilder in Java to create a basic online Judge
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

