Trying to write a custom dialog preference (a NumberPickerDialog
). Although the android documentation details on this topic, I seem to miss some essential building blocks in their documentation.
What I have so far is a custom preference dialog that shows up in the settings activity. I can click on the preference and fill in a value and press OK/Cancel.
The custom layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextNumberPickerValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
NumberPickerDialog (work in progress...):
public class NumberPickerPreference extends DialogPreference {
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.numberpicker_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
setPersistent(false);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
Editor editor = getEditor();
persistInt( value??? );
}
}
}
Preference.xml extended with:
<com.cleancode.utils.numberpickerpreference.NumberPickerPreference
android:defaultValue="550"
android:key="prefLongFlashDuration"
android:summary="@string/long_flash_duration_summary"
android:title="@string/long_flash_duration" />
How can I:
- actually show the default value of 550?
- retrieve the value from the dialog?
- enforce that only integer values are entered?
I hope somebody can shed some light on this, pitty Android documentation is not a little newby friendly on this topic.
Many thanks.