Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -295,6 +299,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mCancelString = savedInstanceState.getString(KEY_CANCEL_STRING);
}

if (mShowYearOnly) {
Copy link
Owner

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

((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);
Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if someone later set showYearPickerFirst to false
You'll have to change the setter for showYearPickerFirst too

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) {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MainActivity extends AppCompatActivity implements
private CheckBox titleTime;
private CheckBox titleDate;
private CheckBox showYearFirst;
private CheckBox showYearOnly;
private CheckBox enableSeconds;
private CheckBox enableMinutes;
private CheckBox limitTimes;
Expand Down Expand Up @@ -65,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {
titleTime = (CheckBox) findViewById(R.id.title_time);
titleDate = (CheckBox) findViewById(R.id.title_date);
showYearFirst = (CheckBox) findViewById(R.id.show_year_first);
showYearOnly = (CheckBox) findViewById(R.id.show_year_only);
enableSeconds = (CheckBox) findViewById(R.id.enable_seconds);
enableMinutes = (CheckBox) findViewById(R.id.enable_minutes);
limitTimes = (CheckBox) findViewById(R.id.limit_times);
Expand Down Expand Up @@ -130,6 +132,7 @@ public void onClick(View v) {
dpd.vibrate(vibrateDate.isChecked());
dpd.dismissOnPause(dismissDate.isChecked());
dpd.showYearPickerFirst(showYearFirst.isChecked());
dpd.showYearOnly(showYearOnly.isChecked());
if (modeCustomAccentDate.isChecked()) {
dpd.setAccentColor(Color.parseColor("#9C27B0"));
}
Expand Down
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@
android:text="@string/show_year_first"
android:checked="false"/>

<CheckBox
android:id="@+id/show_year_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_year_only"
android:checked="false" />

<CheckBox
android:id="@+id/title_date"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="title_time">Show a title</string>
<string name="title_date">Show a title</string>
<string name="show_year_first">Show the year picker first</string>
<string name="show_year_only">Only show the year picker</string>
<string name="enable_seconds">Show seconds picker</string>
<string name="enable_minutes">Show minutes picker</string>
<string name="limit_times">Limit selectable times</string>
Expand Down