Skip to content

Commit

Permalink
Both pickers now support OnDismissListeners and OnCancelListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
wdullaer committed Apr 28, 2015
1 parent 88a01a3 commit 0ef38ff
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Setup
The easiest way to add the Material DateTime Picker library to your project is by adding it as a dependency to your `build.gradle`
```java
dependencies {
compile 'com.wdullaer:materialdatetimepicker:1.2.2'
compile 'com.wdullaer:materialdatetimepicker:1.3.0'
}
```

Expand Down Expand Up @@ -81,6 +81,17 @@ tdp.setThemeDark(true);
```
It doesn't strictly follow the Material Design spec, but gets the job done for the time being

* `OnDismissListener` and `OnCancelListener`
Both pickers can be passed a `DialogInterface.OnDismissLisener` or `DialogInterface.OnCancelListener` which allows you to run code when either of these events occur.
```java
tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
```

Potential Improvements
----------------------
* Landscape timepicker can use some improvement
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:1.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME=1.2.2
VERSION_CODE=5
VERSION_NAME=1.3.0
VERSION_CODE=6
GROUP=com.wdullaer

ANDROID_BUILD_MIN_SDK_VERSION=14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -81,6 +82,8 @@ public class DatePickerDialog extends DialogFragment implements
private final Calendar mCalendar = Calendar.getInstance();
private OnDateSetListener mCallBack;
private HashSet<OnDateChangedListener> mListeners = new HashSet<OnDateChangedListener>();
private DialogInterface.OnCancelListener mOnCancelListener;
private DialogInterface.OnDismissListener mOnDismissListener;

private AccessibleDateAnimator mAnimator;

Expand Down Expand Up @@ -263,7 +266,7 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
tryVibrate();
dismiss();
getDialog().cancel();
}
});
cancelButton.setTypeface(TypefaceHelper.get(activity,"Roboto-Medium"));
Expand Down Expand Up @@ -296,6 +299,18 @@ public void onPause() {
mHapticFeedbackController.stop();
}

@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
if(mOnCancelListener != null) mOnCancelListener.onCancel(dialog);
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if(mOnDismissListener != null) mOnDismissListener.onDismiss(dialog);
}

private void setCurrentView(final int viewIndex) {
long millis = mCalendar.getTimeInMillis();

Expand Down Expand Up @@ -436,6 +451,14 @@ public void setOnDateSetListener(OnDateSetListener listener) {
mCallBack = listener;
}

public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
mOnCancelListener = onCancelListener;
}

public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
mOnDismissListener = onDismissListener;
}

// If the newly selected month / year does not contain the currently selected day number,
// change the selected day number to the last day of the selected month or year.
// e.g. Switching from Mar to Apr when Mar 31 is selected -> Apr 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.app.ActionBar.LayoutParams;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class TimePickerDialog extends DialogFragment implements OnValueSelectedL
private static final int PULSE_ANIMATOR_DELAY = 300;

private OnTimeSetListener mCallback;
private DialogInterface.OnCancelListener mOnCancelListener;
private DialogInterface.OnDismissListener mOnDismissListener;

private HapticFeedbackController mHapticFeedbackController;

Expand Down Expand Up @@ -171,6 +174,14 @@ public void setOnTimeSetListener(OnTimeSetListener callback) {
mCallback = callback;
}

public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
mOnCancelListener = onCancelListener;
}

public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
mOnDismissListener = onDismissListener;
}

public void setStartTime(int hourOfDay, int minute) {
mInitialHourOfDay = hourOfDay;
mInitialMinute = minute;
Expand Down Expand Up @@ -275,7 +286,7 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
tryVibrate();
dismiss();
getDialog().cancel();
}
});
mCancelButton.setTypeface(TypefaceHelper.get(getDialog().getContext(),"Roboto-Medium"));
Expand Down Expand Up @@ -369,6 +380,18 @@ public void onPause() {
mHapticFeedbackController.stop();
}

@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
if(mOnCancelListener != null) mOnCancelListener.onCancel(dialog);
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if(mOnDismissListener != null) mOnDismissListener.onDismiss(dialog);
}

public void tryVibrate() {
mHapticFeedbackController.tryVibrate();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.wdullaer.datetimepickerexample;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -47,6 +50,12 @@ public void onClick(View v) {
mode24Hours.isChecked()
);
tpd.setThemeDark(false);
tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
tpd.show(getFragmentManager(), "Timepickerdialog");
}
});
Expand Down

0 comments on commit 0ef38ff

Please sign in to comment.