Last active
August 29, 2015 14:10
-
-
Save macsystems/af24cad08c2f2c2794e0 to your computer and use it in GitHub Desktop.
An DatePicker Dialog Fragment which respects rotation change. Also you can customize the setMin/Max Date using the extended OnDateSetListener interface.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CoreDatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { | |
public final static String TAG = CoreDatePickerDialogFragment.class.getName(); | |
private final static int REQUEST_CODE = -1; | |
private final static String SAVED_PICKER_STATE = "CoreDatePickerDialogFragment.internal_state"; | |
private DatePickerDialog datePickerDialog; | |
private CoreOnDateSetListener callback; | |
public static CoreDatePickerDialogFragment newInstance(final Fragment targetFragment) { | |
Preconditions.checkNotNull(targetFragment); | |
Preconditions.checkArgument(targetFragment instanceof CoreOnDateSetListener, "Target need to implement CoreOnDateSetListener"); | |
// | |
final CoreDatePickerDialogFragment instance = new CoreDatePickerDialogFragment(); | |
instance.setTargetFragment(targetFragment, REQUEST_CODE); | |
return instance; | |
} | |
@Override | |
public void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final Fragment targetFragment = getTargetFragment(); | |
callback = (CoreOnDateSetListener) targetFragment; | |
} | |
@Override | |
public Dialog onCreateDialog(final Bundle savedInstanceState) { | |
final Calendar c = Calendar.getInstance(); | |
final int year = c.get(Calendar.YEAR); | |
final int month = c.get(Calendar.MONTH); | |
final int day = c.get(Calendar.DAY_OF_MONTH); | |
final DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day); | |
if (savedInstanceState != null) { | |
final Bundle internalState = savedInstanceState.getBundle(SAVED_PICKER_STATE); | |
dialog.onRestoreInstanceState(internalState); | |
} | |
dialog.getDatePicker().setMinDate(callback.getMinDate()); | |
dialog.getDatePicker().setMaxDate(callback.getMaxDate()); | |
datePickerDialog = dialog; | |
return dialog; | |
} | |
public void onDateSet(final DatePicker view, final int year, final int month, final int day) { | |
callback.onDateSet(view, year, month, day); | |
} | |
@Override | |
public void onSaveInstanceState(final Bundle outState) { | |
super.onSaveInstanceState(outState); | |
final Bundle bundle = datePickerDialog.onSaveInstanceState(); | |
outState.putBundle(SAVED_PICKER_STATE, bundle); | |
} | |
/** | |
* The callback used to indicate the user is done filling in the date. | |
*/ | |
public interface CoreOnDateSetListener extends DatePickerDialog.OnDateSetListener { | |
/** | |
* Return here your max date the user can choose | |
* | |
* @return | |
* @see android.widget.DatePicker#setMaxDate(long) | |
*/ | |
long getMaxDate(); | |
/** | |
* Return here your min date the user can choose | |
* | |
* @return | |
* @see android.widget.DatePicker#setMinDate(long) | |
*/ | |
long getMinDate(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void showEnterDateDialg() { | |
final CoreDatePickerDialogFragment coreDatePickerDialogFragment = CoreDatePickerDialogFragment.newInstance(this); | |
coreDatePickerDialogFragment.show(getFragmentManager(), CoreDatePickerDialogFragment.TAG); | |
} | |
@Override | |
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.set(year, monthOfYear, dayOfMonth); | |
final long time = calendar.getTimeInMillis(); | |
final String newDate = DATE_FORMAT.format(time); | |
dateView.setText(newDate); | |
} | |
@Override | |
public long getMaxDate() { | |
return Long.MAX_VALUE; | |
} | |
// avoid exception raised when current mills are the same. | |
@Override | |
public long getMinDate() { | |
return System.currentTimeMillis() - ONE_SECOND; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment