Android CalendarView showing wrong month on scroll when we set max date

1.4k views Asked by At

I have added setMinDate(Dec 1, 2015) and setMaxDate(current month last day) for calendarview, when moving from min date to max date, it is showing previous month(by default it is showing december, when i scroll to min date from max date, then min date to max date it is showing november finally). and how to remove unfocused month days from calendar, can any one suggest me

Here is my code

public void showCalendar() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ctx);
        LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.calendar_view_layout,
                null);

        CalendarView calendar = (CalendarView) dialogView
                .findViewById(R.id.calendarView);

        Button cancelBtn = (Button) dialogView.findViewById(R.id.cancel);
        Button okBtn = (Button) dialogView.findViewById(R.id.ok);

        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);

        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);
        calendar.setBackgroundColor(Color.WHITE);
        calendar.setFocusedMonthDateColor(Color.BLACK);




        /* calendar.setMinDate(); */
        calendar.setMaxDate(System.currentTimeMillis());

        // The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(Color.TRANSPARENT);

        // sets the color for the dates of an unfocused month.
        calendar.setUnfocusedMonthDateColor(Color.BLACK);

        // sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(
                R.color.transparent));

        // sets the color for the vertical bar shown at the beginning and at the
        // end of the selected date.
        // calendar.setSelectedDateVerticalBar(Color.GREEN);

        // sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            // show the selected date as a toast
            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int day) {

                currentMonth = month;
                currentDay = day;
                currentYear = year;

                /*
                 * Toast.makeText(getActivity(), day + "/" + (month + 1) + "/" +
                 * year, Toast.LENGTH_LONG) .show();
                 */
            }
        });

        getCurrentMonth(calendar);

        dialogBuilder.setView(dialogView);
        final AlertDialog b = dialogBuilder.create();
        b.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        b.show();

    }

    void getCurrentMonth(CalendarView calendarView) {

        Date today = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);

        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        Date lastDayOfMonth = calendar.getTime();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        long maxDate = lastDayOfMonth.getTime();
        calendarView.setMaxDate(maxDate);
        String[] monthLastDay = sdf.format(lastDayOfMonth).split("-");

        int year = Integer.parseInt(monthLastDay[0]) - 1;

        String lastYearFirstDate = year + "-" + monthLastDay[1] + "-" + 01;

        try {
            long minDate = sdf.parse(lastYearFirstDate).getTime();
            calendarView.setMinDate(minDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
1

There are 1 answers

1
Luca Nicoletti On

Have a look here. Months starts from 0, so, 0 = Jan, 11 = Dec. Second question is unclear, explain it better please, some examples would be fine