tobias
tobias

Reputation: 2362

Set default value for TimePickerPreference

I am using this TimePickerPreference implementation: http://www.ebessette.com/d/TimePickerPreference

It works nice. But I do not know how to set a default value for it. So if the preference is called the first time, it should show the current time. I also would like to extend it, to show the chosen time in the title.

In my preference.xml:

       <com.example.preference.TimePickerPreference
            android:key="quit_time"
            android:dialogTitle="Quit"
            android:title="Quit"/>

Upvotes: 0

Views: 603

Answers (2)

JBWhikY
JBWhikY

Reputation: 1

Firstly, I apologize for my English, I'm pulling translators . I know the thread is old, but I searched for information on this problem today and nadia had put no solution . What if it happens to anyone else here I leave . This component out there also has the bug that if you cancel the insertion of the time this is always stored . Also comment on your solution.

Let's start with the main thing, is not exactly why he never throws the setter of the default value and the quickest solution would be to manually control element builders. I personally have decided to create a new attribute called "timeByDefault" that is responsible for conducting the functionality of "defaultValue".


    /**
     * @param context
     * @param attrs
     */
    public TimePickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        handleCustomAttributes(context, attrs);
        initialize();
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public TimePickerPreference(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        handleCustomAttributes(context, attrs);
        initialize();
    }

    private void handleCustomAttributes(Context context, AttributeSet attrs){
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.TimePickerPreference);
        CharSequence timeByDefaultCS = arr.getString(R.styleable.TimePickerPreference_timeByDefault);
        if (null != timeByDefaultCS){
            setTimeByDefault(timeByDefaultCS.toString());
        }
    }

    public void setTimeByDefault(Object defaultValue) {
        setDefaultValue(defaultValue);
    }

Now we create the attribute in "attrs.xml" file in the folder "/res/values​/", and add the following resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TimePickerPreference">
        < attr name="timeByDefault" format="string" />
    </declare-styleable>
</resources>

It will be very important for the new attribute can be accessed from xml will declare xmlns:custom="http://schemas.android.com/apk/res-auto" at the beginning of our xml file in the manner indicated below.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    >
    <PreferenceCategory android:title="Notifications">

        <com.mypackage.TimePickerPreference 
            android:key="KEY_TO_SAVE_THE_TIME_IN_PREFERENCE"
            android:title="My custom title"
            android:summary="My detail"
            android:dialogTitle="My dialog title"
            custom:timeByDefault="07:30"
            />

    </PreferenceCategory>

</PreferenceScreen>

Now I will explain the issue of canceling the date change:

Opening the dialog box allows us to accept or cancel the new time. This component accepts always time whatever you do. So I created a variable to temporarily store intermediate private time really introduced until accept that value.

private String tmpPersistString = "";

Replace the following function to leave as follows:

@Override
public void onTimeChanged(TimePicker view, int hour, int minute) {
    tmpPersistString = hour + ":" + minute;
}

Similarly we control when we pressed one of two buttons, that when we are pressing to accept it then that we store the value of the new time:

@Override
public void onClick(DialogInterface dialog, int which) {
    super.getDialog().getCurrentFocus().clearFocus();
    super.onClick(dialog, which);
    if (which != -2){
        persistString(tmpPersistString);
        tmpPersistString = "";
    }
}

And although it is not entirely necessary to initialize the temporary variable ground when the dialog if the ESC key is pressed to close if you have physical keyboard, or the return button or clicks outside the window.

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    tmpPersistString = "";

}

I can only hope that you have understood enough to make this component a good choice at the time to develop and it will be of use to more people. A greeting!

Upvotes: 0

evilone
evilone

Reputation: 22740

Maybe this code helps you TimePickerPreference.java

Upvotes: 1

Related Questions