srowley
srowley

Reputation: 837

Android DatePickerDialog Title Issue

I have implemented the DatePickerDialog, and it works. However, the title seems to not have a format. For example, it should read: Tuesday, April 12, 2011. But instead it reads 3, 2011 4 12.

The source code looks like it should be formatting the title, but it isn't:

private void updateTitle(int year, int month, int day) {
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
    mCalendar.set(Calendar.DAY_OF_MONTH, day);
    setTitle(mTitleDateFormat.format(mCalendar.getTime()));
}

Upvotes: 1

Views: 6242

Answers (1)

boiledwater
boiledwater

Reputation: 10482

As you say,DatePickerDialog title format depend "mTitleDateFormat" that initialize in DatePickerDialog constructor, like this:mTitleDateFormat = java.text.DateFormat. getDateInstance(java.text.DateFormat.FULL);

But mTitleDateFormat is private ,outer can not modify .So you can extends class DatePickerDialog .

The follow code work well in my work. Class code:

class DatePickerDialog extends android.app.DatePickerDialog {
    public DatePickerDialog(Context context, OnDateSetListener callBack,
            int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        updateTitle(year, monthOfYear, dayOfMonth);
    }
    public void onDateChanged(DatePicker view, int year,
            int month, int day) {
        updateTitle(year, month, day);
    }
    private void updateTitle(int year, int month, int day) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
        setTitle(getFormat().format(mCalendar.getTime()));
    }   
        /*
         * the format for dialog tile,and you can override this method
         */
    public SimpleDateFormat getFormat(){
        return new SimpleDateFormat("yyyy-MM-dd");
    };
}

Upvotes: 4

Related Questions