Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ec186706 authored by Alan Viverette's avatar Alan Viverette
Browse files

Allow alert dialogs to inflate custom view layouts

Adds APIs to set a layout resource ID as an AlertDialog's custom view. To
make this useful for developers, also ensures that Dialog content is set
up when calls are made to Dialog.findViewById() before show().

BUG: 11136748
Change-Id: I29747a28d7e30f4e31fe474424109ff29e1eaa98
parent fa706a88
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -3239,6 +3239,7 @@ package android.app {
    method public android.app.AlertDialog.Builder setSingleChoiceItems(android.widget.ListAdapter, int, android.content.DialogInterface.OnClickListener);
    method public android.app.AlertDialog.Builder setSingleChoiceItems(android.widget.ListAdapter, int, android.content.DialogInterface.OnClickListener);
    method public android.app.AlertDialog.Builder setTitle(int);
    method public android.app.AlertDialog.Builder setTitle(int);
    method public android.app.AlertDialog.Builder setTitle(java.lang.CharSequence);
    method public android.app.AlertDialog.Builder setTitle(java.lang.CharSequence);
    method public android.app.AlertDialog.Builder setView(int);
    method public android.app.AlertDialog.Builder setView(android.view.View);
    method public android.app.AlertDialog.Builder setView(android.view.View);
    method public android.app.AlertDialog show();
    method public android.app.AlertDialog show();
  }
  }
+17 −0
Original line number Original line Diff line number Diff line
@@ -851,6 +851,21 @@ public class AlertDialog extends Dialog implements DialogInterface {
            return this;
            return this;
        }
        }
        
        
        /**
         * Set a custom view resource to be the contents of the Dialog. The
         * resource will be inflated, adding all top-level views to the screen.
         *
         * @param layoutResId Resource ID to be inflated.
         * @return This Builder object to allow for chaining of calls to set
         *         methods
         */
        public Builder setView(int layoutResId) {
            P.mView = null;
            P.mViewLayoutResId = layoutResId;
            P.mViewSpacingSpecified = false;
            return this;
        }

        /**
        /**
         * Set a custom view to be the contents of the Dialog. If the supplied view is an instance
         * Set a custom view to be the contents of the Dialog. If the supplied view is an instance
         * of a {@link ListView} the light background will be used.
         * of a {@link ListView} the light background will be used.
@@ -861,6 +876,7 @@ public class AlertDialog extends Dialog implements DialogInterface {
         */
         */
        public Builder setView(View view) {
        public Builder setView(View view) {
            P.mView = view;
            P.mView = view;
            P.mViewLayoutResId = 0;
            P.mViewSpacingSpecified = false;
            P.mViewSpacingSpecified = false;
            return this;
            return this;
        }
        }
@@ -890,6 +906,7 @@ public class AlertDialog extends Dialog implements DialogInterface {
        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
                int viewSpacingRight, int viewSpacingBottom) {
                int viewSpacingRight, int viewSpacingBottom) {
            P.mView = view;
            P.mView = view;
            P.mViewLayoutResId = 0;
            P.mViewSpacingSpecified = true;
            P.mViewSpacingSpecified = true;
            P.mViewSpacingLeft = viewSpacingLeft;
            P.mViewSpacingLeft = viewSpacingLeft;
            P.mViewSpacingTop = viewSpacingTop;
            P.mViewSpacingTop = viewSpacingTop;
+11 −4
Original line number Original line Diff line number Diff line
@@ -456,13 +456,20 @@ public class Dialog implements DialogInterface, Window.Callback,
    }
    }


    /**
    /**
     * Finds a view that was identified by the id attribute from the XML that
     * Finds a child view with the given identifier.
     * was processed in {@link #onStart}.
     * <p>
     * Prior to API 20, this function could only be used after the first call
     * to {@link #show()}. In later versions, this function may be used at any
     * time; however, the first call to this function "locks in" certain dialog
     * characteristics.
     *
     *
     * @param id the identifier of the view to find
     * @param id the identifier of the view to find
     * @return The view if found or null otherwise.
     * @return The view with the given id or null.
     */
     */
    public View findViewById(int id) {
    public View findViewById(int id) {
        if (!mCreated) {
            dispatchOnCreate(null);
        }
        return mWindow.findViewById(id);
        return mWindow.findViewById(id);
    }
    }


@@ -479,7 +486,7 @@ public class Dialog implements DialogInterface, Window.Callback,
    /**
    /**
     * Set the screen content to an explicit view.  This view is placed
     * Set the screen content to an explicit view.  This view is placed
     * directly into the screen's view hierarchy.  It can itself be a complex
     * directly into the screen's view hierarchy.  It can itself be a complex
     * view hierarhcy.
     * view hierarchy.
     * 
     * 
     * @param view The desired content to display.
     * @param view The desired content to display.
     */
     */
+41 −14
Original line number Original line Diff line number Diff line
@@ -70,6 +70,8 @@ public class AlertController {
    
    
    private View mView;
    private View mView;


    private int mViewLayoutResId;

    private int mViewSpacingLeft;
    private int mViewSpacingLeft;
    
    
    private int mViewSpacingTop;
    private int mViewSpacingTop;
@@ -232,11 +234,6 @@ public class AlertController {
    public void installContent() {
    public void installContent() {
        /* We use a custom title so never request a window title */
        /* We use a custom title so never request a window title */
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        
        if (mView == null || !canTextInput(mView)) {
            mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        }
        mWindow.setContentView(mAlertDialogLayout);
        mWindow.setContentView(mAlertDialogLayout);
        setupView();
        setupView();
    }
    }
@@ -262,11 +259,21 @@ public class AlertController {
        }
        }
    }
    }


    /**
     * Set the view resource to display in the dialog.
     */
    public void setView(int layoutResId) {
        mView = null;
        mViewLayoutResId = layoutResId;
        mViewSpacingSpecified = false;
    }

    /**
    /**
     * Set the view to display in the dialog.
     * Set the view to display in the dialog.
     */
     */
    public void setView(View view) {
    public void setView(View view) {
        mView = view;
        mView = view;
        mViewLayoutResId = 0;
        mViewSpacingSpecified = false;
        mViewSpacingSpecified = false;
    }
    }
    
    
@@ -276,6 +283,7 @@ public class AlertController {
    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
            int viewSpacingBottom) {
            int viewSpacingBottom) {
        mView = view;
        mView = view;
        mViewLayoutResId = 0;
        mViewSpacingSpecified = true;
        mViewSpacingSpecified = true;
        mViewSpacingLeft = viewSpacingLeft;
        mViewSpacingLeft = viewSpacingLeft;
        mViewSpacingTop = viewSpacingTop;
        mViewSpacingTop = viewSpacingTop;
@@ -406,20 +414,36 @@ public class AlertController {
            mWindow.setCloseOnTouchOutsideIfNotSet(true);
            mWindow.setCloseOnTouchOutsideIfNotSet(true);
        }
        }


        FrameLayout customPanel = null;
        final FrameLayout customPanel = (FrameLayout) mWindow.findViewById(R.id.customPanel);
        final View customView;
        if (mView != null) {
        if (mView != null) {
            customPanel = (FrameLayout) mWindow.findViewById(R.id.customPanel);
            customView = mView;
            FrameLayout custom = (FrameLayout) mWindow.findViewById(R.id.custom);
        } else if (mViewLayoutResId != 0) {
            custom.addView(mView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
            final LayoutInflater inflater = LayoutInflater.from(mContext);
            customView = inflater.inflate(mViewLayoutResId, customPanel, false);
        } else {
            customView = null;
        }

        if (customView == null || !canTextInput(customView)) {
            mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        }

        if (customView != null) {
            final FrameLayout custom = (FrameLayout) mWindow.findViewById(R.id.custom);
            custom.addView(customView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));

            if (mViewSpacingSpecified) {
            if (mViewSpacingSpecified) {
                custom.setPadding(mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,
                custom.setPadding(
                        mViewSpacingBottom);
                        mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, mViewSpacingBottom);
            }
            }

            if (mListView != null) {
            if (mListView != null) {
                ((LinearLayout.LayoutParams) customPanel.getLayoutParams()).weight = 0;
                ((LinearLayout.LayoutParams) customPanel.getLayoutParams()).weight = 0;
            }
            }
        } else {
        } else {
            mWindow.findViewById(R.id.customPanel).setVisibility(View.GONE);
            customPanel.setVisibility(View.GONE);
        }
        }
        
        
        /* Only display the divider if we have a title and a 
        /* Only display the divider if we have a title and a 
@@ -427,7 +451,7 @@ public class AlertController {
         */
         */
        if (hasTitle) {
        if (hasTitle) {
            View divider = null;
            View divider = null;
            if (mMessage != null || mView != null || mListView != null) {
            if (mMessage != null || customView != null || mListView != null) {
                divider = mWindow.findViewById(R.id.titleDivider);
                divider = mWindow.findViewById(R.id.titleDivider);
            } else {
            } else {
                divider = mWindow.findViewById(R.id.titleDividerTop);
                divider = mWindow.findViewById(R.id.titleDividerTop);
@@ -774,6 +798,7 @@ public class AlertController {
        public CharSequence[] mItems;
        public CharSequence[] mItems;
        public ListAdapter mAdapter;
        public ListAdapter mAdapter;
        public DialogInterface.OnClickListener mOnClickListener;
        public DialogInterface.OnClickListener mOnClickListener;
        public int mViewLayoutResId;
        public View mView;
        public View mView;
        public int mViewSpacingLeft;
        public int mViewSpacingLeft;
        public int mViewSpacingTop;
        public int mViewSpacingTop;
@@ -859,6 +884,8 @@ public class AlertController {
                } else {
                } else {
                    dialog.setView(mView);
                    dialog.setView(mView);
                }
                }
            } else if (mViewLayoutResId != 0) {
                dialog.setView(mViewLayoutResId);
            }
            }


            /*
            /*