FileDocCategorySizeDatePackage
DialogPreference.javaAPI DocAndroid 5.1 API16122Thu Mar 12 22:22:10 GMT 2015android.preference

DialogPreference

public abstract class DialogPreference extends Preference implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener, PreferenceManager.OnActivityDestroyListener
A base class for {@link Preference} objects that are dialog-based. These preferences will, when clicked, open a dialog showing the actual preference controls.
attr
ref android.R.styleable#DialogPreference_dialogTitle
attr
ref android.R.styleable#DialogPreference_dialogMessage
attr
ref android.R.styleable#DialogPreference_dialogIcon
attr
ref android.R.styleable#DialogPreference_dialogLayout
attr
ref android.R.styleable#DialogPreference_positiveButtonText
attr
ref android.R.styleable#DialogPreference_negativeButtonText

Fields Summary
private AlertDialog.Builder
mBuilder
private CharSequence
mDialogTitle
private CharSequence
mDialogMessage
private android.graphics.drawable.Drawable
mDialogIcon
private CharSequence
mPositiveButtonText
private CharSequence
mNegativeButtonText
private int
mDialogLayoutResId
private android.app.Dialog
mDialog
The dialog, if it is showing.
private int
mWhichButtonClicked
Which button was clicked.
Constructors Summary
public DialogPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)

        super(context, attrs, defStyleAttr, defStyleRes);

        final TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes);
        mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle);
        if (mDialogTitle == null) {
            // Fallback on the regular title of the preference
            // (the one that is seen in the list)
            mDialogTitle = getTitle();
        }
        mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage);
        mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon);
        mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText);
        mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText);
        mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout,
                mDialogLayoutResId);
        a.recycle();
    
public DialogPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        this(context, attrs, defStyleAttr, 0);
    
public DialogPreference(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
    
public DialogPreference(android.content.Context context)

        this(context, null);
    
Methods Summary
public android.app.DialoggetDialog()
Gets the dialog that is shown by this preference.

return
The dialog, or null if a dialog is not being shown.

        return mDialog;
    
public android.graphics.drawable.DrawablegetDialogIcon()
Returns the icon to be shown on subsequent dialogs.

return
The icon, as a {@link Drawable}.

        return mDialogIcon;
    
public intgetDialogLayoutResource()
Returns the layout resource that is used as the content View for subsequent dialogs.

return
The layout resource.

        return mDialogLayoutResId;
    
public java.lang.CharSequencegetDialogMessage()
Returns the message to be shown on subsequent dialogs.

return
The message.

        return mDialogMessage;
    
public java.lang.CharSequencegetDialogTitle()
Returns the title to be shown on subsequent dialogs.

return
The title.

        return mDialogTitle;
    
public java.lang.CharSequencegetNegativeButtonText()
Returns the text of the negative button to be shown on subsequent dialogs.

return
The text of the negative button.

        return mNegativeButtonText;
    
public java.lang.CharSequencegetPositiveButtonText()
Returns the text of the positive button to be shown on subsequent dialogs.

return
The text of the positive button.

        return mPositiveButtonText;
    
protected booleanneedInputMethod()
Returns whether the preference needs to display a soft input method when the dialog is displayed. Default is false. Subclasses should override this method if they need the soft input method brought up automatically.

hide

        return false;
    
public voidonActivityDestroy()
{@inheritDoc}

        
        if (mDialog == null || !mDialog.isShowing()) {
            return;
        }
        
        mDialog.dismiss();
    
protected voidonBindDialogView(android.view.View view)
Binds views in the content View of the dialog to data.

Make sure to call through to the superclass implementation.

param
view The content View of the dialog, if it is custom.

        View dialogMessageView = view.findViewById(com.android.internal.R.id.message);
        
        if (dialogMessageView != null) {
            final CharSequence message = getDialogMessage();
            int newVisibility = View.GONE;
            
            if (!TextUtils.isEmpty(message)) {
                if (dialogMessageView instanceof TextView) {
                    ((TextView) dialogMessageView).setText(message);
                }
                
                newVisibility = View.VISIBLE;
            }
            
            if (dialogMessageView.getVisibility() != newVisibility) {
                dialogMessageView.setVisibility(newVisibility);
            }
        }
    
protected voidonClick()

        if (mDialog != null && mDialog.isShowing()) return;

        showDialog(null);
    
public voidonClick(android.content.DialogInterface dialog, int which)

        mWhichButtonClicked = which;
    
protected android.view.ViewonCreateDialogView()
Creates the content view for the dialog (if a custom content view is required). By default, it inflates the dialog layout resource if it is set.

return
The content View for the dialog.
see
#setLayoutResource(int)

        if (mDialogLayoutResId == 0) {
            return null;
        }
        
        LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext());
        return inflater.inflate(mDialogLayoutResId, null);
    
protected voidonDialogClosed(boolean positiveResult)
Called when the dialog is dismissed and should be used to save data to the {@link SharedPreferences}.

param
positiveResult Whether the positive button was clicked (true), or the negative button was clicked or the dialog was canceled (false).

    
public voidonDismiss(android.content.DialogInterface dialog)

        
        getPreferenceManager().unregisterOnActivityDestroyListener(this);
        
        mDialog = null;
        onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE);
    
protected voidonPrepareDialogBuilder(AlertDialog.Builder builder)
Prepares the dialog builder to be shown when the preference is clicked. Use this to set custom properties on the dialog.

Do not {@link AlertDialog.Builder#create()} or {@link AlertDialog.Builder#show()}.

    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        if (myState.isDialogShowing) {
            showDialog(myState.dialogBundle);
        }
    
protected android.os.ParcelableonSaveInstanceState()

        final Parcelable superState = super.onSaveInstanceState();
        if (mDialog == null || !mDialog.isShowing()) {
            return superState;
        }

        final SavedState myState = new SavedState(superState);
        myState.isDialogShowing = true;
        myState.dialogBundle = mDialog.onSaveInstanceState();
        return myState;
    
private voidrequestInputMethod(android.app.Dialog dialog)
Sets the required flags on the dialog window to enable input method window to show up.

        Window window = dialog.getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
public voidsetDialogIcon(android.graphics.drawable.Drawable dialogIcon)
Sets the icon of the dialog. This will be shown on subsequent dialogs.

param
dialogIcon The icon, as a {@link Drawable}.

        mDialogIcon = dialogIcon;
    
public voidsetDialogIcon(int dialogIconRes)
Sets the icon (resource ID) of the dialog. This will be shown on subsequent dialogs.

param
dialogIconRes The icon, as a resource ID.

        mDialogIcon = getContext().getDrawable(dialogIconRes);
    
public voidsetDialogLayoutResource(int dialogLayoutResId)
Sets the layout resource that is inflated as the {@link View} to be shown as the content View of subsequent dialogs.

param
dialogLayoutResId The layout resource ID to be inflated.
see
#setDialogMessage(CharSequence)

        mDialogLayoutResId = dialogLayoutResId;
    
public voidsetDialogMessage(java.lang.CharSequence dialogMessage)
Sets the message of the dialog. This will be shown on subsequent dialogs.

This message forms the content View of the dialog and conflicts with list-based dialogs, for example. If setting a custom View on a dialog via {@link #setDialogLayoutResource(int)}, include a text View with ID {@link android.R.id#message} and it will be populated with this message.

param
dialogMessage The message.

        mDialogMessage = dialogMessage;
    
public voidsetDialogMessage(int dialogMessageResId)

see
#setDialogMessage(CharSequence)
param
dialogMessageResId The dialog message as a resource.

        setDialogMessage(getContext().getString(dialogMessageResId));
    
public voidsetDialogTitle(java.lang.CharSequence dialogTitle)
Sets the title of the dialog. This will be shown on subsequent dialogs.

param
dialogTitle The title.

        mDialogTitle = dialogTitle;
    
public voidsetDialogTitle(int dialogTitleResId)

see
#setDialogTitle(CharSequence)
param
dialogTitleResId The dialog title as a resource.

        setDialogTitle(getContext().getString(dialogTitleResId));
    
public voidsetNegativeButtonText(java.lang.CharSequence negativeButtonText)
Sets the text of the negative button of the dialog. This will be shown on subsequent dialogs.

param
negativeButtonText The text of the negative button.

        mNegativeButtonText = negativeButtonText;
    
public voidsetNegativeButtonText(int negativeButtonTextResId)

see
#setNegativeButtonText(CharSequence)
param
negativeButtonTextResId The negative button text as a resource.

        setNegativeButtonText(getContext().getString(negativeButtonTextResId));
    
public voidsetPositiveButtonText(java.lang.CharSequence positiveButtonText)
Sets the text of the positive button of the dialog. This will be shown on subsequent dialogs.

param
positiveButtonText The text of the positive button.

        mPositiveButtonText = positiveButtonText;
    
public voidsetPositiveButtonText(int positiveButtonTextResId)

see
#setPositiveButtonText(CharSequence)
param
positiveButtonTextResId The positive button text as a resource.

        setPositiveButtonText(getContext().getString(positiveButtonTextResId));
    
protected voidshowDialog(android.os.Bundle state)
Shows the dialog associated with this Preference. This is normally initiated automatically on clicking on the preference. Call this method if you need to show the dialog on some other event.

param
state Optional instance state to restore on the dialog

        Context context = getContext();

        mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;
        
        mBuilder = new AlertDialog.Builder(context)
            .setTitle(mDialogTitle)
            .setIcon(mDialogIcon)
            .setPositiveButton(mPositiveButtonText, this)
            .setNegativeButton(mNegativeButtonText, this);

        View contentView = onCreateDialogView();
        if (contentView != null) {
            onBindDialogView(contentView);
            mBuilder.setView(contentView);
        } else {
            mBuilder.setMessage(mDialogMessage);
        }
        
        onPrepareDialogBuilder(mBuilder);
        
        getPreferenceManager().registerOnActivityDestroyListener(this);
        
        // Create the dialog
        final Dialog dialog = mDialog = mBuilder.create();
        if (state != null) {
            dialog.onRestoreInstanceState(state);
        }
        if (needInputMethod()) {
            requestInputMethod(dialog);
        }
        dialog.setOnDismissListener(this);
        dialog.show();