Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogPreference cause error onDisplayPreferenceDialog()

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 ?

like image 337
Shifatul Avatar asked Aug 04 '17 17:08

Shifatul


Video Answer


1 Answers

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>
like image 191
Shifatul Avatar answered Sep 20 '22 15:09

Shifatul