I'm writing some custom preference dialogs in Android by extending the DialogPreference
class. However, I'm getting a bit concerned at the amount of boiler plate code that is needed for this as there appears to be a lot of behaviour to test.
For instance, this example of a number preference dialog is fairly typical: http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/NumberPreference.java
In particular, the onSave()
/RestoreInstanceState()
methods and "class SavedState" parts, which are needed so that the current changes to the dialog are retained on orientation changes are quite verbose and complex.
Does anyone have any tips for writing DialogPreference classes in a more concise way?
MyCustomDialogPreference(Context context, AttributeSet attrs)
constructor. super(context, attrs)
.setPersistent(false)
to indicate to the super Preference class that you persist the preference value on your own.setDialogLayoutResource(int dialogLayoutResId)
.onBindDialogView(View view)
- update the view with the value(s) of your preference. super.onBindDialogView(view)
.onDialogClosed(boolean positiveResult)
- if positiveResult is true then persist the value(s) from your view to the SharedPreferences. super.onDialogClosed(positiveResult)
.This was the bare minimum, and it assumes that:
(a) If you want to create the dialog panel layout programmatically, then implement also onCreateDialogView()
instead of calling setDialogLayoutResource()
in the constructor.
(b) If your preference supports only a single key/value pair, then you can use the helper save methods persistBoolean(boolean), persistFloat(float), persistInt(int), persistLong(long), persistString(String)
when you persist the changed preference value in onDialogClosed(). Otherwise, you need to use the getEditor()
method, like so:
private MyCustomView myView; @Override protected void onBindDialogView(View view) { super.onBindDialogView(view); // the view was created by my custom onCreateDialogView() myView = (MyCustomView)view; SharedPreferences sharedPreferences = getSharedPreferences(); myView.setValue1(sharedPreferences.getString(myKey1, myDefaultValue1)); myView.setValue2(sharedPreferences.getString(myKey2, myDefaultValue2)); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { Editor editor = getEditor(); editor.putString(myKey1, myView.getValue1()); editor.putString(myKey2, myView.getValue2()); editor.commit(); } }
(c) If you plan to supply default values from an inflated xml, then you need to implement also the onGetDefaultValue(TypedArray a, int index)
method.
@RichardNewton, I know that a month has passed since you asked the question. I hope you can still use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With