30

We are currently migrating to Androidx namespace with our Android app project. However I noticed that not only the namespace seems to have changed. For DialogPreference also some interfaces which were using before are now missing

For example the following methods seem to be missing: onBindDialogView, showDialog, onDialogClosed.

Since we use some of these methods to influence the default behavior of the dialog, it is unclear to me how I should realize this functionality now. For example we are validating the input before closing the dialog, we are saving the value in a database instead of the sharedpreferences and adding some dynamic elements to the dialog.

Has anyone else already encountered this problem and found a solution? Did I miss anything in the documentation? Is there another concept that we can / should use?

It would be possible to use Fragments instead of DialogPreference but for small amounts of content (e.g. a list of tree items, where the user can choose from) this seems to be a lot of overhead for me...

little_planet
  • 655
  • 1
  • 6
  • 13

3 Answers3

25

Starting from androidx source files, I've migrated custom classes based on old DialogPreference to new androidx.preference.DialogPreference with the following procedure:

Step 1

The old custom dialog class (e.g. CustomDialogPreference) based on legacy DialogPreference should be split into two separate classes:

  1. One class (e.g. CustomPreference) should extend androidx.preference.DialogPreference and will contain only the code related to preference handling (data management).
  2. Another class (e.g. CustomDialog) should extend androidx.preference.PreferenceDialogFragmentCompat and will contain only the code related to dialog handling (user interface), including onDialogClosed. This class should expose a static method newInstance to return an instance of this class.

Step 2

In the main fragment handling preferences based on PreferenceFragmentCompat the onDisplayPreferenceDialog method should be overridden to show the custom dialog, e.g.:

    private static final String DIALOG_FRAGMENT_TAG = "CustomPreference";

    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
            return;
        }

        if (preference instanceof CustomPreference) {
            final DialogFragment f = CustomDialog.newInstance(preference.getKey());
            f.setTargetFragment(this, 0);
            f.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG);
        } else {
            super.onDisplayPreferenceDialog(preference);
        }
    }
Livio
  • 530
  • 7
  • 14
  • same what I did. Just hadn't found the time to write an answer. Thank you very much. – little_planet Nov 15 '18 at 07:08
  • 12
    @little_planet Can u pl show the contents of both custom classes as well? – Shahood ul Hassan Apr 17 '19 at 13:57
  • 3
    almost 100% true for modern AndroidX as well: https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-3-ae9793fd31ec – PIXP Sep 08 '19 at 15:46
  • Can please either of owner or @Livio provide working examples? I believe there are many of us that find your answers helpful but don't manage to get it working. – Androidz Jan 14 '20 at 10:03
  • 2
    @Androidz I successfully followed this answer and migrated to AndroidX ColorPicker preference, embedded into my "ToDo Agenda". It works now. See this commit: https://github.com/andstatus/todoagenda/commit/27cce2c0a0fcddfe22ce71efd86b88d04020c7b2 – yvolk Feb 09 '20 at 09:13
  • getParentFragmentManager() have to be changed to getFragmentManager()? – Anatolii Shuba Dec 01 '20 at 22:49
3

Instead of using DialogPreference, you can write your own custom Preference with an AlertDialog. This may be a workaround for those who don't want to deal with the DialogPreference and PreferenceDialogFragmentCompat.

import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;

public class CustomDialogPreference extends Preference {
    private final Context context;
    public CustomDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }
    
    @Override
    protected void onClick() { //what happens when clicked on the preference
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("TITLE")
                .setMessage("message")
                .setPositiveButton("OK", (dialog, which) -> {
                    String preferenceKey = getKey(); //You can update the SharedPreference with the key
                    //....
               })
                .setNegativeButton("CANCEL", (dialog, which) -> {
                    //....
                })
                .create().show();
    }
}

onClick() and getKey() methods belong to the Preference class. The context object comes with the constructor and so on..

The key can be defined, as other preferences, in xml file or programmatically in the PreferenceFragment.

<com.myApp.CustomDialogPreference
    android:key="my_preference_key"
    android:summary="..."
    android:title="..." />
Ahmed
  • 125
  • 1
  • 4
1

A little hack for everyone who (like me) do not completely understand how we should combine androidx.preference.DialogPreference and androidx.preference.PreferenceDialogFragmentCompat:

Step 1:

In your DialogFragment's onAttach() method get the value of your desired SharedPreference (get the key either from newInstance() method or just hardcore it inside) and save it as a variable. On the other hand, save your new value in SharedPreference before closing your DialogFragment. By doing so, you have created your "custom Preference".

Step 2:

Create empty androidx.preference.DialogPreference and use it inside your PreferenceScreen. Then combine it with your DialogFragment as described in 2nd step by @Livio:

private static final String DIALOG_FRAGMENT_TAG = "CustomPreference";

@Override
public void onDisplayPreferenceDialog(Preference preference) {
    if (getFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
        return;
    }

    if (preference instanceof CustomPreference) {
        final DialogFragment f = CustomDialog.newInstance(preference.getKey());
        f.setTargetFragment(this, 0);
        f.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);
    } else {
        super.onDisplayPreferenceDialog(preference);
    }
}

By doing so, you will get the same result with only difference that you need to deal with SharedPreference yourself inside your DialogFragment.

Androidz
  • 261
  • 2
  • 11