This article shows how to create a calendarPickerView using TimeSquare Library. We have seen the use of calendarPickerView 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'
}
- Now add the following code in the activity_main.xml file. This will add the CalendarPickerView Layout in the app.
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
Â
ÂÂ Â Â Â
android:layout_width
=
"match_parent"
   Â
android:layout_height
=
"match_parent"
   Â
tools:context
=
".MainActivity"
>
Â
ÂÂ Â Â Â
<
com.squareup.timessquare.CalendarPickerView
       Â
android:id
=
"@+id/calendar"
       Â
android:layout_width
=
"match_parent"
       Â
android:layout_height
=
"match_parent"
/>
Â
Â</
androidx.constraintlayout.widget.ConstraintLayout
>
- Now add the following code in the MainActivity.java file. This will show the calendar 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
package
org.neveropen.gfgcalendarPickerView;
Â
Âimport
androidx.appcompat
   Â
.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.Toast;
import
com.squareup
   Â
.timessquare
   Â
.CalendarPickerView;
import
java.text.DateFormat;
import
java.util.Calendar;
import
java.util.Date;
Â
Âpublic
class
MainActivity
   Â
extends
AppCompatActivity {
Â
ÂÂ Â Â Â
@Override
   Â
protected
void
onCreate(
       Â
Bundle savedInstanceState)
   Â
{
       Â
super
.onCreate(savedInstanceState);
       Â
setContentView(R.layout.activity_main);
Â
ÂÂ Â Â Â Â Â Â Â
// This will return us today date
       Â
Date today =
new
Date();
Â
ÂÂ Â Â Â Â Â Â Â
Calendar nextYear
           Â
= Calendar.getInstance();
Â
ÂÂ Â Â Â Â Â Â Â
// This will help us
       Â
// to show date from
       Â
// today to next year
       Â
nextYear.add(Calendar.YEAR,
1
);
Â
ÂÂ Â Â Â Â Â Â Â
CalendarPickerView
           Â
datePicker
           Â
= findViewById(
               Â
R.id.calendar);
Â
ÂÂ Â Â Â Â Â Â Â
// we have to initialize
       Â
// our calendar 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(
               Â
new
CalendarPickerView
                   Â
.OnDateSelectedListener() {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
@Override
                       Â
public
void
onDateSelected(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();
                       Â
}
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
@Override
                       Â
public
void
onDateUnselected(
                           Â
Date date)
                       Â
{
                       Â
}
                   Â
});
   Â
}
}
Output: