prolificinteractive / material-calendarview

A Material design back port of Android's CalendarView
https://prolificinteractive.github.io/material-calendarview/
MIT License
5.92k stars 1.32k forks source link

When currentDate is set to a Monday, weekView shows previous week by default #729

Closed KaHa6uc closed 6 years ago

KaHa6uc commented 6 years ago

I'm layout-ing your class like this:

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/cvCalendar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
      app:mcv_showOtherDates="all"
      app:mcv_calendarMode="week"
      app:mcv_firstDayOfWeek="monday"
      app:mcv_tileHeight="26dp"
      app:mcv_selectionColor="@color/ColorPrimary"
        />

And then stylise like this:

        calendarView = mView.findViewById( R.id.cvCalendar );
        Calendar c = Calendar.getInstance();
//      c.set( Calendar.DAY_OF_MONTH, c.get( Calendar.DAY_OF_MONTH )-1 );
        calendarView.setCurrentDate( c );
        calendarView.setSelectedDate( c );
        calendarView.setLeftArrowMask( null );
        calendarView.setRightArrowMask( null );
        calendarView.setTopbarVisible( false );
        c.add( Calendar.DAY_OF_MONTH, 1 );
        calendarView.state().edit().setMaximumDate( c )
            .setFirstDayOfWeek( Calendar.MONDAY ).commit();

When I .setCurrentDate() to a Monday, the initial display shows the previous week - the week that contains the Sunday with which the week that contains currentDate starts, even though I've told it that the week should start on Monday.

Uncomment the commented line to subtract the number of days needed to set it to a Monday to reproduce the issue.

Please advise.

Thanks in advance.

huygee commented 6 years ago

Probably related, but the same issue occurs with the title view for the month. From what I can tell, currentMonth is initialized to today at MaterialCalendarView.java#L388 and then it's only updated in response to when the user scrolls via the pager. This is particularly problematic when the min/max date are such that scrolling is not possible.

It seems like the DayViews do represent the days from the current week, which makes it even more confusing. Rather than fork the repo, I've been working around this by using reflection to update MaterialCalendarView.currentMonth prior to calling setCurrentDate().

bharamalhusen commented 6 years ago

When set first day of week is set to a Monday, weekView shows previous week by default I am also facing with the same issue. but its only with the oreo i test with jeally bean to naought versions its working fine.

calendarView.state().edit()
                .setFirstDayOfWeek(Calendar.MONDAY)
                .setCalendarDisplayMode(CalendarMode.WEEKS).commit();
dhruvmi commented 6 years ago

Any fix or workaround for this issue? I am facing this in week mode without setting setFirstDayOfWeek() to MONDAY, today is 27th Feb 2018 and it is showing the last week.

I did set minimum and maximum date and it worked for me, here is my snippet

        MaterialCalendarView calendarView = view.findViewById(R.id.calendarView);

        calendarView.setOnDateChangedListener(this);
        calendarView.setPagingEnabled(false);
        calendarView.setTopbarVisible(false);
        Calendar currentCal = Calendar.getInstance();
        currentCal.clear(Calendar.HOUR_OF_DAY);
        currentCal.clear(Calendar.MINUTE);
        currentCal.clear(Calendar.SECOND);
        currentCal.clear(Calendar.MILLISECOND);
        calendarView.setSelectedDate(currentCal.getTime());

        Calendar startCal = (Calendar) currentCal.clone();
        startCal.set(Calendar.DAY_OF_WEEK, startCal.getFirstDayOfWeek());

        Calendar endCal = (Calendar) startCal.clone();
        endCal.add(Calendar.WEEK_OF_YEAR, 1);

        calendarView.state().edit()
                .setMinimumDate(
                        CalendarDay.from(
                                startCal.get(Calendar.YEAR), 
                                startCal.get(Calendar.MONTH), 
                                startCal.get(Calendar.DAY_OF_MONTH)
                        )
                )
                .setMaximumDate(
                        CalendarDay.from(
                                endCal.get(Calendar.YEAR), 
                                endCal.get(Calendar.MONTH), 
                                endCal.get(Calendar.DAY_OF_MONTH)
                        )
                )
                .setCalendarDisplayMode(CalendarMode.WEEKS)
                .commit();

        calendarView.addDecorators(
                new TodayDecorator()
        );
JyoHuang commented 6 years ago

thank you @dhruvmi and I can work fine by reseting the minDate and MaxDate after as you mentioned. note: remember to set .setFirstDayOfWeek(Calendar.SUNDAY) because the first day of a week is Sunday.

MaterialCalendarView calendarView = view.findViewById(R.id.calendarView);

    calendarView.setOnDateChangedListener(this);

    Calendar currentCal = Calendar.getInstance();
    currentCal.clear(Calendar.HOUR_OF_DAY);
    currentCal.clear(Calendar.MINUTE);
    currentCal.clear(Calendar.SECOND);
    currentCal.clear(Calendar.MILLISECOND);
    calendarView.setSelectedDate(currentCal.getTime());

    Calendar startCal = (Calendar) currentCal.clone();
    startCal.set(Calendar.DAY_OF_WEEK, startCal.getFirstDayOfWeek());

    Calendar endCal = (Calendar) startCal.clone();
    endCal.add(Calendar.WEEK_OF_YEAR, 1);

    calendarView.state().edit()
            .setMinimumDate(
                    CalendarDay.from(
                            startCal.get(Calendar.YEAR), 
                            startCal.get(Calendar.MONTH), 
                            startCal.get(Calendar.DAY_OF_MONTH)
                    )
            )
            .setMaximumDate(
                    CalendarDay.from(
                            endCal.get(Calendar.YEAR), 
                            endCal.get(Calendar.MONTH), 
                            endCal.get(Calendar.DAY_OF_MONTH)
                    )
            )
            .setCalendarDisplayMode(CalendarMode.WEEKS)
            .commit();

    calendarView.addDecorators(
            new TodayDecorator()
    );

    calendarView.state().edit()
            .setMinimumDate(
                    CalendarDay.from(
                            startCal.get(2000), 
                            startCal.get(1), 
                            startCal.get(1)
                    )
            )
            .setMaximumDate(
                    CalendarDay.from(
                            endCal.get(2050), 
                            endCal.get(12), 
                            endCal.get(30)
                    )
            )
            .setCalendarDisplayMode(CalendarMode.WEEKS)
            .commit();
quentin41500 commented 6 years ago

I believe that should be fixed in with #809

Arunsmail94 commented 6 years ago

i disabled topbar visible using setTopbarVisible(false). but need a month and year when scrolling the calendar in past or previous. how to get the date or i need to remove the mcv_arrows.