Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set first day of week in DatePicker to monday

I have a DatePicker where I disabled the SpinnerView and now only use the CalendarView. But it starts with the sunday and I would like to use monday as the first day of the week. How can I change that? I guessed that the first day would depend on the phones settings but on my swiss phone its still wrong...

I haven't found any set Method or any XML command.

<DatePicker
   android:id="@+id/date_picker"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true" />

and

DatePicker datePicker = (DatePicker)   convertView.findViewById(R.id.date_picker);
datePicker.setSpinnersShown(false);

------------EDIT:------------

I tried to change the locale:

Locale.setDefault(Locale.GERMAN);

But that didn't change anything ether. So I read the android-17.android.widget.DatePicker.java where I found out:

public DatePicker(Context context, AttributeSet attrs, int defStyle) {
  //...  
  // initialization based on locale
        setCurrentLocale(Locale.getDefault());
  //...
}


 /**
 * Sets the current locale.
 *
 * @param locale The current locale.
 */
private void setCurrentLocale(Locale locale) {
    if (locale.equals(mCurrentLocale)) {
        return;
    }

    mCurrentLocale = locale;

    mTempDate = getCalendarForLocale(mTempDate, locale);
    mMinDate = getCalendarForLocale(mMinDate, locale);
    mMaxDate = getCalendarForLocale(mMaxDate, locale);
    mCurrentDate = getCalendarForLocale(mCurrentDate, locale);

    mNumberOfMonths = mTempDate.getActualMaximum(Calendar.MONTH) + 1;
    mShortMonths = new String[mNumberOfMonths];
    for (int i = 0; i < mNumberOfMonths; i++) {
        mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i,
                DateUtils.LENGTH_MEDIUM);
    }
}

 /**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale The locale.
 */
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}

So why does Locale.setDefault(Locale.GERMAN) not work?! As you can see above I get the DatePicker from the XML.. is there any mistake?!

like image 728
tyzess Avatar asked Nov 29 '12 17:11

tyzess


1 Answers

You have to set the first day of week manually, for example in onCreate()/onCreateView() of your activity/fragment:

date = (DatePicker) value.findViewById(R.id.date);
if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
    date.setSpinnersShown(false);
    date.setCalendarViewShown(true);
    date.getCalendarView().setFirstDayOfWeek(Calendar.getInstance().getFirstDayOfWeek());
}

The example above uses default locale (Calendar.getInstance() actually does), however you can specify the day manually:

date.getCalendarView().setFirstDayOfWeek(Calendar.MONDAY);
like image 59
Yan.Yurkin Avatar answered Nov 27 '22 06:11

Yan.Yurkin