Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
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 @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
Expand Down Expand Up @@ -40,6 +41,10 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B
private int mDeleteDrawableSrcResId;
private int mTheme = -1;

private int mSign;
public static final int SIGN_POSITIVE = 0;
public static final int SIGN_NEGATIVE = 1;

/**
* Instantiates an HmsPicker object
*
Expand Down Expand Up @@ -125,6 +130,10 @@ private void restyleViews() {
if (mEnteredHms != null) {
mEnteredHms.setTheme(mTheme);
}
if(mLeft != null){
mLeft.setTextColor(mTextColor);
mLeft.setBackgroundResource(mKeyBackgroundResId);
}
}

@Override
Expand Down Expand Up @@ -155,7 +164,7 @@ protected void onFinishInflate() {
mLeft = (Button) v4.findViewById(R.id.key_left);
mNumbers[0] = (Button) v4.findViewById(R.id.key_middle);
mRight = (Button) v4.findViewById(R.id.key_right);
setLeftRightEnabled(false);
setRightEnabled(false);

for (int i = 0; i < 10; i++) {
mNumbers[i].setOnClickListener(this);
Expand All @@ -164,6 +173,12 @@ protected void onFinishInflate() {
}
updateHms();

Resources res = mContext.getResources();
mLeft.setText(res.getString(R.string.number_picker_plus_minus));
mLeft.setOnClickListener(this);
// mLabel = (TextView) findViewById(R.id.label);
// mSign = SIGN_POSITIVE;

mHoursLabel = (TextView) findViewById(R.id.hours_label);
mMinutesLabel = (TextView) findViewById(R.id.minutes_label);
mSecondsLabel = (TextView) findViewById(R.id.seconds_label);
Expand Down Expand Up @@ -203,10 +218,20 @@ protected void doOnClick(View v) {
mInput[mInputPointer] = 0;
mInputPointer--;
}
} else if(v == mLeft){
onLeftClicked();
}
updateKeypad();
}

private void onLeftClicked() {
if (mSign == SIGN_POSITIVE) {
mSign = SIGN_NEGATIVE;
} else {
mSign = SIGN_POSITIVE;
}
}

@Override
public boolean onLongClick(View v) {
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Expand Down Expand Up @@ -249,7 +274,7 @@ 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]);
mEnteredHms.setTime(mSign == SIGN_NEGATIVE, mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
}

private void addClickedNumber(int val) {
Expand Down Expand Up @@ -317,6 +342,17 @@ public int getSeconds() {
return mInput[1] * 10 + mInput[0];
}

/**
* Using View.GONE, View.VISIBILE, or View.INVISIBLE, set the visibility of the plus/minus indicator
*
* @param visibility an int using Android's View.* convention
*/
public void setPlusMinusVisibility(int visibility) {
if (mLeft != null) {
mLeft.setVisibility(visibility);
}
}

/**
* Set the current hours, minutes, and seconds on the picker.
*
Expand All @@ -342,7 +378,7 @@ public void setTime(int hours, int minutes, int seconds) {
}


@Override
@Override
public Parcelable onSaveInstanceState() {
final Parcelable parcel = super.onSaveInstanceState();
final SavedState state = new SavedState(parcel);
Expand Down Expand Up @@ -433,12 +469,14 @@ public void restoreEntryState(Bundle inState, String key) {
}
}

protected void setLeftRightEnabled(boolean enabled) {
mLeft.setEnabled(enabled);
protected void setRightEnabled(boolean enabled) {
mRight.setEnabled(enabled);
if (!enabled) {
mLeft.setContentDescription(null);
mRight.setContentDescription(null);
}
}

public boolean isNegative(){
return mSign == SIGN_NEGATIVE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public class HmsPickerBuilder {
private int mHours;
private int mMinutes;
private int mSeconds;
private Integer plusMinusVisibility;

/**
* Set the visibility of the +/- button. This takes an int corresponding to Android's View.VISIBLE, View.INVISIBLE,
* or View.GONE. When using View.INVISIBLE, the +/- button will still be present in the layout but be
* non-clickable. When set to View.GONE, the +/- button will disappear entirely, and the "0" button will occupy its
* space.
*
* @param plusMinusVisibility an int corresponding to View.VISIBLE, View.INVISIBLE, or View.GONE
* @return the current Builder object
*/
public HmsPickerBuilder setPlusMinusVisibility(int plusMinusVisibility) {
this.plusMinusVisibility = plusMinusVisibility;
return this;
}


/**
* Attach a FragmentManager. This is required for creation of the Fragment.
Expand Down Expand Up @@ -147,7 +163,7 @@ public void show() {
}
ft.addToBackStack(null);

final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId);
final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility);
if (targetFragment != null) {
fragment.setTargetFragment(targetFragment, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 PLUS_MINUS_VISIBILITY_KEY = "HmsPickerDialogFragment_PlusMinusVisibilityKey";

private HmsPicker mPicker;

Expand All @@ -33,6 +34,7 @@ public class HmsPickerDialogFragment extends DialogFragment {
private int mHours;
private int mMinutes;
private int mSeconds;
private int mPlusMinusVisibility = View.INVISIBLE;

/**
* Create an instance of the Picker (used internally)
Expand All @@ -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, Integer plusMinusVisibility) {
final HmsPickerDialogFragment frag = new HmsPickerDialogFragment();
Bundle args = new Bundle();
args.putInt(REFERENCE_KEY, reference);
args.putInt(THEME_RES_ID_KEY, themeResId);
if (plusMinusVisibility != null) {
args.putInt(PLUS_MINUS_VISIBILITY_KEY, plusMinusVisibility);
}
frag.setArguments(args);
return frag;
}
Expand All @@ -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(PLUS_MINUS_VISIBILITY_KEY)) {
mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY);
}

setStyle(DialogFragment.STYLE_NO_TITLE, 0);

Expand Down Expand Up @@ -106,6 +114,7 @@ public void onClick(View view) {
}
final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();

if (activity instanceof HmsPickerDialogHandler) {
final HmsPickerDialogHandler act =
(HmsPickerDialogHandler) activity;
Expand All @@ -115,6 +124,17 @@ public void onClick(View view) {
(HmsPickerDialogHandler) fragment;
frag.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}

if (activity instanceof HmsPickerDialogHandlerV2) {
final HmsPickerDialogHandlerV2 act =
(HmsPickerDialogHandlerV2) activity;
act.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
} else if (fragment instanceof HmsPickerDialogHandlerV2) {
final HmsPickerDialogHandlerV2 frag =
(HmsPickerDialogHandlerV2) fragment;
frag.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}

dismiss();
}
});
Expand All @@ -123,12 +143,18 @@ public void onClick(View view) {
mPicker.setSetButton(doneButton);
mPicker.setTime(mHours, mMinutes, mSeconds);
mPicker.setTheme(mTheme);
mPicker.setPlusMinusVisibility(mPlusMinusVisibility);

getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId);

return view;
}

public interface HmsPickerDialogHandlerV2 {

void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds);
}

/**
* This interface allows objects to register for the Picker's set action.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.codetroopers.betterpickers.R;
Expand All @@ -17,6 +18,7 @@ public class HmsView extends LinearLayout {
private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens;
private final Typeface mAndroidClockMonoThin;
private Typeface mOriginalHoursTypeface;
private ZeroTopPaddingTextView mMinusLabel;

private ColorStateList mTextColor;

Expand Down Expand Up @@ -76,6 +78,9 @@ private void restyleViews() {
if (mSecondsTens != null) {
mSecondsTens.setTextColor(mTextColor);
}
if (mMinusLabel != null) {
mMinusLabel.setTextColor(mTextColor);
}
}

@Override
Expand All @@ -87,6 +92,8 @@ protected void onFinishInflate() {
mMinutesOnes = (ZeroTopPaddingTextView) findViewById(R.id.minutes_ones);
mSecondsTens = (ZeroTopPaddingTextView) findViewById(R.id.seconds_tens);
mSecondsOnes = (ZeroTopPaddingTextView) findViewById(R.id.seconds_ones);
mMinusLabel = (ZeroTopPaddingTextView) findViewById(R.id.minus_label);

if (mHoursOnes != null) {
mOriginalHoursTypeface = mHoursOnes.getTypeface();
mHoursOnes.updatePaddingForBoldDate();
Expand Down Expand Up @@ -118,7 +125,15 @@ protected void onFinishInflate() {
* @param secondsOnesDigit the ones digit of the seconds TextView
*/
public void setTime(int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit,
int secondsOnesDigit) {
setTime(false, hoursOnesDigit, minutesTensDigit, minutesOnesDigit, secondsTensDigit, secondsOnesDigit);
}

public void setTime(boolean isNegative, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit,
int secondsOnesDigit) {

mMinusLabel.setVisibility(isNegative ? View.VISIBLE : View.GONE);

if (mHoursOnes != null) {
mHoursOnes.setText(String.format("%d", hoursOnesDigit));
}
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/res/layout/hms_picker_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
style="@style/label"
android:gravity="top"
android:text="@string/hms_picker_seconds_label"/>
<com.codetroopers.betterpickers.widget.ZeroTopPaddingTextView
android:id="@+id/minus_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"
android:ellipsize="none"
style="@style/label"
android:gravity="top"
android:text="@string/number_picker_minus_label"/>
</com.codetroopers.betterpickers.hmspicker.HmsView>
<ImageButton
android:id="@+id/delete"
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@
<category android:name="com.doomonafireball.betterpickers.sample.SAMPLE" />
</intent-filter>
</activity>
<activity
android:name=".activity.hmspicker.SampleHmsBasicNegativeDurationUsage"
android:label="HMS/Basic Usage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.doomonafireball.betterpickers.sample.SAMPLE" />
</intent-filter>
</activity>
<activity
android:name=".activity.hmspicker.SampleHmsThemeCustom"
android:label="HMS/Theme Custom">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 SampleHmsBasicNegativeDurationUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 {

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()
.setPlusMinusVisibility(View.VISIBLE)
.setFragmentManager(getSupportFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment);
hpb.show();
}
});
}

@Override
public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) {
mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, (isNegative ? -1 : 1) * hours, (isNegative ? -1 : 1) * minutes, (isNegative ? -1 : 1) * seconds));
}

}