mCallBack = callBack;
Context themeContext = getContext();
- setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_set), this);
- setButton(BUTTON_NEGATIVE, themeContext.getText(R.string.cancel), (OnClickListener) null);
+ setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
setIcon(0);
setTitle(R.string.date_picker_dialog_title);
}
public void onClick(DialogInterface dialog, int which) {
- if (mCallBack != null) {
- mDatePicker.clearFocus();
- mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
- mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
- }
+ tryNotifyDateSet();
}
public void onDateChanged(DatePicker view, int year,
mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
}
+ private void tryNotifyDateSet() {
+ if (mCallBack != null) {
+ mDatePicker.clearFocus();
+ mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
+ mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
+ }
+ }
+
+ @Override
+ protected void onStop() {
+ tryNotifyDateSet();
+ super.onStop();
+ }
+
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
setTitle(R.string.time_picker_dialog_title);
Context themeContext = getContext();
- setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_set), this);
- setButton(BUTTON_NEGATIVE, themeContext.getText(R.string.cancel),
- (OnClickListener) null);
+ setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
LayoutInflater inflater =
(LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void onClick(DialogInterface dialog, int which) {
- if (mCallback != null) {
- mTimePicker.clearFocus();
- mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
- mTimePicker.getCurrentMinute());
- }
+ tryNotifyTimeSet();
}
public void updateTime(int hourOfDay, int minutOfHour) {
/* do nothing */
}
+ private void tryNotifyTimeSet() {
+ if (mCallback != null) {
+ mTimePicker.clearFocus();
+ mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
+ mTimePicker.getCurrentMinute());
+ }
+ }
+
+ @Override
+ protected void onStop() {
+ tryNotifyTimeSet();
+ super.onStop();
+ }
+
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
private static final int SELECTOR_ADJUSTMENT_DURATION_MILLIS = 800;
/**
- * The duration of scrolling to the next/previous value while changing the
- * current value by one, i.e. increment or decrement.
+ * The duration of scrolling to the next/previous value while snapping to
+ * a given position.
*/
- private static final int CHANGE_CURRENT_BY_ONE_SCROLL_DURATION = 300;
+ private static final int SNAP_SCROLL_DURATION = 300;
/**
* The strength of fading in the top and bottom while drawing the selector.
/**
* Coefficient for adjusting touch scroll distance.
*/
- private static final float TOUCH_SCROLL_DECELERATION_COEFFICIENT = 2.5f;
+ private static final float TOUCH_SCROLL_DECELERATION_COEFFICIENT = 2.0f;
/**
* The resource id for the default layout.
*/
private static final char[] DIGIT_CHARACTERS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
- };
+ };
/**
* Constant for unspecified size.
if (absDeltaMoveY > mMinFlingDistance) {
fling(initialVelocity);
} else {
- changeValueByOne(deltaMove < 0);
+ final int normalizedDeltaMove =
+ (int) (absDeltaMoveY / TOUCH_SCROLL_DECELERATION_COEFFICIENT);
+ if (normalizedDeltaMove < mSelectorElementHeight) {
+ snapToNextValue(deltaMove < 0);
+ } else {
+ snapToClosestValue();
+ }
}
onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
} else {
}
mPreviousScrollerY = 0;
if (increment) {
- mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight,
- CHANGE_CURRENT_BY_ONE_SCROLL_DURATION);
+ mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight, SNAP_SCROLL_DURATION);
} else {
- mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight,
- CHANGE_CURRENT_BY_ONE_SCROLL_DURATION);
+ mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight, SNAP_SCROLL_DURATION);
}
invalidate();
} else {
return false;
}
+ private void snapToNextValue(boolean increment) {
+ int deltaY = mCurrentScrollOffset - mInitialScrollOffset;
+ int amountToScroll = 0;
+ if (deltaY != 0) {
+ mPreviousScrollerY = 0;
+ if (deltaY > 0) {
+ if (increment) {
+ amountToScroll = - deltaY;
+ } else {
+ amountToScroll = mSelectorElementHeight - deltaY;
+ }
+ } else {
+ if (increment) {
+ amountToScroll = - mSelectorElementHeight - deltaY;
+ } else {
+ amountToScroll = - deltaY;
+ }
+ }
+ mFlingScroller.startScroll(0, 0, 0, amountToScroll, SNAP_SCROLL_DURATION);
+ invalidate();
+ }
+ }
+
+ private void snapToClosestValue() {
+ // adjust to the closest value
+ int deltaY = mInitialScrollOffset - mCurrentScrollOffset;
+ if (deltaY != 0) {
+ mPreviousScrollerY = 0;
+ if (Math.abs(deltaY) > mSelectorElementHeight / 2) {
+ deltaY += (deltaY > 0) ? -mSelectorElementHeight : mSelectorElementHeight;
+ }
+ mFlingScroller.startScroll(0, 0, 0, deltaY, SNAP_SCROLL_DURATION);
+ invalidate();
+ }
+ }
+
/**
* Command for setting the input text selection.
*/
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="16dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="16dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="16dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="16dip"
android:layout_marginRight="14dip"
android:focusable="true"
android:id="@+id/minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="14dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:id="@+id/amPm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginTop="10dip"
+ android:layout_marginBottom="10dip"
android:layout_marginLeft="16dip"
android:layout_marginRight="16dip"
android:focusable="true"
<java-symbol type="string" name="date_picker_increment_year_button" />
<java-symbol type="string" name="date_time" />
<java-symbol type="string" name="date_time_set" />
+ <java-symbol type="string" name="date_time_done" />
<java-symbol type="string" name="day_of_week_long_friday" />
<java-symbol type="string" name="day_of_week_long_monday" />
<java-symbol type="string" name="day_of_week_long_saturday" />
<string name="date_picker_dialog_title">Set date</string>
<!-- Name of the button in the date/time picker to accept the date/time change -->
<string name="date_time_set">Set</string>
+ <!-- Name of the button in the date/time picker to accept the date/time change -->
+ <string name="date_time_done">Done</string>
<!-- Security Permissions strings-->
<!-- The default permission group for any permissions that have not explicitly set a group. -->
<item name="android:selectionDividerHeight">2dip</item>
<item name="android:selectionDividersDistance">48dip</item>
<item name="android:internalMinWidth">48dip</item>
- <item name="android:internalMaxHeight">200dip</item>
+ <item name="android:internalMaxHeight">180dip</item>
<item name="android:minFlingDistance">150dip</item>
</style>