Skip to content Skip to sidebar Skip to footer

Creating A Dialogpreference From Xml

I have been attempting to use an android.preference.DialogPreference inflated from XML, but the documentation seems to be missing some essential bits, and I cannot find a working e

Solution 1:

Here is an example of how to use the dialog preference (subclassing as you mentioned).

package dk.myapp.views;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

/**
 * The OptionDialogPreference will display a dialog, and will persist the
 * <code>true</code> when pressing the positive button and <code>false</code>
 * otherwise. It will persist to the android:key specified in xml-preference.
 */publicclassOptionDialogPreferenceextendsDialogPreference {

    publicOptionDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @OverrideprotectedvoidonDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

}

The preferences.xml should contain

<dk.myapp.views.OptionDialogPreferenceandroid:key="@string/prefKeyResetQuests"android:dialogIcon="@android:drawable/ic_dialog_alert"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"/>

I have a res/value containing (though the key-name can also be declared explicitly above).

<stringname="prefKeyResetQuests">resetQuests</string>

My PreferenceActivity does the actual resetting from onPause. Note that onStop may be too late since the onStop will not always be called immediately after pressing back:

@OverridepublicvoidonPause() {
    SharedPreferencesprefs= android.preference.PreferenceManager.
        getDefaultSharedPreferences(getBaseContext());
    if(prefs.getBoolean(
        getResources().getString(R.string.prefKeyResetQuests), false)) {
        // apply reset, and then set the pref-value back to false
    }
}

Or equivalently since we are still in the PreferenceActivity:

@OverrideprotectedvoidonPause() {
    PreferenceprefResetQuests=
        findPreference(getResources().getString(R.string.prefKeyResetQuests));
    if(prefResetQuests.getSharedPreferences().
        getBoolean(prefResetQuests.getKey(), false)){
        // apply reset, and then set the pref-value back to false 
    }
}

Solution 2:

This one is weird, you need to subclass DialogPreference. The subclass does not need to do anything. So

publicclassMyDialogPreferenceextendsDialogPreference {

    publicMyDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

} 

can be instantiated. While a plane old DialogPreference can not. Very weird, they should be the exact same thing.

Solution 3:

First:

Create your own class which extends DialogPreference as bellow:

package com.test.view;

import android.preference.DialogPreference;

publicclassDialogExPreferenceextendsDialogPreference 
{
    publicDialogExPreference(Context oContext, AttributeSet attrs)
    {
        super(oContext, attrs);     
    }
}

Second:

Modify the xml file as bellow:

<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"><com.test.view.DialogExPreferenceandroid:title="@string/title"android:dialogMessage="@string/lite"android:negativeButtonText="test"/></PreferenceScreen>

Then it's OK.

Post a Comment for "Creating A Dialogpreference From Xml"