title: Android Calendar - Start week with monday
body: <p>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 <code>Calendar cal = Calendar.getInstanty(); cal.setFirstDayOfWeek(Calendar.MONDAY);</code>
  but this didn't help.. any ideas? </p>
  
  <p>Thanks you</p>
  
  <p><img src="https://i.stack.imgur.com/eHhrJ.png" alt="enter image description here"></p>
  
  <pre><code>    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 &lt; 7; i++) { // Rows
                  TableRow tr = (TableRow) _days.getChildAt(i);
  
                  for (int j = 0; j &lt; 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 &lt; 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() &amp;&amp; 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 &lt; _shortDayNames.length; i++) {
                  _shortDayNames[i] = DateUtils.getDayOfWeekString(i +1 , DateUtils.LENGTH_SHORT);
              }
  
              for (int i = 0; i &lt; _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 &gt; 0) {
                  tempCal.set(Calendar.DAY_OF_MONTH, -1);
  
                  int previousMonthMax = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);
  
                  for (int i = previousMonthCount; i &gt; 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 &lt; maxDay; i++) {
                  if(i == 0 &amp;&amp; _visibleStartDate == null)
                      _visibleStartDate = (Calendar)tempCal.clone();
  
                  days[index] = (i + 1);
                  index++;
              }
  
              int nextMonthDay = 1;
  
              for (int i = index; i &lt; 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;
      }
  </code></pre>