Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Calendar - Start week with monday

I made a calendar like it is shown on the screenshot.. but I need to do that week start with monday instead of sunday... i tied to do Calendar cal = Calendar.getInstanty(); cal.setFirstDayOfWeek(Calendar.MONDAY); but this didn't help.. any ideas?

Thanks you

enter image description here

    import java.util.Calendar;
import java.util.Locale;

    import android.content.Context;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Spinner;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    import android.text.format.DateFormat;
import android.text.format.DateUtils;


    public class CalendarView extends LinearLayout {
        public CalendarView(Context context) {
            super(context);
            init(context);
        }



        public CalendarView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public interface OnMonthChangedListener {
            public void onMonthChanged(CalendarView view);
        }

        public void setOnMonthChangedListener(OnMonthChangedListener l) {
            _onMonthChangedListener = l;
        }

        public interface OnSelectedDayChangedListener {
            public void onSelectedDayChanged(CalendarView view);
        }

        public void setOnSelectedDayChangedListener(OnSelectedDayChangedListener l) {
            _onSelectedDayChangedListener = l;
        }

        public Calendar getVisibleStartDate() {
            return _calendar.getVisibleStartDate();
        }

        public Calendar getVisibleEndDate() {
            return _calendar.getVisibleEndDate();
        }

        public Calendar getSelectedDay() {
            return _calendar.getSelectedDay();
        }


        private void init(Context context) {
            View v = LayoutInflater.from(context).inflate(R.layout.calendar, this, true);

            _calendar = new CalendarWrapper();
            _days = (TableLayout) v.findViewById(R.id.days);
            _up = (TextView) v.findViewById(R.id.up);
            _prev = (Button) v.findViewById(R.id.previous);
            _next = (Button) v.findViewById(R.id.next);


            refreshCurrentDate();




            // Days Table
            String[] shortWeekDayNames = _calendar.getShortDayNames();
            for (int i = 0; i < 7; i++) { // Rows
                TableRow tr = (TableRow) _days.getChildAt(i);

                for (int j = 0; j < 7; j++) { // Columns
                    Boolean header = i == 0; // First row is weekday headers
                    TextView tv = (TextView) tr.getChildAt(j);

                    if (header)
                        tv.setText(shortWeekDayNames[j]);
                    else
                        tv.setOnClickListener(_dayClicked);
                }
            }

            refreshDayCells();

            // Listeners
            _calendar.setOnDateChangedListener(_dateChanged);
            _prev.setOnClickListener(_incrementClicked);
            _next.setOnClickListener(_incrementClicked);

            setView(MONTH_VIEW);
        }
        private OnClickListener _incrementClicked = new OnClickListener() {
            public void onClick(View v) {
                int inc = (v == _next ? 1 : -1);

                if (_currentView == MONTH_VIEW)
                    _calendar.addMonth(inc);
                else if (_currentView == DAY_VIEW) {
                    _calendar.addDay(inc);
                    invokeSelectedDayChangedListener();
                }
                else if (_currentView == YEAR_VIEW) {
                    _currentYear += inc;
                    refreshUpText();
                }
            }
        };
        private OnDateChangedListener _dateChanged = new OnDateChangedListener() {
            public void onDateChanged(CalendarWrapper sc) {
                Boolean monthChanged = _currentYear != sc.getYear() || _currentMonth != sc.getMonth();

                if (monthChanged) {
                    refreshDayCells();
                    invokeMonthChangedListener();
                }

                refreshCurrentDate();
                refreshUpText();
            }
        };



        private OnClickListener _dayClicked = new OnClickListener() {
            public void onClick(View v) {

            }
        };




        private void refreshDayCells() {
            int[] dayGrid = _calendar.get7x6DayArray();
            int monthAdd = -1;
            int row = 1; // Skip weekday header row
            int col = 0;

            for (int i = 0; i < dayGrid.length; i++) {
                int day = dayGrid[i];

                if (day == 1)
                    monthAdd++;

                TableRow tr = (TableRow) _days.getChildAt(row);
                TextView tv = (TextView) tr.getChildAt(col);

                //Clear current markers, if any.
                tv.setBackgroundDrawable(null);
                tv.setWidth(0);
                tv.setTextSize(12);
                tv.setText(dayGrid[i] + " 100€");
                tv.setBackgroundColor(Color.WHITE);


                if (monthAdd == 0)
                    tv.setTextColor(Color.BLACK);
                else
                    tv.setTextColor(Color.LTGRAY);


                tv.setTag(new int[] { monthAdd, dayGrid[i] });

                col++;

                if (col == 7) {
                    col = 0;
                    row++;
                }
            }
        }

        private void setView(int view) {
            if (_currentView != view) {
                _currentView = view;
                _days.setVisibility(_currentView == MONTH_VIEW ? View.VISIBLE : View.GONE);
                refreshUpText();
            }
        }

        private void refreshUpText() {
            switch (_currentView) {
                case MONTH_VIEW:
                    _up.setText(_calendar.toString("MMMM yyyy"));
                    break;
                case YEAR_VIEW:
                    _up.setText(_currentYear + "");
                    break;
                case CENTURY_VIEW:
                    _up.setText("CENTURY_VIEW");
                    break;
                case DECADE_VIEW:
                    _up.setText("DECADE_VIEW");
                    break;
                case DAY_VIEW:
                    _up.setText(_calendar.toString("EEEE, MMMM dd, yyyy"));
                    break;
                case ITEM_VIEW:
                    _up.setText("ITEM_VIEW");
                    break;
                default:
                    break;
            }
        }

        private void refreshCurrentDate() {
            _currentYear = _calendar.getYear();
            _currentMonth = _calendar.getMonth();
            _calendar.getDay();


            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            if(month == _calendar.getMonth() && year== _calendar.getYear()){_prev.setVisibility(INVISIBLE);}
            else {_prev.setVisibility(VISIBLE);}
        }

        private void invokeMonthChangedListener() {
            if (_onMonthChangedListener != null)
                _onMonthChangedListener.onMonthChanged(this);
        }

        private void invokeSelectedDayChangedListener() {
            if (_onSelectedDayChangedListener != null)
                _onSelectedDayChangedListener.onSelectedDayChanged(this);
        }

        private final int CENTURY_VIEW = 5;
        private final int DECADE_VIEW = 4;
        private final int YEAR_VIEW = 3;
        private final int MONTH_VIEW = 2;
        private final int DAY_VIEW = 1;
        private final int ITEM_VIEW = 0;

        private CalendarWrapper _calendar;
        private TableLayout _days;
        private TextView _up;
        private Button _prev;
        private Button _next;
//      private Spinner sailFromSpinner;
        private OnMonthChangedListener _onMonthChangedListener;
        private OnSelectedDayChangedListener _onSelectedDayChangedListener;
        private int _currentView;
        private int _currentYear;
        private int _currentMonth;
        public Calendar cal = Calendar.getInstance();
    }

    class CalendarWrapper {
        public interface OnDateChangedListener {
            public void onDateChanged(CalendarWrapper sc);
        }

        public CalendarWrapper() {
            _calendar = Calendar.getInstance();



            _shortDayNames = new String[_calendar.getActualMaximum(Calendar.DAY_OF_WEEK)];
            _shortMonthNames = new String[_calendar.getActualMaximum(Calendar.MONTH) + 1]; // Months are 0-based so size is Max + 1

            for (int i = 0; i < _shortDayNames.length; i++) {
                _shortDayNames[i] = DateUtils.getDayOfWeekString(i +1 , DateUtils.LENGTH_SHORT);
            }

            for (int i = 0; i < _shortMonthNames.length; i++) {
                _shortMonthNames[i] = DateUtils.getMonthString(i, DateUtils.LENGTH_SHORT);
            }
        }


        public int getYear() {
            return _calendar.get(Calendar.YEAR);
        }

        public int getMonth() {
            return _calendar.get(Calendar.MONTH);
        }

        public int getDayOfWeek() {
            return _calendar.get(Calendar.DAY_OF_WEEK);
        }

        public int getDay() {
            return _calendar.get(Calendar.DAY_OF_MONTH);
        }


        public void setYear(int value) {
            _calendar.set(Calendar.YEAR, value);
            invokeDateChangedListener();
        }

        public void setYearAndMonth(int year, int month) {
            _calendar.set(Calendar.YEAR, year);
            _calendar.set(Calendar.MONTH, month);
            invokeDateChangedListener();
        }

        public void setMonth(int value) {
            _calendar.set(Calendar.MONTH, value);
            invokeDateChangedListener();
        }

        public void setDay(int value) {
            _calendar.set(Calendar.DAY_OF_MONTH, value);
            invokeDateChangedListener();
        }

        public void addYear(int value) {
            if(value != 0) {
                _calendar.add(Calendar.YEAR, value);
                invokeDateChangedListener();
            }
        }

        public void addMonth(int value) {
            if(value != 0) {
                _calendar.add(Calendar.MONTH, value);
                invokeDateChangedListener();
            }
        }

        public void addMonthSetDay(int monthAdd, int day) {
            _calendar.add(Calendar.MONTH, monthAdd);
            _calendar.set(Calendar.DAY_OF_MONTH, day);

            invokeDateChangedListener();
        }

        public void addDay(int value) {
            if(value != 0) {
                _calendar.add(Calendar.DAY_OF_MONTH, value);
                invokeDateChangedListener();
            }
        }



        public String[] getShortDayNames() {
                return _shortDayNames;
        }

        public String[] getShortMonthNames() {
            return _shortMonthNames;
        }

        public int[] get7x6DayArray() {
            _visibleStartDate = null;
            _visibleEndDate = null;

            int[] days = new int[42];

            Calendar tempCal = (Calendar) _calendar.clone();
            tempCal.setFirstDayOfWeek(2);
            tempCal.set(Calendar.DAY_OF_MONTH, 1);

            int dayOfWeekOn1st = tempCal.get(Calendar.DAY_OF_WEEK);
            int maxDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int previousMonthCount = dayOfWeekOn1st - 1;
            int index = 0;

            if (previousMonthCount > 0) {
                tempCal.set(Calendar.DAY_OF_MONTH, -1);

                int previousMonthMax = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);

                for (int i = previousMonthCount; i > 0; i--) {
                    int day = previousMonthMax - i + 1; 

                    if(i == previousMonthCount) {
                        _visibleStartDate = (Calendar)tempCal.clone();
//                      _visibleStartDate.setFirstDayOfWeek(2);
                        _visibleStartDate.set(Calendar.DAY_OF_MONTH, day);
                    }

                    days[index] = day;
                    index++;
                }
            }

            for (int i = 0; i < maxDay; i++) {
                if(i == 0 && _visibleStartDate == null)
                    _visibleStartDate = (Calendar)tempCal.clone();

                days[index] = (i + 1);
                index++;
            }

            int nextMonthDay = 1;

            for (int i = index; i < days.length; i++) {
                if(i == index)


                days[index] = nextMonthDay;
                nextMonthDay++;
                index++;
            }

            _visibleEndDate = (Calendar) _calendar.clone();
            _visibleEndDate.add(Calendar.MONTH, 1);
            _visibleEndDate.set(Calendar.DAY_OF_MONTH, days[41]);

            return days;
        }

        public Calendar getSelectedDay() {
            return (Calendar)_calendar.clone();
        }

        public Calendar getVisibleStartDate() {
            return (Calendar) _visibleStartDate.clone();
        }

        public Calendar getVisibleEndDate() {
            return (Calendar) _visibleEndDate.clone();
        }

        public void setOnDateChangedListener(OnDateChangedListener l) {
            _onDateChangedListener = l;
        }

        public String toString(CharSequence format) {
            return DateFormat.format(format, _calendar).toString();
        }

        private void invokeDateChangedListener() {
            if (_onDateChangedListener != null)
                _onDateChangedListener.onDateChanged(this);
        }

        private Calendar _calendar;
        private String[] _shortDayNames;
        private String[] _shortMonthNames;
        private OnDateChangedListener _onDateChangedListener;
        private Calendar _visibleStartDate;
        private Calendar _visibleEndDate;
    }
like image 417
Sergio Avatar asked Nov 13 '22 21:11

Sergio


1 Answers

Don't know if I'm right, don't have android / java to test right now.

By seems that you do some math on _calendar, then clone it inside the get7x6DayArray, set to begin on Monday in that clone, them return the days[] to be show. But you never set the _calendar to begin on Monday, so you get the "shortWeekDayNames" begining with the Sunday (that's problably what your locale is set).

EDIT: taking a closer look to your question: in your CalendarWrapper you create an Array to hold the name of the days of the week: _shortDayNames.

Then you do a loop to fill the array with the short names: you loop from 0-6, getting the 1-7 days' names.

If you look closer at this and this, you'll see that Sunday is always the value 1: calendar.SUNDAY = 1, so _shortDayNames[0] will always get the String that corresponds to the "1" value, which is always Sunday. So your calendar will always be shown to begin with "Sunday" when you display it.

Isn't that the problem ?

like image 140
woliveirajr Avatar answered Dec 14 '22 18:12

woliveirajr