mod
mod

Reputation: 11

Android TimePicker in PreferenceScreen -> read the values

I have a custom DialogPreference. The Dialog is called from a PreferenceScreen. All works fine, the Dialog starts and shows the TimePicker.

But how do I get the selected values? First of all, I tried to write the selected hours in the summary of the Preference. (therefore the var xxx :) Later on, I want to save the values in SharedPreferences.

This is what I have for now:

public class Calendar extends DialogPreference implements
    TimePicker.OnTimeChangedListener {

TimePicker tp;
int xxx;

public Calendar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public Calendar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

private void initialize() {
    setPersistent(true);
}

@Override
protected View onCreateDialogView() {
    tp = new TimePicker(getContext());
    tp.setIs24HourView(true);

    return tp;
}

@Override
public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        // getEditor().
        setTitle(getTitle());
        setSummary(Integer.toString(xxx));

    }
}
private TimePicker.OnTimeChangedListener mTimeSetListener =
    new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            xxx=hourOfDay;

        }
    };
}

Thanks a lot and best regards

Upvotes: 1

Views: 3193

Answers (2)

Shahkar Raza
Shahkar Raza

Reputation: 385

To store the value in shared pref, implement on preference Change Listener. note that preference should be default Shared preference

preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();

inside onprefchange:

if (preference.getKey().equals(getString(R.string.reminder_end_time_key))){

    editor.putString("End_Date", String.valueOf(newValue));
    editor.apply();
    endTimePickerPreference.setSummary((CharSequence)newValue);
}

Upvotes: 0

Yashima
Yashima

Reputation: 2238

Thanks for asking this question, it provided me with an important answer on how to create a DialogPreference.

I hope I might also have an answer for you. I modified your code a little bit and I can now store the time selected from the Dialog:

@Override
protected View onCreateDialogView() {
    this.tp = new TimePicker(getContext());
    this.tp.setIs24HourView(true);
    final String storedValue = getPersistedString("07:00");
    final String[] split = storedValue.split(":");
    this.tp.setCurrentHour(Integer.parseInt(split[0]));
    this.tp.setCurrentMinute(Integer.parseInt(split[1]));
    return this.tp;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        final String result = this.tp.getCurrentHour() + ":" + this.tp.getCurrentMinute();
        persistString(result);
    }
}

When the dialog is shown I retrieve the stored value and simply set the currentHour and currentMinute fields of the TimePicker. The other way round when the dialog is closed. Since I control both the format on the way in as well as on the way out there should not be a problem with illegal values.

Was this what you were looking for?

Upvotes: 1

Related Questions