Skip to content

Instantly share code, notes, and snippets.

@0neel
Last active April 6, 2017 11:03
Show Gist options
  • Save 0neel/788c7caa64b6bd8cfefcdf50872a700e to your computer and use it in GitHub Desktop.
Save 0neel/788c7caa64b6bd8cfefcdf50872a700e to your computer and use it in GitHub Desktop.
The preference showing a dialog with a time picker. Supports disabled state.
/**
* Based on http://stackoverflow.com/a/10608622/2489474
* <p>Created by Maksim Kovalev on 05.04.2017.
*/
public class TimePreference extends DialogPreference {
public static final long TIME_NOT_SET = -1;
private TimePicker picker = null;
private long time = TIME_NOT_SET;
public TimePreference(Context context) {
this(context, null);
}
public TimePreference(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}
public TimePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
builder.setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
time = TIME_NOT_SET;
notifyTimeChanged();
}
});
}
@Override
protected View onCreateDialogView() {
Context context = getContext();
picker = new TimePicker(context);
picker.setIs24HourView(DateFormat.is24HourFormat(context));
return picker;
}
@Override
@SuppressWarnings("deprecation")
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
Calendar calendar = Calendar.getInstance();
if (time != TIME_NOT_SET) {
calendar.setTimeInMillis(time);
}
picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
@Override
@SuppressWarnings("deprecation")
protected void onDialogClosed(boolean positiveResult) {
if (!positiveResult) {
return;
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
calendar.set(Calendar.MINUTE, picker.getCurrentMinute());
time = calendar.getTimeInMillis();
notifyTimeChanged();
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if (restoreValue) {
if (defaultValue == null) {
time = getPersistedLong(TIME_NOT_SET);
} else {
time = Long.parseLong(getPersistedString((String) defaultValue));
}
} else {
if (defaultValue == null) {
time = TIME_NOT_SET;
} else {
time = Long.parseLong((String) defaultValue);
}
}
persistLong(time);
updateSummary();
}
@Override
public CharSequence getSummary() {
if (time == TIME_NOT_SET) {
return getContext().getString(R.string.disabled);
} else {
java.text.DateFormat format = DateFormat.getTimeFormat(getContext());
return format.format(new Date(time));
}
}
private void updateSummary() {
setSummary(getSummary());
}
private void notifyTimeChanged() {
updateSummary();
if (callChangeListener(time)) {
persistLong(time);
notifyChanged();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment