public
Last active

TimePicker DialogPreference for Android

  • Download Gist
TimePreference.java
Java
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
package com.xxx.xxx.preference;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
 
// Based on http://stackoverflow.com/a/7484289/922168
 
public class TimePreference extends DialogPreference {
private int mHour = 0;
private int mMinute = 0;
private TimePicker picker = null;
private final String DEFAULT_VALUE = "00:00";
 
public static int getHour(String time) {
String[] pieces = time.split(":");
return Integer.parseInt(pieces[0]);
}
 
public static int getMinute(String time) {
String[] pieces = time.split(":");
return Integer.parseInt(pieces[1]);
}
 
public TimePreference(Context context) {
this(context, null);
}
 
public TimePreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
 
public TimePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
public void setTime(int hour, int minute) {
mHour = hour;
mMinute = minute;
String time = toTime(mHour, mMinute);
persistString(time);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
public String toTime(int hour, int minute) {
return String.valueOf(hour) + ":" + String.valueOf(minute);
}
public void updateSummary() {
String time = String.valueOf(mHour) + ":" + String.valueOf(mMinute);
setSummary(time24to12(time));
}
 
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
return picker;
}
 
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(mHour);
picker.setCurrentMinute(mMinute);
}
 
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
 
if (positiveResult) {
int currHour = picker.getCurrentHour();
int currMinute = picker.getCurrentMinute();
if (!callChangeListener(toTime(currHour, currMinute))) {
return;
}
 
// persist
setTime(currHour, currMinute);
updateSummary();
}
}
 
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
 
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
String time = null;
 
if (restorePersistedValue) {
if (defaultValue == null) {
time = getPersistedString(DEFAULT_VALUE);
}
else {
time = getPersistedString(DEFAULT_VALUE);
}
}
else {
time = defaultValue.toString();
}
 
int currHour = getHour(time);
int currMinute = getMinute(time);
// need to persist here for default value to work
setTime(currHour, currMinute);
updateSummary();
}
public static Date toDate(String inTime) {
try {
DateFormat inTimeFormat = new SimpleDateFormat("HH:mm", Locale.US);
return inTimeFormat.parse(inTime);
} catch(ParseException e) {
return null;
}
}
public static String time24to12(String inTime) {
Date inDate = toDate(inTime);
if(inDate != null) {
DateFormat outTimeFormat = new SimpleDateFormat("hh:mm a", Locale.US);
return outTimeFormat.format(inDate);
} else {
return inTime;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.