From fc2548b2e73f5621d2befb440828664e8309c772 Mon Sep 17 00:00:00 2001 From: Arnaud de Mouhy Date: Tue, 13 Sep 2016 11:18:50 +0200 Subject: [PATCH 1/3] Ability to display only the year picker --- .../date/DatePickerDialog.java | 21 ++++++++++++++++--- .../datetimepickerexample/MainActivity.java | 3 +++ sample/src/main/res/layout/activity_main.xml | 7 +++++++ sample/src/main/res/values/strings.xml | 1 + 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java index 766266f7..5c6fdc07 100644 --- a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java +++ b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java @@ -134,6 +134,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; @@ -267,6 +268,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, mYearView = (TextView) view.findViewById(R.id.date_picker_year); mYearView.setOnClickListener(this); + if (mShowYearOnly) { + ((LinearLayout)mMonthAndDayView.getParent()).removeView(mMonthAndDayView); + ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); + } + int listPosition = -1; int listPositionOffset = 0; int currentView = mDefaultView; @@ -576,6 +582,13 @@ public void showYearPickerFirst(boolean yearPicker) { mDefaultView = yearPicker ? YEAR_VIEW : MONTH_AND_DAY_VIEW; } + public void showYearOnly(boolean yearOnly) { + mShowYearOnly = yearOnly; + if (yearOnly) { + showYearPickerFirst(true); + } + } + @SuppressWarnings("unused") public void setFirstDayOfWeek(int startOfWeek) { if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) { @@ -794,9 +807,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); } diff --git a/sample/src/main/java/com/wdullaer/datetimepickerexample/MainActivity.java b/sample/src/main/java/com/wdullaer/datetimepickerexample/MainActivity.java index 4f2f7359..92fbc7fd 100644 --- a/sample/src/main/java/com/wdullaer/datetimepickerexample/MainActivity.java +++ b/sample/src/main/java/com/wdullaer/datetimepickerexample/MainActivity.java @@ -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; @@ -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); @@ -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")); } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index ec622dd4..0d0c6c5d 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -132,6 +132,13 @@ android:text="@string/show_year_first" android:checked="false"/> + + Show a title Show a title Show the year picker first + Only show the year picker Show seconds picker Show minutes picker Limit selectable times From 62acbbb94113c40ea5442c0731399b63671a725f Mon Sep 17 00:00:00 2001 From: Arnaud de Mouhy Date: Tue, 13 Sep 2016 14:18:14 +0200 Subject: [PATCH 2/3] save year_only state on orientation change --- .../date/DatePickerDialog.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java index 5c6fdc07..3c2d4259 100644 --- a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java +++ b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java @@ -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"; @@ -224,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(); @@ -268,11 +270,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, mYearView = (TextView) view.findViewById(R.id.date_picker_year); mYearView.setOnClickListener(this); - if (mShowYearOnly) { - ((LinearLayout)mMonthAndDayView.getParent()).removeView(mMonthAndDayView); - ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); - } - int listPosition = -1; int listPositionOffset = 0; int currentView = mDefaultView; @@ -281,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); @@ -301,6 +299,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, mCancelString = savedInstanceState.getString(KEY_CANCEL_STRING); } + if (mShowYearOnly) { + ((LinearLayout)mMonthAndDayView.getParent()).removeView(mMonthAndDayView); + ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); + } + final Activity activity = getActivity(); mDayPickerView = new SimpleDayPickerView(activity, this); mYearPickerView = new YearPickerView(activity, this); From 2df0d80d2e99ff59ec1572910851d10773b43fc5 Mon Sep 17 00:00:00 2001 From: Arnaud de Mouhy Date: Sat, 15 Oct 2016 12:03:05 +0200 Subject: [PATCH 3/3] Fixed a crash when day of week view is not available --- .../wdullaer/materialdatetimepicker/date/DatePickerDialog.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java index 3c2d4259..e37b2ab8 100644 --- a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java +++ b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/DatePickerDialog.java @@ -301,7 +301,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, if (mShowYearOnly) { ((LinearLayout)mMonthAndDayView.getParent()).removeView(mMonthAndDayView); - ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); + if(mDayOfWeekView != null) ((LinearLayout)mDayOfWeekView.getParent()).removeView(mDayOfWeekView); } final Activity activity = getActivity();