Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimePicker in PreferenceScreen

I'd like to create a preference field called Interval and I want to be able to popup a TimePicker and set a mm:ss formated value with minimal value 00:30 and step 30 seconds.

Is it possible to use TimePicker in PreferenceScreen ?

like image 280
hsz Avatar asked Apr 03 '11 22:04

hsz


2 Answers

There is no TimePreference built into Android. However, creating your own is fairly easy. Here's one I did:

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;  public class TimePreference extends DialogPreference {     private int lastHour=0;     private int lastMinute=0;     private TimePicker picker=null;      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 ctxt, AttributeSet attrs) {         super(ctxt, attrs);          setPositiveButtonText("Set");         setNegativeButtonText("Cancel");     }      @Override     protected View onCreateDialogView() {         picker=new TimePicker(getContext());          return(picker);     }      @Override     protected void onBindDialogView(View v) {         super.onBindDialogView(v);          picker.setCurrentHour(lastHour);         picker.setCurrentMinute(lastMinute);     }      @Override     protected void onDialogClosed(boolean positiveResult) {         super.onDialogClosed(positiveResult);          if (positiveResult) {             lastHour=picker.getCurrentHour();             lastMinute=picker.getCurrentMinute();              String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);              if (callChangeListener(time)) {                 persistString(time);             }         }     }      @Override     protected Object onGetDefaultValue(TypedArray a, int index) {         return(a.getString(index));     }      @Override     protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {         String time=null;          if (restoreValue) {             if (defaultValue==null) {                 time=getPersistedString("00:00");             }             else {                 time=getPersistedString(defaultValue.toString());             }         }         else {             time=defaultValue.toString();         }          lastHour=getHour(time);         lastMinute=getMinute(time);     } } 
like image 51
CommonsWare Avatar answered Oct 05 '22 19:10

CommonsWare


I have modified the code from first answer:

  • it stores selected time in long form (milliseconds) which is easier to work with (using Calendar) then string
  • it automatically shows selected time in summary field in user's format (12 or 24 hour)

Updated code:

public class TimePreference extends DialogPreference {     private Calendar calendar;     private TimePicker picker = null;      public TimePreference(Context ctxt) {         this(ctxt, null);     }      public TimePreference(Context ctxt, AttributeSet attrs) {         this(ctxt, attrs, android.R.attr.dialogPreferenceStyle);     }      public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {         super(ctxt, attrs, defStyle);          setPositiveButtonText(R.string.set);         setNegativeButtonText(R.string.cancel);         calendar = new GregorianCalendar();     }      @Override     protected View onCreateDialogView() {         picker = new TimePicker(getContext());         return (picker);     }      @Override     protected void onBindDialogView(View v) {         super.onBindDialogView(v);         picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));         picker.setCurrentMinute(calendar.get(Calendar.MINUTE));     }      @Override     protected void onDialogClosed(boolean positiveResult) {         super.onDialogClosed(positiveResult);          if (positiveResult) {             calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());             calendar.set(Calendar.MINUTE, picker.getCurrentMinute());              setSummary(getSummary());             if (callChangeListener(calendar.getTimeInMillis())) {                 persistLong(calendar.getTimeInMillis());                 notifyChanged();             }         }     }      @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) {                 calendar.setTimeInMillis(getPersistedLong(System.currentTimeMillis()));             } else {                 calendar.setTimeInMillis(Long.parseLong(getPersistedString((String) defaultValue)));             }         } else {             if (defaultValue == null) {                 calendar.setTimeInMillis(System.currentTimeMillis());             } else {                 calendar.setTimeInMillis(Long.parseLong((String) defaultValue));             }         }         setSummary(getSummary());     }      @Override     public CharSequence getSummary() {         if (calendar == null) {             return null;         }         return DateFormat.getTimeFormat(getContext()).format(new Date(calendar.getTimeInMillis()));     } }  
like image 29
David Vávra Avatar answered Oct 05 '22 17:10

David Vávra