org.bostonandroid.datepreference
Class DatePreference

java.lang.Object
  extended by android.preference.Preference
      extended by android.preference.DialogPreference
          extended by org.bostonandroid.datepreference.DatePreference
All Implemented Interfaces:
android.content.DialogInterface.OnClickListener, android.content.DialogInterface.OnDismissListener, android.preference.PreferenceManager.OnActivityDestroyListener, android.widget.DatePicker.OnDateChangedListener, java.lang.Comparable<android.preference.Preference>

public class DatePreference
extends android.preference.DialogPreference
implements android.widget.DatePicker.OnDateChangedListener


Nested Class Summary
 
Nested classes/interfaces inherited from class android.preference.Preference
android.preference.Preference.BaseSavedState, android.preference.Preference.OnPreferenceChangeListener, android.preference.Preference.OnPreferenceClickListener
 
Field Summary
 
Fields inherited from class android.preference.Preference
DEFAULT_ORDER
 
Constructor Summary
DatePreference(android.content.Context context, android.util.AttributeSet attrs)
           
DatePreference(android.content.Context context, android.util.AttributeSet attrs, int defStyle)
           
 
Method Summary
static java.util.Calendar defaultCalendar()
          The default date to use when the XML does not set it or the XML has an error.
static java.lang.String defaultCalendarString()
          The defaultCalendar() as a string using the formatter().
static java.text.SimpleDateFormat formatter()
          Produces the date formatter used for dates in the XML.
 java.util.Calendar getDate()
          Produces the date used for the date picker.
static java.util.Calendar getDateFor(android.content.SharedPreferences preferences, java.lang.String field)
          Produces the date the user has selected for the given preference, as a calendar.
 void onClick(android.content.DialogInterface dialog, int which)
          Called whenever the user clicks on a button.
protected  android.view.View onCreateDialogView()
          Produces a DatePicker set to the date produced by getDate().
 void onDateChanged(android.widget.DatePicker view, int year, int month, int day)
          Called when the user changes the date.
protected  void onDialogClosed(boolean shouldSave)
          Called when the dialog is closed.
protected  java.lang.Object onGetDefaultValue(android.content.res.TypedArray a, int index)
           
protected  void onRestoreInstanceState(android.os.Parcelable state)
          Called when Android restores the activity.
protected  android.os.Parcelable onSaveInstanceState()
          Called when Android pauses the activity.
protected  void onSetInitialValue(boolean restoreValue, java.lang.Object def)
          Called when the date picker is shown or restored.
 void setDate(java.lang.String dateString)
          Set the selected date to the specified string.
static java.text.SimpleDateFormat summaryFormatter()
          Produces the date formatter used for showing the date in the summary.
 
Methods inherited from class android.preference.DialogPreference
getDialog, getDialogIcon, getDialogLayoutResource, getDialogMessage, getDialogTitle, getNegativeButtonText, getPositiveButtonText, onActivityDestroy, onBindDialogView, onClick, onDismiss, onPrepareDialogBuilder, setDialogIcon, setDialogIcon, setDialogLayoutResource, setDialogMessage, setDialogMessage, setDialogTitle, setDialogTitle, setNegativeButtonText, setNegativeButtonText, setPositiveButtonText, setPositiveButtonText, showDialog
 
Methods inherited from class android.preference.Preference
callChangeListener, compareTo, findPreferenceInHierarchy, getContext, getDependency, getEditor, getIntent, getKey, getLayoutResource, getOnPreferenceChangeListener, getOnPreferenceClickListener, getOrder, getPersistedBoolean, getPersistedFloat, getPersistedInt, getPersistedLong, getPersistedString, getPreferenceManager, getSharedPreferences, getShouldDisableView, getSummary, getTitle, getView, getWidgetLayoutResource, hasKey, isEnabled, isPersistent, isSelectable, notifyChanged, notifyDependencyChange, notifyHierarchyChanged, onAttachedToActivity, onAttachedToHierarchy, onBindView, onCreateView, onDependencyChanged, onPrepareForRemoval, persistBoolean, persistFloat, persistInt, persistLong, persistString, restoreHierarchyState, saveHierarchyState, setDefaultValue, setDependency, setEnabled, setIntent, setKey, setLayoutResource, setOnPreferenceChangeListener, setOnPreferenceClickListener, setOrder, setPersistent, setSelectable, setShouldDisableView, setSummary, setSummary, setTitle, setTitle, setWidgetLayoutResource, shouldCommit, shouldDisableDependents, shouldPersist, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DatePreference

public DatePreference(android.content.Context context,
                      android.util.AttributeSet attrs,
                      int defStyle)

DatePreference

public DatePreference(android.content.Context context,
                      android.util.AttributeSet attrs)
Method Detail

onCreateDialogView

protected android.view.View onCreateDialogView()
Produces a DatePicker set to the date produced by getDate(). When overriding be sure to call the super.

Overrides:
onCreateDialogView in class android.preference.DialogPreference
Returns:
a DatePicker with the date set

getDate

public java.util.Calendar getDate()
Produces the date used for the date picker. If the user has not selected a date, produces the default from the XML's android:defaultValue. If the default is not set in the XML or if the XML's default is invalid it uses the value produced by defaultCalendar().

Returns:
the Calendar for the date picker

setDate

public void setDate(java.lang.String dateString)
Set the selected date to the specified string.

Parameters:
dateString - The date, represented as a string, in the format specified by formatter().

formatter

public static java.text.SimpleDateFormat formatter()
Produces the date formatter used for dates in the XML. The default is yyyy.MM.dd. Override this to change that.

Returns:
the SimpleDateFormat used for XML dates

summaryFormatter

public static java.text.SimpleDateFormat summaryFormatter()
Produces the date formatter used for showing the date in the summary. The default is MMMM dd, yyyy. Override this to change it.

Returns:
the SimpleDateFormat used for summary dates

onGetDefaultValue

protected java.lang.Object onGetDefaultValue(android.content.res.TypedArray a,
                                             int index)
Overrides:
onGetDefaultValue in class android.preference.Preference

onSetInitialValue

protected void onSetInitialValue(boolean restoreValue,
                                 java.lang.Object def)
Called when the date picker is shown or restored. If it's a restore it gets the persisted value, otherwise it persists the value.

Overrides:
onSetInitialValue in class android.preference.Preference

onSaveInstanceState

protected android.os.Parcelable onSaveInstanceState()
Called when Android pauses the activity.

Overrides:
onSaveInstanceState in class android.preference.DialogPreference

onRestoreInstanceState

protected void onRestoreInstanceState(android.os.Parcelable state)
Called when Android restores the activity.

Overrides:
onRestoreInstanceState in class android.preference.DialogPreference

onDateChanged

public void onDateChanged(android.widget.DatePicker view,
                          int year,
                          int month,
                          int day)
Called when the user changes the date.

Specified by:
onDateChanged in interface android.widget.DatePicker.OnDateChangedListener

onDialogClosed

protected void onDialogClosed(boolean shouldSave)
Called when the dialog is closed. If the close was by pressing "OK" it saves the value.

Overrides:
onDialogClosed in class android.preference.DialogPreference

defaultCalendar

public static java.util.Calendar defaultCalendar()
The default date to use when the XML does not set it or the XML has an error.

Returns:
the Calendar set to the default date

defaultCalendarString

public static java.lang.String defaultCalendarString()
The defaultCalendar() as a string using the formatter().

Returns:
a String representation of the default date

onClick

public void onClick(android.content.DialogInterface dialog,
                    int which)
Called whenever the user clicks on a button. Invokes onDateChanged(DatePicker, int, int, int) and onDialogClosed(boolean). Be sure to call the super when overriding.

Specified by:
onClick in interface android.content.DialogInterface.OnClickListener
Overrides:
onClick in class android.preference.DialogPreference

getDateFor

public static java.util.Calendar getDateFor(android.content.SharedPreferences preferences,
                                            java.lang.String field)
Produces the date the user has selected for the given preference, as a calendar.

Parameters:
preferences - the SharedPreferences to get the date from
field - the name of the preference to get the date from
Returns:
a Calendar that the user has selected