DialogPreference cause error onDisplayPreferenceDialog()
F

1

12

I would like to create a simple (yes/no) dialog preference, I followed the the answers in this post - creating a DialogPreference from XML

But I get an error saying

java.lang.IllegalArgumentException: Tried to display dialog for unknown preference type. Did you forget to override onDisplayPreferenceDialog()?
    at android.support.v7.preference.PreferenceFragmentCompat.onDisplayPreferenceDialog(PreferenceFragmentCompat.java:647)
    at android.support.v7.preference.PreferenceManager.showDialog(PreferenceManager.java:553)
    at android.support.v7.preference.DialogPreference.onClick(DialogPreference.java:262)
    at android.support.v7.preference.Preference.performClick(Preference.java:1115)
    at android.support.v7.preference.Preference.performClick(Preference.java:1100)
    at android.support.v7.preference.Preference$1.onClick(Preference.java:170)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

My XML

  <android.support.v7.preference.PreferenceCategory android:title="History &amp; privacy">

    <com.game.prefs.views.DialogPref
        android:key="pref_clear_search"
        android:title="Reset Quests"
        android:summary="Reset all quest-progress."
        android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
        android:positiveButtonText="Clear Quests"
        android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>

And my JAVA code

public class DialogPref extends DialogPreference {
    public DialogPref(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

What am I doing wrong ?

Fredericfrederica answered 4/8, 2017 at 17:24 Comment(5)
I think my answer isn't really helpful, so I will delete it. But I found two links which may help you: codepath/android_guides on Github and Building a Custom Preference from the official documentation (was easier once I started looking specifically for ...Compat) There it says that you have to "specify the user interface" - maybe that's the missing piece? Good luck!Malpractice
Thanks, I will try it out and let you know.Fredericfrederica
@0X0nosugar Thanks so much for the links, finally figured it outFredericfrederica
Thanks for letting me know :) Why don't you write down how you did it (answering your own question)? Others might find this useful, and I'd be happy to upvoteMalpractice
@0X0nosugar I just didFredericfrederica
F
23

Solution: in PreferenceFragment.java Override the following method

@Override
public void onDisplayPreferenceDialog(Preference preference) {
    if (preference instanceof CustomDialogPreference) {
        DialogFragment dialogFragment = DialogPrefFragCompat.newInstance(preference.getKey());
        dialogFragment.setTargetFragment(this, 0);
        dialogFragment.show(getFragmentManager(), null);
    } else super.onDisplayPreferenceDialog(preference);
}

CustomDialogPreference .java

public class CustomDialogPreference  extends DialogPreference {
    public CustomDialogPreference(Context context) {
        this(context, null);
    }

    public CustomDialogPreference(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.dialogPreferenceStyle);
    }

    public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, defStyleAttr);
    }

    public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}

DialogPrefFragCompat.java

public class DialogPrefFragCompat extends PreferenceDialogFragmentCompat {
    public static DialogPrefFragCompat newInstance(String key) {
        final DialogPrefFragCompat fragment = new DialogPrefFragCompat();
        final Bundle bundle = new Bundle(1);
        bundle.putString(ARG_KEY, key);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // do things
        }
    }
}

xml:

<android.support.v7.preference.PreferenceCategory android:title="History &amp; privacy">
    <com.game.prefs.views.CustomDialogPreference
        android:key="pref_clear_search"
        android:title="Reset Quests"
        android:summary="Reset all quest-progress."
        android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
        android:dialogTitle=""
        android:positiveButtonText="Clear Quests"
        android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>
Fredericfrederica answered 5/8, 2017 at 17:54 Comment(2)
I'm having the same problem here. What is PreferenceFragment and how is it wired into your custom preference, CustomDialogPreference?Gutta
They really made it simple.Melanous

© 2022 - 2024 — McMap. All rights reserved.