1

I'm trying to access user-defined listeners in this Android library with Xamarin bindings (original here) for when the calendar is scrolled to another month or a date is selected on the calendar.

The listeners in the code given in the sample are as follows:

        compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
        @Override
        public void onDayClick(Date dateClicked) {
            List<Event> events = compactCalendarView.getEvents(dateClicked);
            Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
        }

        @Override
        public void onMonthScroll(Date firstDayOfNewMonth) {
            Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
        }
    });

Specifically what I'd like to access is the onDayClick listener. The code for it in its Controller class in Java doesn't specify a button but rather calculates the position in the calendar of the date you clicked and then returns a date based on that calculation.

    void onSingleTapUp(MotionEvent e) {

    // Don't handle single tap when calendar is scrolling and is not stationary

    if (isScrolling()) {

        return;

    }



    int dayColumn = Math.round((paddingLeft + e.getX() - paddingWidth - paddingRight) / widthPerDay);

    int dayRow = Math.round((e.getY() - paddingHeight) / heightPerDay);



    setCalenderToFirstDayOfMonth(calendarWithFirstDayOfMonth, currentDate, monthsScrolledSoFar(), 0);



    int firstDayOfMonth = getDayOfWeek(calendarWithFirstDayOfMonth);



    int dayOfMonth = ((dayRow - 1) * 7) - firstDayOfMonth;

    if (isRtl) {

        dayOfMonth +=  6 - dayColumn;

    } else {

        dayOfMonth += dayColumn;

    }

    if (dayOfMonth < calendarWithFirstDayOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH)

            && dayOfMonth >= 0) {

        calendarWithFirstDayOfMonth.add(Calendar.DATE, dayOfMonth);



        currentCalender.setTimeInMillis(calendarWithFirstDayOfMonth.getTimeInMillis());

        performOnDayClickCallback(currentCalender.getTime());

    }

}

Attempting to declare a listener as CompactCalendarView.ICompactCalendarViewListener listener; seems to declare it fine, but attempting to assign it a new CompactCalendarViewListener() gives me an "Undefined function" error.

I understand C# uses events instead of listeners, but I don't know how it handles user-defined listeners in Java libraries, or how to override events such as onDayClick/onMonthScroll.

Any help is greatly appreciated!

W. Hoffman
  • 27
  • 7
  • I read your question thrice and still was not able to understand about what exactly is not working here!? – FreakyAli Feb 28 '19 at 06:57
  • I cannot use the SetListener function provided in the Java library that has Xamarin bindings. I make a Listener object using the ICompactCalendarViewListener interface but I cannot make an instance of it since it's an interface and therefore cannot assign anything to the listener. I cannot use the library's constructor for a listener, either. I wrote to ask how to access the functions the listener provides, those being the onDayClick and onMonthScroll methods, i.e. if they were bound to an event or something Xamarin does in the binding that I am unaware of. Sorry if it's a bit unclear. – W. Hoffman Feb 28 '19 at 13:19
  • Actually, looking again, I think I have to write a class that implements the interface myself...I'll answer this myself if that's the solution. – W. Hoffman Feb 28 '19 at 13:27
  • I have added a solution take a look – FreakyAli Feb 28 '19 at 14:52

1 Answers1

2

Your classes in Xamarin.Android using C# cannot be anonymous as C# does not support anonymous declarations what you need to do is something like this:

compactCalendarView.SetListener(new CompactCalendarViewListener());

And then define a class that inherits from Java.Lang.Object for Android's Dispose method and the interface that you want to use something like below:

 public class CompactCalendarViewListener : Java.Lang.Object, CompactCalendarView.ICompactCalendarViewListener
{
    public void OnDayClick(Date p0)
    {
        // throw new System.NotImplementedException();
    }

    public void OnMonthScroll(Date p0)
    {
        //throw new System.NotImplementedException();
    }
}
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • Thank you! I was working on this myself but had ran into some issues with declarations but this helps a lot. – W. Hoffman Feb 28 '19 at 15:06
  • Sure no problem feel free to revert in case of any issues in the future – FreakyAli Feb 28 '19 at 15:07
  • @FreakyAli Can you please help me ? Im having a problem about listerns and events too ? https://stackoverflow.com/questions/62365511/how-to-adapt-java-eventlisteners-to-c%e2%a7%a3 – Shahid Od Jun 14 '20 at 00:00