0

I am using the Android CalendarView to create a simple Calendar app. I need to get the Month when the user presses the previous or next buttons in the calendar view. The CalendarView doesn't have any methods to get the month when the user clicks those buttons, so I tried using the following code to extract the Month.

ViewGroup vg = (ViewGroup) calendarView.getChildAt(0);
 View subView = vg.getChildAt(0);

but for some reason the ViewGroup holds a DayPickerViewPager widget, and 2 AppcompatImageButtons (the previous and next buttons), but no TextView or anything like that which holds the actual Month name.

The subView holds the DayPickerViewPager. Does anyone know how I can get the month on previous/next button clicks?

Thank you in advance

D. Gal
  • 329
  • 2
  • 14

2 Answers2

0

The CalendarView doesn't have any methods to get the month when the user clicks those buttons

I think it does; at least when you select a given date.

long getDate ()

Gets the selected date in milliseconds

You can process that into a Calendar object, which you can get the month from.

Refer: Java - Milliseconds to Month/Year (not just the current time)

I need to get the Month when the user presses the previous or next buttons in the calendar view

You need to use setOnDateChangeListener to capture the date that is finally selected.

It's not clear why you need an action event for the "next / previous" buttons when the user has not made a selection yet.

But, it you did, then you will need to setOnClickListener on both of those AppCompatImageButtons. However, once you do that yourself, you'll be overwriting the existing click listeners for them.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • The setOnDateChangeListener works only when selecting a date from the calendar. The getDate gets the selected date. I've added custom events to my calendar app. The events are stored in a database and I'd like to query them by Month. Meaning that if the user presses the next button, a text view will have it's text set to the number of events for that month and a recyclerview will be populated with the titles so that the user can simply scroll and click on the desired event. – D. Gal Mar 06 '17 at 21:55
  • Do you need a whole CalendarView just for month selection? You can create your own `MonthSelectionView` and define your own listeners on that. – OneCricketeer Mar 06 '17 at 21:58
  • The users can also click on the date and this opens a new intent. So say I'd like to set an event for the 7th of April. The user will go to April, then tap on the 7th and this will open up an intent with a form and user inputs the desired data, then saves it to the database. – D. Gal Mar 06 '17 at 22:01
  • I can't remember what a CalendarView looks like, but if the month is being displayed there, then there must be an internal `Calendar` object that gets passed through a `SimpleDateFormat` function to get that string. – OneCricketeer Mar 06 '17 at 22:08
  • Like, you see [`CalendarView` source](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/CalendarView.java), but I'm not finding any `Calendar` object there. – OneCricketeer Mar 06 '17 at 22:09
  • I've been trying for quite some time and can't seem to find a solution for it. Originally I wanted to highlight the dates that have events. So if I have events on the 7th, 13th, and 20th of this month I would highlight their background, say , blue. But apparently that cannot be done either so I wanted to create a custom way for me to show the user the event dates for a particular month. But I can't seem to extract the Month on next/previous click. – D. Gal Mar 06 '17 at 22:20
  • You could try a different Calendar. https://android-arsenal.com/search?q=calendar – OneCricketeer Mar 06 '17 at 22:27
0

Unfortunately, you cannot implement the month change event using Android's default CalenderView. However, an alternative customized MaterialCalendarView is available here that provides both the date and month change listeners.

The sample code to use the view is as follows:

MaterialCalendarView calendarView = new MaterialCalendarView(getActivity());
calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
      // Do something here
      Date selectedDate = date.getDate();
}
});

The date change listener can also be implemented in the similar manner using MaterialCalendarView.

R.Malik
  • 33
  • 6