Question

I would like to set up a time picker in a preference screen and allow the user to pick both a start time and also an end time and store it as a persistant value like other settings.

Could you show all the coding needed to set this up because I couldn't find anything like that by searching.

I'm thinking maybe the time picker should be in a dialog but I don't yet know how to set those up yet. If you can show coding that calls a dialog with a start and end time time picker and how to save that information for later retrieval from a preferences screen that would be great.

All help will be greatly appreciated.

Truly, Emad

Was it helpful?

Solution

I have a simple TimePreference that I use in one of my books:

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);
  }
}

A production-grade implementation of this would use string resources for the two button captions. It might also have a bit more error checking on the value, in case some code updates it via SharedPreferences.Editor and does not format it properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top