This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathNumberPickerDialogFragment.java
More file actions
278 lines (247 loc) · 12 KB
/
NumberPickerDialogFragment.java
File metadata and controls
278 lines (247 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.codetroopers.betterpickers.numberpicker;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.codetroopers.betterpickers.OnDialogDismissListener;
import com.codetroopers.betterpickers.R;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Vector;
/**
* Dialog to set alarm time.
*/
public class NumberPickerDialogFragment extends DialogFragment {
private static final String REFERENCE_KEY = "NumberPickerDialogFragment_ReferenceKey";
private static final String THEME_RES_ID_KEY = "NumberPickerDialogFragment_ThemeResIdKey";
private static final String MIN_NUMBER_KEY = "NumberPickerDialogFragment_MinNumberKey";
private static final String MAX_NUMBER_KEY = "NumberPickerDialogFragment_MaxNumberKey";
private static final String PLUS_MINUS_VISIBILITY_KEY = "NumberPickerDialogFragment_PlusMinusVisibilityKey";
private static final String DECIMAL_VISIBILITY_KEY = "NumberPickerDialogFragment_DecimalVisibilityKey";
private static final String LABEL_TEXT_KEY = "NumberPickerDialogFragment_LabelTextKey";
private static final String CURRENT_NUMBER_KEY = "NumberPickerDialogFragment_CurrentNumberKey";
private static final String CURRENT_DECIMAL_KEY = "NumberPickerDialogFragment_CurrentDecimalKey";
private static final String CURRENT_SIGN_KEY = "NumberPickerDialogFragment_CurrentSignKey";
private NumberPicker mPicker;
private int mReference = -1;
private int mTheme = -1;
private ColorStateList mTextColor;
private String mLabelText = "";
private int mDialogBackgroundResId;
private BigDecimal mMinNumber = null;
private BigDecimal mMaxNumber = null;
private Integer mCurrentNumber = null;
private Double mCurrentDecimal = null;
private Integer mCurrentSign = null;
private int mPlusMinusVisibility = View.VISIBLE;
private int mDecimalVisibility = View.VISIBLE;
private Vector<NumberPickerDialogHandlerV2> mNumberPickerDialogHandlersV2 = new Vector<NumberPickerDialogHandlerV2>();
private OnDialogDismissListener mDismissCallback;
/**
* Create an instance of the Picker (used internally)
*
* @param reference an (optional) user-defined reference, helpful when tracking multiple Pickers
* @param themeResId the style resource ID for theming
* @param minNumber (optional) the minimum possible number
* @param maxNumber (optional) the maximum possible number
* @param plusMinusVisibility (optional) View.VISIBLE, View.INVISIBLE, or View.GONE
* @param decimalVisibility (optional) View.VISIBLE, View.INVISIBLE, or View.GONE
* @param labelText (optional) text to add as a label
* @return a Picker!
*/
public static NumberPickerDialogFragment newInstance(int reference,
int themeResId,
BigDecimal minNumber,
BigDecimal maxNumber,
Integer plusMinusVisibility,
Integer decimalVisibility,
String labelText,
Integer currentNumberValue,
Double currentDecimalValue,
Integer currentNumberSign) {
final NumberPickerDialogFragment frag = new NumberPickerDialogFragment();
Bundle args = new Bundle();
args.putInt(REFERENCE_KEY, reference);
args.putInt(THEME_RES_ID_KEY, themeResId);
if (minNumber != null) {
args.putSerializable(MIN_NUMBER_KEY, minNumber);
}
if (maxNumber != null) {
args.putSerializable(MAX_NUMBER_KEY, maxNumber);
}
if (plusMinusVisibility != null) {
args.putInt(PLUS_MINUS_VISIBILITY_KEY, plusMinusVisibility);
}
if (decimalVisibility != null) {
args.putInt(DECIMAL_VISIBILITY_KEY, decimalVisibility);
}
if (labelText != null) {
args.putString(LABEL_TEXT_KEY, labelText);
}
if (currentNumberValue != null) {
args.putInt(CURRENT_NUMBER_KEY, currentNumberValue);
}
if (currentDecimalValue != null) {
args.putDouble(CURRENT_DECIMAL_KEY, currentDecimalValue);
}
if (currentNumberSign != null) {
args.putInt(CURRENT_SIGN_KEY, currentNumberSign);
}
frag.setArguments(args);
return frag;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null && args.containsKey(REFERENCE_KEY)) {
mReference = args.getInt(REFERENCE_KEY);
}
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);
}
if (args != null && args.containsKey(DECIMAL_VISIBILITY_KEY)) {
mDecimalVisibility = args.getInt(DECIMAL_VISIBILITY_KEY);
}
if (args != null && args.containsKey(MIN_NUMBER_KEY)) {
mMinNumber = (BigDecimal) args.getSerializable(MIN_NUMBER_KEY);
}
if (args != null && args.containsKey(MAX_NUMBER_KEY)) {
mMaxNumber = (BigDecimal) args.getSerializable(MAX_NUMBER_KEY);
}
if (args != null && args.containsKey(LABEL_TEXT_KEY)) {
mLabelText = args.getString(LABEL_TEXT_KEY);
}
if (args != null && args.containsKey(CURRENT_NUMBER_KEY)) {
mCurrentNumber = args.getInt(CURRENT_NUMBER_KEY);
}
if (args != null && args.containsKey(CURRENT_DECIMAL_KEY)) {
mCurrentDecimal = args.getDouble(CURRENT_DECIMAL_KEY);
}
if (args != null && args.containsKey(CURRENT_SIGN_KEY)) {
mCurrentSign = args.getInt(CURRENT_SIGN_KEY);
}
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
// Init defaults
mTextColor = getResources().getColorStateList(R.color.dialog_text_color_holo_dark);
mDialogBackgroundResId = R.drawable.dialog_full_holo_dark;
if (mTheme != -1) {
TypedArray a = getActivity().getApplicationContext().obtainStyledAttributes(mTheme, R.styleable.BetterPickersDialogFragment);
mTextColor = a.getColorStateList(R.styleable.BetterPickersDialogFragment_bpTextColor);
mDialogBackgroundResId = a.getResourceId(R.styleable.BetterPickersDialogFragment_bpDialogBackground, mDialogBackgroundResId);
a.recycle();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.number_picker_dialog, container, false);
Button doneButton = (Button) view.findViewById(R.id.done_button);
Button cancelButton = (Button) view.findViewById(R.id.cancel_button);
cancelButton.setTextColor(mTextColor);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
doneButton.setTextColor(mTextColor);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BigDecimal number = mPicker.getEnteredNumber();
if (mMinNumber != null && mMaxNumber != null && (isSmaller(number) || isBigger(number))) {
String errorText = getString(R.string.min_max_error, mMinNumber, mMaxNumber);
mPicker.getErrorView().setText(errorText);
mPicker.getErrorView().show();
return;
} else if (mMinNumber != null && isSmaller(number)) {
String errorText = getString(R.string.min_error, mMinNumber);
mPicker.getErrorView().setText(errorText);
mPicker.getErrorView().show();
return;
} else if (mMaxNumber != null && isBigger(number)) {
String errorText = getString(R.string.max_error, mMaxNumber);
mPicker.getErrorView().setText(errorText);
mPicker.getErrorView().show();
return;
}
for (NumberPickerDialogHandlerV2 handler : mNumberPickerDialogHandlersV2) {
handler.onDialogNumberSet(mReference, mPicker.getNumber(), mPicker.getDecimal(), mPicker.getIsNegative(), number);
}
final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();
if (activity instanceof NumberPickerDialogHandlerV2) {
final NumberPickerDialogHandlerV2 act = (NumberPickerDialogHandlerV2) activity;
act.onDialogNumberSet(mReference, mPicker.getNumber(), mPicker.getDecimal(), mPicker.getIsNegative(), number);
} else if (fragment instanceof NumberPickerDialogHandlerV2) {
final NumberPickerDialogHandlerV2 frag = (NumberPickerDialogHandlerV2) fragment;
frag.onDialogNumberSet(mReference, mPicker.getNumber(), mPicker.getDecimal(), mPicker.getIsNegative(), number);
}
dismiss();
}
});
mPicker = (NumberPicker) view.findViewById(R.id.number_picker);
mPicker.setSetButton(doneButton);
mPicker.setTheme(mTheme);
mPicker.setDecimalVisibility(mDecimalVisibility);
mPicker.setPlusMinusVisibility(mPlusMinusVisibility);
mPicker.setLabelText(mLabelText);
if (mMinNumber != null) {
mPicker.setMin(mMinNumber);
}
if (mMaxNumber != null) {
mPicker.setMax(mMaxNumber);
}
mPicker.setNumber(mCurrentNumber, mCurrentDecimal, mCurrentSign);
getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId);
return view;
}
private boolean isBigger(BigDecimal number) {
return number.compareTo(mMaxNumber) > 0;
}
private boolean isSmaller(BigDecimal number) {
return number.compareTo(mMinNumber) < 0;
}
@Override
public void onDismiss(DialogInterface dialoginterface) {
super.onDismiss(dialoginterface);
if (mDismissCallback != null) {
mDismissCallback.onDialogDismiss(dialoginterface);
}
}
public void setOnDismissListener(OnDialogDismissListener ondialogdismisslistener) {
mDismissCallback = ondialogdismisslistener;
}
/**
* This interface allows objects to register for the Picker's set action.
*/
public interface NumberPickerDialogHandlerV2 {
void onDialogNumberSet(int reference, BigInteger number, double decimal, boolean isNegative, BigDecimal fullNumber);
}
/**
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
*
* @param handlers a Vector of handlers
*/
public void setNumberPickerDialogHandlersV2(Vector<NumberPickerDialogHandlerV2> handlers) {
this.mNumberPickerDialogHandlersV2 = handlers;
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
}