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
<?xmlversion="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
packageorg.neveropen.gfgcalendarPickerView;ÂÂimportandroidx.appcompat   Â.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;importcom.squareup   Â.timessquare   Â.CalendarPickerView;importjava.text.DateFormat;importjava.util.Calendar;importjava.util.Date;ÂÂpublicclassMainActivity   ÂextendsAppCompatActivity {   Â@Override   ÂprotectedvoidonCreate(       ÂBundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Â// This will return us today date       ÂDate today =newDate();       Â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(               ÂnewCalendarPickerView                   Â.OnDateSelectedListener() {                       Â@Override                       ÂpublicvoidonDateSelected(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                       ÂpublicvoidonDateUnselected(                           ÂDate date)                       Â{                       Â}                   Â});   Â}}
Output:

