-
-
Notifications
You must be signed in to change notification settings - Fork 946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibility to select year only #237
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ public class DatePickerDialog extends DialogFragment implements | |
private static final String KEY_YEAR_START = "year_start"; | ||
private static final String KEY_YEAR_END = "year_end"; | ||
private static final String KEY_CURRENT_VIEW = "current_view"; | ||
private static final String KEY_SHOW_YEAR_ONLY = "show_year_only"; | ||
private static final String KEY_LIST_POSITION_OFFSET = "list_position_offset"; | ||
private static final String KEY_MIN_DATE = "min_date"; | ||
private static final String KEY_MAX_DATE = "max_date"; | ||
|
@@ -134,6 +135,7 @@ public class DatePickerDialog extends DialogFragment implements | |
private boolean mDismissOnPause = false; | ||
private boolean mAutoDismiss = false; | ||
private int mDefaultView = MONTH_AND_DAY_VIEW; | ||
private boolean mShowYearOnly = false; | ||
private int mOkResid = R.string.mdtp_ok; | ||
private String mOkString; | ||
private int mCancelResid = R.string.mdtp_cancel; | ||
|
@@ -223,6 +225,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) { | |
outState.putInt(KEY_YEAR_START, mMinYear); | ||
outState.putInt(KEY_YEAR_END, mMaxYear); | ||
outState.putInt(KEY_CURRENT_VIEW, mCurrentView); | ||
outState.putBoolean(KEY_SHOW_YEAR_ONLY, mShowYearOnly); | ||
int listPosition = -1; | ||
if (mCurrentView == MONTH_AND_DAY_VIEW) { | ||
listPosition = mDayPickerView.getMostVisiblePosition(); | ||
|
@@ -275,6 +278,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
mMinYear = savedInstanceState.getInt(KEY_YEAR_START); | ||
mMaxYear = savedInstanceState.getInt(KEY_YEAR_END); | ||
currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW); | ||
mShowYearOnly = savedInstanceState.getBoolean(KEY_SHOW_YEAR_ONLY); | ||
listPosition = savedInstanceState.getInt(KEY_LIST_POSITION); | ||
listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET); | ||
mMinDate = (Calendar)savedInstanceState.getSerializable(KEY_MIN_DATE); | ||
|
@@ -295,6 +299,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
mCancelString = savedInstanceState.getString(KEY_CANCEL_STRING); | ||
} | ||
|
||
if (mShowYearOnly) { | ||
((LinearLayout)mMonthAndDayView.getParent()).removeView(mMonthAndDayView); | ||
if(mDayOfWeekView != null) ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); | ||
} | ||
|
||
final Activity activity = getActivity(); | ||
mDayPickerView = new SimpleDayPickerView(activity, this); | ||
mYearPickerView = new YearPickerView(activity, this); | ||
|
@@ -576,6 +585,13 @@ public void showYearPickerFirst(boolean yearPicker) { | |
mDefaultView = yearPicker ? YEAR_VIEW : MONTH_AND_DAY_VIEW; | ||
} | ||
|
||
public void showYearOnly(boolean yearOnly) { | ||
mShowYearOnly = yearOnly; | ||
if (yearOnly) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will fail if someone later set showYearPickerFirst to false mShowYearFirst = yearOnly || showYearFirst; You'll also have to add some logic to the sample app that takes this paired logic into account. (would be great if android had some nice way to do reactive UIs, but there you go) |
||
showYearPickerFirst(true); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setFirstDayOfWeek(int startOfWeek) { | ||
if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) { | ||
|
@@ -794,9 +810,11 @@ public void onClick(View v) { | |
@Override | ||
public void onYearSelected(int year) { | ||
mCalendar.set(Calendar.YEAR, year); | ||
adjustDayInMonthIfNeeded(mCalendar); | ||
updatePickers(); | ||
setCurrentView(MONTH_AND_DAY_VIEW); | ||
if (!mShowYearOnly) { | ||
adjustDayInMonthIfNeeded(mCalendar); | ||
updatePickers(); | ||
setCurrentView(MONTH_AND_DAY_VIEW); | ||
} | ||
updateDisplay(true); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than removing these views, set their visibility to GONE.
This avoids potential
NullPointerExceptions
when other pieces of the code try to fetch them