Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise way of writing new DialogPreference classes?

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?

like image 851
RichardNewton Avatar asked Dec 22 '10 03:12

RichardNewton


1 Answers

The bare minimum is:

  1. MyCustomDialogPreference(Context context, AttributeSet attrs) constructor.
    • Don't forget to call super(context, attrs).
    • Call setPersistent(false) to indicate to the super Preference class that you persist the preference value on your own.
    • If you want to inflate the dialog panel layout from a resource, then also call setDialogLayoutResource(int dialogLayoutResId).
  2. onBindDialogView(View view) - update the view with the value(s) of your preference.
    • Don't forget to call super.onBindDialogView(view).
  3. onDialogClosed(boolean positiveResult) - if positiveResult is true then persist the value(s) from your view to the SharedPreferences.
    • Don't forget to call super.onDialogClosed(positiveResult).

This was the bare minimum, and it assumes that:

  • Your custom DialogPreference manages a single preference key/value pair.
  • You are responsible for persisting the preference value.
  • You are inflating the dialog panel layout from a resource.

Now for some additional options:

(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.

like image 85
Roy Sharon Avatar answered Oct 13 '22 10:10

Roy Sharon