From 01064529c7afc812b2d7c9bfa0c848883caf1a0c Mon Sep 17 00:00:00 2001 From: aseik Date: Fri, 6 May 2016 18:01:23 +0200 Subject: [PATCH] Added HMSPicker hour minute only option --- .../betterpickers/hmspicker/HmsPicker.java | 69 +++++++++++++++---- .../hmspicker/HmsPickerBuilder.java | 8 ++- .../hmspicker/HmsPickerDialogFragment.java | 11 ++- .../betterpickers/hmspicker/HmsView.java | 60 ++++++++++++++-- .../src/main/res/layout/hms_picker_view.xml | 8 +++ sample/src/main/AndroidManifest.xml | 8 +++ .../SampleHmsHourMinutesOnlyUsage.java | 46 +++++++++++++ 7 files changed, 188 insertions(+), 22 deletions(-) create mode 100644 sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsHourMinutesOnlyUsage.java diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java index ad2a3dcf..b9118f0b 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java @@ -39,6 +39,7 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B private int mDividerColor; private int mDeleteDrawableSrcResId; private int mTheme = -1; + private boolean mHourMinutesOnly; /** * Instantiates an HmsPicker object @@ -114,7 +115,7 @@ private void restyleViews() { mMinutesLabel.setTextColor(mTextColor); mMinutesLabel.setBackgroundResource(mKeyBackgroundResId); } - if (mSecondsLabel != null) { + if (mSecondsLabel != null && !mHourMinutesOnly) { mSecondsLabel.setTextColor(mTextColor); mSecondsLabel.setBackgroundResource(mKeyBackgroundResId); } @@ -124,6 +125,11 @@ private void restyleViews() { } if (mEnteredHms != null) { mEnteredHms.setTheme(mTheme); + + if(mHourMinutesOnly) { + mEnteredHms.setHourMinutesOnly(true); + mEnteredHms.rearrangeView(); + } } } @@ -249,7 +255,11 @@ private void updateKeypad() { * Hide digit by passing -2 (for highest hours digit only); */ protected void updateHms() { - mEnteredHms.setTime(mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); + if(mHourMinutesOnly) { + mEnteredHms.setTime(mInput[3], mInput[2], mInput[1], mInput[0], 0, 0); + } else { + mEnteredHms.setTime(0, mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); + } } private void addClickedNumber(int val) { @@ -295,8 +305,11 @@ public void setSetButton(Button b) { * @return the inputted hours */ public int getHours() { - int hours = mInput[4]; - return hours; + if(mHourMinutesOnly){ + return mInput[3] * 10 + mInput[2]; + } else { + return mInput[4]; + } } /** @@ -305,7 +318,11 @@ public int getHours() { * @return the inputted minutes */ public int getMinutes() { - return mInput[3] * 10 + mInput[2]; + if(mHourMinutesOnly){ + return mInput[1] * 10 + mInput[0]; + } else { + return mInput[3] * 10 + mInput[2]; + } } /** @@ -314,7 +331,11 @@ public int getMinutes() { * @return the inputted seconds */ public int getSeconds() { - return mInput[1] * 10 + mInput[0]; + if(mHourMinutesOnly){ + return 0; + } else { + return mInput[1] * 10 + mInput[0]; + } } /** @@ -325,13 +346,22 @@ public int getSeconds() { * @param seconds the input seconds value */ public void setTime(int hours, int minutes, int seconds) { - mInput[4] = hours; - mInput[3] = minutes / 10; - mInput[2] = minutes % 10; - mInput[1] = seconds / 10; - mInput[0] = seconds % 10; - for (int i=4; i>=0; i--) { + if(mHourMinutesOnly) { + mInput[3] = hours / 10; + mInput[2] = hours % 10; + mInput[1] = minutes / 10; + mInput[0] = minutes % 10; + + } else { + mInput[4] = hours; + mInput[3] = minutes / 10; + mInput[2] = minutes % 10; + mInput[1] = seconds / 10; + mInput[0] = seconds % 10; + } + + for (int i=mInputSize - 1; i>=0; i--) { if (mInput[i] > 0) { mInputPointer = i; break; @@ -413,7 +443,11 @@ public SavedState[] newArray(int size) { * @return an int representing the time in seconds */ public int getTime() { - return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0]; + if(mHourMinutesOnly) { + return mInput[3] * 36_000 + mInput[2] * 3600 + mInput[1] * 600 + mInput[0] * 60; + } else { + return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0]; + } } public void saveEntryState(Bundle outState, String key) { @@ -441,4 +475,13 @@ protected void setLeftRightEnabled(boolean enabled) { mRight.setContentDescription(null); } } + + public void setHourMinutesOnly(boolean mHourMinutesOnly) { + this.mHourMinutesOnly = mHourMinutesOnly; + if(mHourMinutesOnly) { + mInputSize = 4; + mInput = new int[mInputSize]; + restyleViews(); + } + } } diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java index 769261f8..0269b8fa 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java @@ -22,6 +22,12 @@ public class HmsPickerBuilder { private int mHours; private int mMinutes; private int mSeconds; + private Boolean mHourMinutesOnly; + + public HmsPickerBuilder setHourMinutesOnly(boolean hourMinutesOnly){ + this.mHourMinutesOnly = hourMinutesOnly; + return this; + } /** * Attach a FragmentManager. This is required for creation of the Fragment. @@ -147,7 +153,7 @@ public void show() { } ft.addToBackStack(null); - final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId); + final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, mHourMinutesOnly); if (targetFragment != null) { fragment.setTargetFragment(targetFragment, 0); } diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java index c64452d8..172664e9 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java @@ -22,6 +22,7 @@ public class HmsPickerDialogFragment extends DialogFragment { private static final String REFERENCE_KEY = "HmsPickerDialogFragment_ReferenceKey"; private static final String THEME_RES_ID_KEY = "HmsPickerDialogFragment_ThemeResIdKey"; + private static final String HOUR_MINUTES_ONLY_KEY = "HmsPickerDialogFragment_HourMinutesOnly"; private HmsPicker mPicker; @@ -33,6 +34,7 @@ public class HmsPickerDialogFragment extends DialogFragment { private int mHours; private int mMinutes; private int mSeconds; + private boolean mHourMinutesOnly = false; /** * Create an instance of the Picker (used internally) @@ -41,11 +43,14 @@ public class HmsPickerDialogFragment extends DialogFragment { * @param themeResId the style resource ID for theming * @return a Picker! */ - public static HmsPickerDialogFragment newInstance(int reference, int themeResId) { + public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Boolean hourMinutesOnly) { final HmsPickerDialogFragment frag = new HmsPickerDialogFragment(); Bundle args = new Bundle(); args.putInt(REFERENCE_KEY, reference); args.putInt(THEME_RES_ID_KEY, themeResId); + if(hourMinutesOnly != null){ + args.putBoolean(HOUR_MINUTES_ONLY_KEY, hourMinutesOnly); + } frag.setArguments(args); return frag; } @@ -66,6 +71,9 @@ public void onCreate(Bundle savedInstanceState) { if (args != null && args.containsKey(THEME_RES_ID_KEY)) { mTheme = args.getInt(THEME_RES_ID_KEY); } + if(args != null && args.containsKey(HOUR_MINUTES_ONLY_KEY)){ + mHourMinutesOnly = args.getBoolean(HOUR_MINUTES_ONLY_KEY); + } setStyle(DialogFragment.STYLE_NO_TITLE, 0); @@ -123,6 +131,7 @@ public void onClick(View view) { mPicker.setSetButton(doneButton); mPicker.setTime(mHours, mMinutes, mSeconds); mPicker.setTheme(mTheme); + mPicker.setHourMinutesOnly(mHourMinutesOnly); getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId); diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java index bed74f8d..e6b31d0a 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java @@ -12,11 +12,12 @@ public class HmsView extends LinearLayout { - private ZeroTopPaddingTextView mHoursOnes; + private ZeroTopPaddingTextView mHoursOnes, mHoursTens; private ZeroTopPaddingTextView mMinutesOnes, mMinutesTens; - private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens; + private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens, mSecondsLabel; private final Typeface mAndroidClockMonoThin; private Typeface mOriginalHoursTypeface; + private boolean hourMinutesOnly = false; private ColorStateList mTextColor; @@ -61,6 +62,9 @@ public void setTheme(int themeResId) { } private void restyleViews() { + if (mHoursTens != null) { + mHoursTens.setTextColor(mTextColor); + } if (mHoursOnes != null) { mHoursOnes.setTextColor(mTextColor); } @@ -82,30 +86,64 @@ private void restyleViews() { protected void onFinishInflate() { super.onFinishInflate(); + mHoursTens = (ZeroTopPaddingTextView) findViewById(R.id.hours_tens); mHoursOnes = (ZeroTopPaddingTextView) findViewById(R.id.hours_ones); mMinutesTens = (ZeroTopPaddingTextView) findViewById(R.id.minutes_tens); mMinutesOnes = (ZeroTopPaddingTextView) findViewById(R.id.minutes_ones); mSecondsTens = (ZeroTopPaddingTextView) findViewById(R.id.seconds_tens); mSecondsOnes = (ZeroTopPaddingTextView) findViewById(R.id.seconds_ones); + mSecondsLabel = (ZeroTopPaddingTextView) findViewById(R.id.seconds_label); + + rearrangeView(); + } + + public void rearrangeView(){ + if (mHoursTens != null && !hourMinutesOnly) { + mHoursTens.setVisibility(GONE); + } if (mHoursOnes != null) { mOriginalHoursTypeface = mHoursOnes.getTypeface(); mHoursOnes.updatePaddingForBoldDate(); } - if (mMinutesTens != null) { + if (mMinutesTens != null && !hourMinutesOnly) { mMinutesTens.updatePaddingForBoldDate(); } - if (mMinutesOnes != null) { + if (mMinutesOnes != null && !hourMinutesOnly) { mMinutesOnes.updatePaddingForBoldDate(); } // Set the lowest time unit with thin font (excluding hundredths) - if (mSecondsTens != null) { + if (mSecondsTens != null && !hourMinutesOnly) { mSecondsTens.setTypeface(mAndroidClockMonoThin); mSecondsTens.updatePadding(); } - if (mSecondsOnes != null) { + if (mSecondsOnes != null && !hourMinutesOnly) { mSecondsOnes.setTypeface(mAndroidClockMonoThin); mSecondsOnes.updatePadding(); } + + if (mHoursTens != null && hourMinutesOnly) { + mHoursTens.updatePaddingForBoldDate(); + mHoursTens.setVisibility(VISIBLE); + } + + if (mMinutesTens != null && hourMinutesOnly) { + mMinutesTens.setTypeface(mAndroidClockMonoThin); + mMinutesTens.updatePadding(); + } + if (mMinutesOnes != null && hourMinutesOnly) { + mMinutesOnes.setTypeface(mAndroidClockMonoThin); + mMinutesOnes.updatePadding(); + } + + if (mSecondsTens != null && hourMinutesOnly) { + mSecondsTens.setVisibility(GONE); + } + if (mSecondsOnes != null && hourMinutesOnly) { + mSecondsOnes.setVisibility(GONE); + } + if(mSecondsLabel != null && hourMinutesOnly){ + mSecondsLabel.setVisibility(GONE); + } } /** @@ -117,8 +155,12 @@ protected void onFinishInflate() { * @param secondsTensDigit the tens digit of the seconds TextView * @param secondsOnesDigit the ones digit of the seconds TextView */ - public void setTime(int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit, + public void setTime(int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit, int secondsOnesDigit) { + + if(mHoursTens != null){ + mHoursTens.setText(String.format("%d", hoursTensDigit)); + } if (mHoursOnes != null) { mHoursOnes.setText(String.format("%d", hoursOnesDigit)); } @@ -135,4 +177,8 @@ public void setTime(int hoursOnesDigit, int minutesTensDigit, int minutesOnesDig mSecondsOnes.setText(String.format("%d", secondsOnesDigit)); } } + + public void setHourMinutesOnly(boolean hourMinutesOnly) { + this.hourMinutesOnly = hourMinutesOnly; + } } \ No newline at end of file diff --git a/library/src/main/res/layout/hms_picker_view.xml b/library/src/main/res/layout/hms_picker_view.xml index 0f3afe08..6492fc84 100644 --- a/library/src/main/res/layout/hms_picker_view.xml +++ b/library/src/main/res/layout/hms_picker_view.xml @@ -19,6 +19,14 @@ android:layout_gravity="center" android:baselineAligned="false" android:gravity="top"> + + + + + + + diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsHourMinutesOnlyUsage.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsHourMinutesOnlyUsage.java new file mode 100644 index 00000000..0e7808d7 --- /dev/null +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsHourMinutesOnlyUsage.java @@ -0,0 +1,46 @@ +package com.codetroopers.betterpickers.sample.activity.hmspicker; + +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.codetroopers.betterpickers.hmspicker.HmsPickerBuilder; +import com.codetroopers.betterpickers.hmspicker.HmsPickerDialogFragment; +import com.codetroopers.betterpickers.sample.R; +import com.codetroopers.betterpickers.sample.activity.BaseSampleActivity; + +/** + * User: derek Date: 3/17/13 Time: 3:59 PM + */ +public class SampleHmsHourMinutesOnlyUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandler { + + private TextView mResultTextView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.text_and_button); + + mResultTextView = (TextView) findViewById(R.id.text); + Button button = (Button) findViewById(R.id.button); + + mResultTextView.setText(R.string.no_value); + button.setText(R.string.hms_picker_set); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + HmsPickerBuilder hpb = new HmsPickerBuilder() + .setHourMinutesOnly(true) + .setFragmentManager(getSupportFragmentManager()) + .setStyleResId(R.style.BetterPickersDialogFragment); + hpb.show(); + } + }); + } + + @Override + public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + } +}