2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.internal.app;
19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
21 import com.android.internal.R;
23 import android.annotation.Nullable;
24 import android.app.AlertDialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.res.TypedArray;
28 import android.database.Cursor;
29 import android.graphics.drawable.Drawable;
30 import android.os.Handler;
31 import android.os.Message;
32 import android.text.TextUtils;
33 import android.util.AttributeSet;
34 import android.util.TypedValue;
35 import android.view.Gravity;
36 import android.view.KeyEvent;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.view.ViewGroup.LayoutParams;
41 import android.view.ViewParent;
42 import android.view.ViewStub;
43 import android.view.Window;
44 import android.view.WindowInsets;
45 import android.view.WindowManager;
46 import android.widget.AdapterView;
47 import android.widget.AdapterView.OnItemClickListener;
48 import android.widget.ArrayAdapter;
49 import android.widget.Button;
50 import android.widget.CheckedTextView;
51 import android.widget.CursorAdapter;
52 import android.widget.FrameLayout;
53 import android.widget.ImageView;
54 import android.widget.LinearLayout;
55 import android.widget.ListAdapter;
56 import android.widget.ListView;
57 import android.widget.ScrollView;
58 import android.widget.SimpleCursorAdapter;
59 import android.widget.TextView;
61 import java.lang.ref.WeakReference;
63 public class AlertController {
64 public static final int MICRO = 1;
66 private final Context mContext;
67 private final DialogInterface mDialogInterface;
68 protected final Window mWindow;
70 private CharSequence mTitle;
71 protected CharSequence mMessage;
72 protected ListView mListView;
75 private int mViewLayoutResId;
77 private int mViewSpacingLeft;
78 private int mViewSpacingTop;
79 private int mViewSpacingRight;
80 private int mViewSpacingBottom;
81 private boolean mViewSpacingSpecified = false;
83 private Button mButtonPositive;
84 private CharSequence mButtonPositiveText;
85 private Message mButtonPositiveMessage;
87 private Button mButtonNegative;
88 private CharSequence mButtonNegativeText;
89 private Message mButtonNegativeMessage;
91 private Button mButtonNeutral;
92 private CharSequence mButtonNeutralText;
93 private Message mButtonNeutralMessage;
95 protected ScrollView mScrollView;
97 private int mIconId = 0;
98 private Drawable mIcon;
100 private ImageView mIconView;
101 private TextView mTitleView;
102 protected TextView mMessageView;
103 private View mCustomTitleView;
105 private boolean mForceInverseBackground;
107 private ListAdapter mAdapter;
109 private int mCheckedItem = -1;
111 private int mAlertDialogLayout;
112 private int mButtonPanelSideLayout;
113 private int mListLayout;
114 private int mMultiChoiceItemLayout;
115 private int mSingleChoiceItemLayout;
116 private int mListItemLayout;
118 private boolean mShowTitle;
120 private int mButtonPanelLayoutHint = AlertDialog.LAYOUT_HINT_NONE;
122 private Handler mHandler;
124 private final View.OnClickListener mButtonHandler = new View.OnClickListener() {
126 public void onClick(View v) {
128 if (v == mButtonPositive && mButtonPositiveMessage != null) {
129 m = Message.obtain(mButtonPositiveMessage);
130 } else if (v == mButtonNegative && mButtonNegativeMessage != null) {
131 m = Message.obtain(mButtonNegativeMessage);
132 } else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
133 m = Message.obtain(mButtonNeutralMessage);
142 // Post a message so we dismiss after the above handlers are executed
143 mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)
148 private static final class ButtonHandler extends Handler {
149 // Button clicks have Message.what as the BUTTON{1,2,3} constant
150 private static final int MSG_DISMISS_DIALOG = 1;
152 private WeakReference<DialogInterface> mDialog;
154 public ButtonHandler(DialogInterface dialog) {
155 mDialog = new WeakReference<>(dialog);
159 public void handleMessage(Message msg) {
162 case DialogInterface.BUTTON_POSITIVE:
163 case DialogInterface.BUTTON_NEGATIVE:
164 case DialogInterface.BUTTON_NEUTRAL:
165 ((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
168 case MSG_DISMISS_DIALOG:
169 ((DialogInterface) msg.obj).dismiss();
174 private static boolean shouldCenterSingleButton(Context context) {
175 final TypedValue outValue = new TypedValue();
176 context.getTheme().resolveAttribute(R.attr.alertDialogCenterButtons, outValue, true);
177 return outValue.data != 0;
180 public static final AlertController create(Context context, DialogInterface di, Window window) {
181 final TypedArray a = context.obtainStyledAttributes(
182 null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);
183 int controllerType = a.getInt(R.styleable.AlertDialog_controllerType, 0);
186 switch (controllerType) {
188 return new MicroAlertController(context, di, window);
190 return new AlertController(context, di, window);
194 protected AlertController(Context context, DialogInterface di, Window window) {
196 mDialogInterface = di;
198 mHandler = new ButtonHandler(di);
200 final TypedArray a = context.obtainStyledAttributes(null,
201 R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);
203 mAlertDialogLayout = a.getResourceId(
204 R.styleable.AlertDialog_layout, R.layout.alert_dialog);
205 mButtonPanelSideLayout = a.getResourceId(
206 R.styleable.AlertDialog_buttonPanelSideLayout, 0);
207 mListLayout = a.getResourceId(
208 R.styleable.AlertDialog_listLayout, R.layout.select_dialog);
210 mMultiChoiceItemLayout = a.getResourceId(
211 R.styleable.AlertDialog_multiChoiceItemLayout,
212 R.layout.select_dialog_multichoice);
213 mSingleChoiceItemLayout = a.getResourceId(
214 R.styleable.AlertDialog_singleChoiceItemLayout,
215 R.layout.select_dialog_singlechoice);
216 mListItemLayout = a.getResourceId(
217 R.styleable.AlertDialog_listItemLayout,
218 R.layout.select_dialog_item);
219 mShowTitle = a.getBoolean(R.styleable.AlertDialog_showTitle, true);
223 /* We use a custom title so never request a window title */
224 window.requestFeature(Window.FEATURE_NO_TITLE);
227 static boolean canTextInput(View v) {
228 if (v.onCheckIsTextEditor()) {
232 if (!(v instanceof ViewGroup)) {
236 ViewGroup vg = (ViewGroup)v;
237 int i = vg.getChildCount();
240 v = vg.getChildAt(i);
241 if (canTextInput(v)) {
249 public void installContent() {
250 int contentView = selectContentView();
251 mWindow.setContentView(contentView);
255 private int selectContentView() {
256 if (mButtonPanelSideLayout == 0) {
257 return mAlertDialogLayout;
259 if (mButtonPanelLayoutHint == AlertDialog.LAYOUT_HINT_SIDE) {
260 return mButtonPanelSideLayout;
262 // TODO: use layout hint side for long messages/lists
263 return mAlertDialogLayout;
266 public void setTitle(CharSequence title) {
268 if (mTitleView != null) {
269 mTitleView.setText(title);
274 * @see AlertDialog.Builder#setCustomTitle(View)
276 public void setCustomTitle(View customTitleView) {
277 mCustomTitleView = customTitleView;
280 public void setMessage(CharSequence message) {
282 if (mMessageView != null) {
283 mMessageView.setText(message);
288 * Set the view resource to display in the dialog.
290 public void setView(int layoutResId) {
292 mViewLayoutResId = layoutResId;
293 mViewSpacingSpecified = false;
297 * Set the view to display in the dialog.
299 public void setView(View view) {
301 mViewLayoutResId = 0;
302 mViewSpacingSpecified = false;
306 * Set the view to display in the dialog along with the spacing around that view
308 public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
309 int viewSpacingBottom) {
311 mViewLayoutResId = 0;
312 mViewSpacingSpecified = true;
313 mViewSpacingLeft = viewSpacingLeft;
314 mViewSpacingTop = viewSpacingTop;
315 mViewSpacingRight = viewSpacingRight;
316 mViewSpacingBottom = viewSpacingBottom;
320 * Sets a hint for the best button panel layout.
322 public void setButtonPanelLayoutHint(int layoutHint) {
323 mButtonPanelLayoutHint = layoutHint;
327 * Sets a click listener or a message to be sent when the button is clicked.
328 * You only need to pass one of {@code listener} or {@code msg}.
330 * @param whichButton Which button, can be one of
331 * {@link DialogInterface#BUTTON_POSITIVE},
332 * {@link DialogInterface#BUTTON_NEGATIVE}, or
333 * {@link DialogInterface#BUTTON_NEUTRAL}
334 * @param text The text to display in positive button.
335 * @param listener The {@link DialogInterface.OnClickListener} to use.
336 * @param msg The {@link Message} to be sent when clicked.
338 public void setButton(int whichButton, CharSequence text,
339 DialogInterface.OnClickListener listener, Message msg) {
341 if (msg == null && listener != null) {
342 msg = mHandler.obtainMessage(whichButton, listener);
345 switch (whichButton) {
347 case DialogInterface.BUTTON_POSITIVE:
348 mButtonPositiveText = text;
349 mButtonPositiveMessage = msg;
352 case DialogInterface.BUTTON_NEGATIVE:
353 mButtonNegativeText = text;
354 mButtonNegativeMessage = msg;
357 case DialogInterface.BUTTON_NEUTRAL:
358 mButtonNeutralText = text;
359 mButtonNeutralMessage = msg;
363 throw new IllegalArgumentException("Button does not exist");
368 * Specifies the icon to display next to the alert title.
370 * @param resId the resource identifier of the drawable to use as the icon,
373 public void setIcon(int resId) {
377 if (mIconView != null) {
379 mIconView.setVisibility(View.VISIBLE);
380 mIconView.setImageResource(mIconId);
382 mIconView.setVisibility(View.GONE);
388 * Specifies the icon to display next to the alert title.
390 * @param icon the drawable to use as the icon or null for no icon
392 public void setIcon(Drawable icon) {
396 if (mIconView != null) {
398 mIconView.setVisibility(View.VISIBLE);
399 mIconView.setImageDrawable(icon);
401 mIconView.setVisibility(View.GONE);
407 * @param attrId the attributeId of the theme-specific drawable
408 * to resolve the resourceId for.
410 * @return resId the resourceId of the theme-specific drawable
412 public int getIconAttributeResId(int attrId) {
413 TypedValue out = new TypedValue();
414 mContext.getTheme().resolveAttribute(attrId, out, true);
415 return out.resourceId;
418 public void setInverseBackgroundForced(boolean forceInverseBackground) {
419 mForceInverseBackground = forceInverseBackground;
422 public ListView getListView() {
426 public Button getButton(int whichButton) {
427 switch (whichButton) {
428 case DialogInterface.BUTTON_POSITIVE:
429 return mButtonPositive;
430 case DialogInterface.BUTTON_NEGATIVE:
431 return mButtonNegative;
432 case DialogInterface.BUTTON_NEUTRAL:
433 return mButtonNeutral;
439 @SuppressWarnings({"UnusedDeclaration"})
440 public boolean onKeyDown(int keyCode, KeyEvent event) {
441 return mScrollView != null && mScrollView.executeKeyEvent(event);
444 @SuppressWarnings({"UnusedDeclaration"})
445 public boolean onKeyUp(int keyCode, KeyEvent event) {
446 return mScrollView != null && mScrollView.executeKeyEvent(event);
450 * Resolves whether a custom or default panel should be used. Removes the
451 * default panel if a custom panel should be used. If the resolved panel is
452 * a view stub, inflates before returning.
454 * @param customPanel the custom panel
455 * @param defaultPanel the default panel
456 * @return the panel to use
459 private ViewGroup resolvePanel(@Nullable View customPanel, @Nullable View defaultPanel) {
460 if (customPanel == null) {
461 // Inflate the default panel, if needed.
462 if (defaultPanel instanceof ViewStub) {
463 defaultPanel = ((ViewStub) defaultPanel).inflate();
466 return (ViewGroup) defaultPanel;
469 // Remove the default panel entirely.
470 if (defaultPanel != null) {
471 final ViewParent parent = defaultPanel.getParent();
472 if (parent instanceof ViewGroup) {
473 ((ViewGroup) parent).removeView(defaultPanel);
477 // Inflate the custom panel, if needed.
478 if (customPanel instanceof ViewStub) {
479 customPanel = ((ViewStub) customPanel).inflate();
482 return (ViewGroup) customPanel;
485 private void setupView() {
486 final View parentPanel = mWindow.findViewById(R.id.parentPanel);
487 final View defaultTopPanel = parentPanel.findViewById(R.id.topPanel);
488 final View defaultContentPanel = parentPanel.findViewById(R.id.contentPanel);
489 final View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);
491 // Install custom content before setting up the title or buttons so
492 // that we can handle panel overrides.
493 final ViewGroup customPanel = (ViewGroup) parentPanel.findViewById(R.id.customPanel);
494 setupCustomContent(customPanel);
496 final View customTopPanel = customPanel.findViewById(R.id.topPanel);
497 final View customContentPanel = customPanel.findViewById(R.id.contentPanel);
498 final View customButtonPanel = customPanel.findViewById(R.id.buttonPanel);
500 // Resolve the correct panels and remove the defaults, if needed.
501 final ViewGroup topPanel = resolvePanel(customTopPanel, defaultTopPanel);
502 final ViewGroup contentPanel = resolvePanel(customContentPanel, defaultContentPanel);
503 final ViewGroup buttonPanel = resolvePanel(customButtonPanel, defaultButtonPanel);
505 setupContent(contentPanel);
506 setupButtons(buttonPanel);
507 setupTitle(topPanel);
509 final boolean hasCustomPanel = customPanel != null
510 && customPanel.getVisibility() != View.GONE;
511 final boolean hasTopPanel = topPanel != null
512 && topPanel.getVisibility() != View.GONE;
513 final boolean hasButtonPanel = buttonPanel != null
514 && buttonPanel.getVisibility() != View.GONE;
516 // Only display the text spacer if we don't have buttons.
517 if (!hasButtonPanel) {
518 if (contentPanel != null) {
519 final View spacer = contentPanel.findViewById(R.id.textSpacerNoButtons);
520 if (spacer != null) {
521 spacer.setVisibility(View.VISIBLE);
524 mWindow.setCloseOnTouchOutsideIfNotSet(true);
528 // Only clip scrolling content to padding if we have a title.
529 if (mScrollView != null) {
530 mScrollView.setClipToPadding(true);
533 // Only show the divider if we have a title.
535 if (mMessage != null || mListView != null || hasCustomPanel) {
536 if (!hasCustomPanel) {
537 divider = topPanel.findViewById(R.id.titleDividerNoCustom);
539 if (divider == null) {
540 divider = topPanel.findViewById(R.id.titleDivider);
544 divider = topPanel.findViewById(R.id.titleDividerTop);
547 if (divider != null) {
548 divider.setVisibility(View.VISIBLE);
551 if (contentPanel != null) {
552 final View spacer = contentPanel.findViewById(R.id.textSpacerNoTitle);
553 if (spacer != null) {
554 spacer.setVisibility(View.VISIBLE);
559 if (mListView instanceof RecycleListView) {
560 ((RecycleListView) mListView).setHasDecor(hasTopPanel, hasButtonPanel);
563 // Update scroll indicators as needed.
564 if (!hasCustomPanel) {
565 final View content = mListView != null ? mListView : mScrollView;
566 if (content != null) {
567 final int indicators = (hasTopPanel ? View.SCROLL_INDICATOR_TOP : 0)
568 | (hasButtonPanel ? View.SCROLL_INDICATOR_BOTTOM : 0);
569 content.setScrollIndicators(indicators,
570 View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM);
574 final TypedArray a = mContext.obtainStyledAttributes(
575 null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);
576 setBackground(a, topPanel, contentPanel, customPanel, buttonPanel,
577 hasTopPanel, hasCustomPanel, hasButtonPanel);
581 private void setupCustomContent(ViewGroup customPanel) {
582 final View customView;
585 } else if (mViewLayoutResId != 0) {
586 final LayoutInflater inflater = LayoutInflater.from(mContext);
587 customView = inflater.inflate(mViewLayoutResId, customPanel, false);
592 final boolean hasCustomView = customView != null;
593 if (!hasCustomView || !canTextInput(customView)) {
594 mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
595 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
599 final FrameLayout custom = (FrameLayout) mWindow.findViewById(R.id.custom);
600 custom.addView(customView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
602 if (mViewSpacingSpecified) {
604 mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, mViewSpacingBottom);
607 if (mListView != null) {
608 ((LinearLayout.LayoutParams) customPanel.getLayoutParams()).weight = 0;
611 customPanel.setVisibility(View.GONE);
615 private void setupTitle(ViewGroup topPanel) {
616 if (mCustomTitleView != null && mShowTitle) {
617 // Add the custom title view directly to the topPanel layout
618 final LayoutParams lp = new LayoutParams(
619 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
621 topPanel.addView(mCustomTitleView, 0, lp);
623 // Hide the title template
624 final View titleTemplate = mWindow.findViewById(R.id.title_template);
625 titleTemplate.setVisibility(View.GONE);
627 mIconView = (ImageView) mWindow.findViewById(R.id.icon);
629 final boolean hasTextTitle = !TextUtils.isEmpty(mTitle);
630 if (hasTextTitle && mShowTitle) {
631 // Display the title if a title is supplied, else hide it.
632 mTitleView = (TextView) mWindow.findViewById(R.id.alertTitle);
633 mTitleView.setText(mTitle);
635 // Do this last so that if the user has supplied any icons we
636 // use them instead of the default ones. If the user has
637 // specified 0 then make it disappear.
639 mIconView.setImageResource(mIconId);
640 } else if (mIcon != null) {
641 mIconView.setImageDrawable(mIcon);
643 // Apply the padding from the icon to ensure the title is
644 // aligned correctly.
645 mTitleView.setPadding(mIconView.getPaddingLeft(),
646 mIconView.getPaddingTop(),
647 mIconView.getPaddingRight(),
648 mIconView.getPaddingBottom());
649 mIconView.setVisibility(View.GONE);
652 // Hide the title template
653 final View titleTemplate = mWindow.findViewById(R.id.title_template);
654 titleTemplate.setVisibility(View.GONE);
655 mIconView.setVisibility(View.GONE);
656 topPanel.setVisibility(View.GONE);
661 protected void setupContent(ViewGroup contentPanel) {
662 mScrollView = (ScrollView) contentPanel.findViewById(R.id.scrollView);
663 mScrollView.setFocusable(false);
665 // Special case for users that only want to display a String
666 mMessageView = (TextView) contentPanel.findViewById(R.id.message);
667 if (mMessageView == null) {
671 if (mMessage != null) {
672 mMessageView.setText(mMessage);
674 mMessageView.setVisibility(View.GONE);
675 mScrollView.removeView(mMessageView);
677 if (mListView != null) {
678 final ViewGroup scrollParent = (ViewGroup) mScrollView.getParent();
679 final int childIndex = scrollParent.indexOfChild(mScrollView);
680 scrollParent.removeViewAt(childIndex);
681 scrollParent.addView(mListView, childIndex,
682 new LayoutParams(MATCH_PARENT, MATCH_PARENT));
684 contentPanel.setVisibility(View.GONE);
689 private static void manageScrollIndicators(View v, View upIndicator, View downIndicator) {
690 if (upIndicator != null) {
691 upIndicator.setVisibility(v.canScrollVertically(-1) ? View.VISIBLE : View.INVISIBLE);
693 if (downIndicator != null) {
694 downIndicator.setVisibility(v.canScrollVertically(1) ? View.VISIBLE : View.INVISIBLE);
698 private void setupButtons(ViewGroup buttonPanel) {
699 int BIT_BUTTON_POSITIVE = 1;
700 int BIT_BUTTON_NEGATIVE = 2;
701 int BIT_BUTTON_NEUTRAL = 4;
702 int whichButtons = 0;
703 mButtonPositive = (Button) buttonPanel.findViewById(R.id.button1);
704 mButtonPositive.setOnClickListener(mButtonHandler);
706 if (TextUtils.isEmpty(mButtonPositiveText)) {
707 mButtonPositive.setVisibility(View.GONE);
709 mButtonPositive.setText(mButtonPositiveText);
710 mButtonPositive.setVisibility(View.VISIBLE);
711 whichButtons = whichButtons | BIT_BUTTON_POSITIVE;
714 mButtonNegative = (Button) buttonPanel.findViewById(R.id.button2);
715 mButtonNegative.setOnClickListener(mButtonHandler);
717 if (TextUtils.isEmpty(mButtonNegativeText)) {
718 mButtonNegative.setVisibility(View.GONE);
720 mButtonNegative.setText(mButtonNegativeText);
721 mButtonNegative.setVisibility(View.VISIBLE);
723 whichButtons = whichButtons | BIT_BUTTON_NEGATIVE;
726 mButtonNeutral = (Button) buttonPanel.findViewById(R.id.button3);
727 mButtonNeutral.setOnClickListener(mButtonHandler);
729 if (TextUtils.isEmpty(mButtonNeutralText)) {
730 mButtonNeutral.setVisibility(View.GONE);
732 mButtonNeutral.setText(mButtonNeutralText);
733 mButtonNeutral.setVisibility(View.VISIBLE);
735 whichButtons = whichButtons | BIT_BUTTON_NEUTRAL;
738 if (shouldCenterSingleButton(mContext)) {
740 * If we only have 1 button it should be centered on the layout and
741 * expand to fill 50% of the available space.
743 if (whichButtons == BIT_BUTTON_POSITIVE) {
744 centerButton(mButtonPositive);
745 } else if (whichButtons == BIT_BUTTON_NEGATIVE) {
746 centerButton(mButtonNegative);
747 } else if (whichButtons == BIT_BUTTON_NEUTRAL) {
748 centerButton(mButtonNeutral);
752 final boolean hasButtons = whichButtons != 0;
754 buttonPanel.setVisibility(View.GONE);
758 private void centerButton(Button button) {
759 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
760 params.gravity = Gravity.CENTER_HORIZONTAL;
761 params.weight = 0.5f;
762 button.setLayoutParams(params);
763 View leftSpacer = mWindow.findViewById(R.id.leftSpacer);
764 if (leftSpacer != null) {
765 leftSpacer.setVisibility(View.VISIBLE);
767 View rightSpacer = mWindow.findViewById(R.id.rightSpacer);
768 if (rightSpacer != null) {
769 rightSpacer.setVisibility(View.VISIBLE);
773 private void setBackground(TypedArray a, View topPanel, View contentPanel, View customPanel,
774 View buttonPanel, boolean hasTitle, boolean hasCustomView, boolean hasButtons) {
781 int centerBright = 0;
782 int bottomBright = 0;
783 int bottomMedium = 0;
785 // If the needsDefaultBackgrounds attribute is set, we know we're
786 // inheriting from a framework style.
787 final boolean needsDefaultBackgrounds = a.getBoolean(
788 R.styleable.AlertDialog_needsDefaultBackgrounds, true);
789 if (needsDefaultBackgrounds) {
790 fullDark = R.drawable.popup_full_dark;
791 topDark = R.drawable.popup_top_dark;
792 centerDark = R.drawable.popup_center_dark;
793 bottomDark = R.drawable.popup_bottom_dark;
794 fullBright = R.drawable.popup_full_bright;
795 topBright = R.drawable.popup_top_bright;
796 centerBright = R.drawable.popup_center_bright;
797 bottomBright = R.drawable.popup_bottom_bright;
798 bottomMedium = R.drawable.popup_bottom_medium;
801 topBright = a.getResourceId(R.styleable.AlertDialog_topBright, topBright);
802 topDark = a.getResourceId(R.styleable.AlertDialog_topDark, topDark);
803 centerBright = a.getResourceId(R.styleable.AlertDialog_centerBright, centerBright);
804 centerDark = a.getResourceId(R.styleable.AlertDialog_centerDark, centerDark);
806 /* We now set the background of all of the sections of the alert.
807 * First collect together each section that is being displayed along
808 * with whether it is on a light or dark background, then run through
809 * them setting their backgrounds. This is complicated because we need
810 * to correctly use the full, top, middle, and bottom graphics depending
811 * on how many views they are and where they appear.
814 final View[] views = new View[4];
815 final boolean[] light = new boolean[4];
816 View lastView = null;
817 boolean lastLight = false;
821 views[pos] = topPanel;
826 /* The contentPanel displays either a custom text message or
827 * a ListView. If it's text we should use the dark background
828 * for ListView we should use the light background. If neither
829 * are there the contentPanel will be hidden so set it as null.
831 views[pos] = contentPanel.getVisibility() == View.GONE ? null : contentPanel;
832 light[pos] = mListView != null;
836 views[pos] = customPanel;
837 light[pos] = mForceInverseBackground;
842 views[pos] = buttonPanel;
846 boolean setView = false;
847 for (pos = 0; pos < views.length; pos++) {
848 final View v = views[pos];
853 if (lastView != null) {
855 lastView.setBackgroundResource(lastLight ? topBright : topDark);
857 lastView.setBackgroundResource(lastLight ? centerBright : centerDark);
863 lastLight = light[pos];
866 if (lastView != null) {
868 bottomBright = a.getResourceId(R.styleable.AlertDialog_bottomBright, bottomBright);
869 bottomMedium = a.getResourceId(R.styleable.AlertDialog_bottomMedium, bottomMedium);
870 bottomDark = a.getResourceId(R.styleable.AlertDialog_bottomDark, bottomDark);
872 // ListViews will use the Bright background, but buttons use the
873 // Medium background.
874 lastView.setBackgroundResource(
875 lastLight ? (hasButtons ? bottomMedium : bottomBright) : bottomDark);
877 fullBright = a.getResourceId(R.styleable.AlertDialog_fullBright, fullBright);
878 fullDark = a.getResourceId(R.styleable.AlertDialog_fullDark, fullDark);
880 lastView.setBackgroundResource(lastLight ? fullBright : fullDark);
884 final ListView listView = mListView;
885 if (listView != null && mAdapter != null) {
886 listView.setAdapter(mAdapter);
887 final int checkedItem = mCheckedItem;
888 if (checkedItem > -1) {
889 listView.setItemChecked(checkedItem + listView.getHeaderViewsCount(), true);
890 listView.setSelection(checkedItem + listView.getHeaderViewsCount());
895 public static class RecycleListView extends ListView {
896 private final int mPaddingTopNoTitle;
897 private final int mPaddingBottomNoButtons;
899 boolean mRecycleOnMeasure = true;
901 public RecycleListView(Context context) {
905 public RecycleListView(Context context, AttributeSet attrs) {
906 super(context, attrs);
908 final TypedArray ta = context.obtainStyledAttributes(
909 attrs, R.styleable.RecycleListView);
910 mPaddingBottomNoButtons = ta.getDimensionPixelOffset(
911 R.styleable.RecycleListView_paddingBottomNoButtons, -1);
912 mPaddingTopNoTitle = ta.getDimensionPixelOffset(
913 R.styleable.RecycleListView_paddingTopNoTitle, -1);
916 public void setHasDecor(boolean hasTitle, boolean hasButtons) {
917 if (!hasButtons || !hasTitle) {
918 final int paddingLeft = getPaddingLeft();
919 final int paddingTop = hasTitle ? getPaddingTop() : mPaddingTopNoTitle;
920 final int paddingRight = getPaddingRight();
921 final int paddingBottom = hasButtons ? getPaddingBottom() : mPaddingBottomNoButtons;
922 setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
927 protected boolean recycleOnMeasure() {
928 return mRecycleOnMeasure;
932 public static class AlertParams {
933 public final Context mContext;
934 public final LayoutInflater mInflater;
936 public int mIconId = 0;
937 public Drawable mIcon;
938 public int mIconAttrId = 0;
939 public CharSequence mTitle;
940 public View mCustomTitleView;
941 public CharSequence mMessage;
942 public CharSequence mPositiveButtonText;
943 public DialogInterface.OnClickListener mPositiveButtonListener;
944 public CharSequence mNegativeButtonText;
945 public DialogInterface.OnClickListener mNegativeButtonListener;
946 public CharSequence mNeutralButtonText;
947 public DialogInterface.OnClickListener mNeutralButtonListener;
948 public boolean mCancelable;
949 public DialogInterface.OnCancelListener mOnCancelListener;
950 public DialogInterface.OnDismissListener mOnDismissListener;
951 public DialogInterface.OnKeyListener mOnKeyListener;
952 public CharSequence[] mItems;
953 public ListAdapter mAdapter;
954 public DialogInterface.OnClickListener mOnClickListener;
955 public int mViewLayoutResId;
957 public int mViewSpacingLeft;
958 public int mViewSpacingTop;
959 public int mViewSpacingRight;
960 public int mViewSpacingBottom;
961 public boolean mViewSpacingSpecified = false;
962 public boolean[] mCheckedItems;
963 public boolean mIsMultiChoice;
964 public boolean mIsSingleChoice;
965 public int mCheckedItem = -1;
966 public DialogInterface.OnMultiChoiceClickListener mOnCheckboxClickListener;
967 public Cursor mCursor;
968 public String mLabelColumn;
969 public String mIsCheckedColumn;
970 public boolean mForceInverseBackground;
971 public AdapterView.OnItemSelectedListener mOnItemSelectedListener;
972 public OnPrepareListViewListener mOnPrepareListViewListener;
973 public boolean mRecycleOnMeasure = true;
976 * Interface definition for a callback to be invoked before the ListView
977 * will be bound to an adapter.
979 public interface OnPrepareListViewListener {
982 * Called before the ListView is bound to an adapter.
983 * @param listView The ListView that will be shown in the dialog.
985 void onPrepareListView(ListView listView);
988 public AlertParams(Context context) {
991 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
994 public void apply(AlertController dialog) {
995 if (mCustomTitleView != null) {
996 dialog.setCustomTitle(mCustomTitleView);
998 if (mTitle != null) {
999 dialog.setTitle(mTitle);
1001 if (mIcon != null) {
1002 dialog.setIcon(mIcon);
1005 dialog.setIcon(mIconId);
1007 if (mIconAttrId != 0) {
1008 dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
1011 if (mMessage != null) {
1012 dialog.setMessage(mMessage);
1014 if (mPositiveButtonText != null) {
1015 dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
1016 mPositiveButtonListener, null);
1018 if (mNegativeButtonText != null) {
1019 dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
1020 mNegativeButtonListener, null);
1022 if (mNeutralButtonText != null) {
1023 dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,
1024 mNeutralButtonListener, null);
1026 if (mForceInverseBackground) {
1027 dialog.setInverseBackgroundForced(true);
1029 // For a list, the client can either supply an array of items or an
1030 // adapter or a cursor
1031 if ((mItems != null) || (mCursor != null) || (mAdapter != null)) {
1032 createListView(dialog);
1034 if (mView != null) {
1035 if (mViewSpacingSpecified) {
1036 dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,
1037 mViewSpacingBottom);
1039 dialog.setView(mView);
1041 } else if (mViewLayoutResId != 0) {
1042 dialog.setView(mViewLayoutResId);
1046 dialog.setCancelable(mCancelable);
1047 dialog.setOnCancelListener(mOnCancelListener);
1048 if (mOnKeyListener != null) {
1049 dialog.setOnKeyListener(mOnKeyListener);
1054 private void createListView(final AlertController dialog) {
1055 final RecycleListView listView =
1056 (RecycleListView) mInflater.inflate(dialog.mListLayout, null);
1057 final ListAdapter adapter;
1059 if (mIsMultiChoice) {
1060 if (mCursor == null) {
1061 adapter = new ArrayAdapter<CharSequence>(
1062 mContext, dialog.mMultiChoiceItemLayout, R.id.text1, mItems) {
1064 public View getView(int position, View convertView, ViewGroup parent) {
1065 View view = super.getView(position, convertView, parent);
1066 if (mCheckedItems != null) {
1067 boolean isItemChecked = mCheckedItems[position];
1068 if (isItemChecked) {
1069 listView.setItemChecked(
1070 position + listView.getHeaderViewsCount(), true);
1077 adapter = new CursorAdapter(mContext, mCursor, false) {
1078 private final int mLabelIndex;
1079 private final int mIsCheckedIndex;
1082 final Cursor cursor = getCursor();
1083 mLabelIndex = cursor.getColumnIndexOrThrow(mLabelColumn);
1084 mIsCheckedIndex = cursor.getColumnIndexOrThrow(mIsCheckedColumn);
1088 public void bindView(View view, Context context, Cursor cursor) {
1089 CheckedTextView text = (CheckedTextView) view.findViewById(R.id.text1);
1090 text.setText(cursor.getString(mLabelIndex));
1091 listView.setItemChecked(
1092 cursor.getPosition() + listView.getHeaderViewsCount(),
1093 cursor.getInt(mIsCheckedIndex) == 1);
1097 public View newView(Context context, Cursor cursor, ViewGroup parent) {
1098 return mInflater.inflate(dialog.mMultiChoiceItemLayout,
1106 if (mIsSingleChoice) {
1107 layout = dialog.mSingleChoiceItemLayout;
1109 layout = dialog.mListItemLayout;
1112 if (mCursor != null) {
1113 adapter = new SimpleCursorAdapter(mContext, layout, mCursor,
1114 new String[] { mLabelColumn }, new int[] { R.id.text1 });
1115 } else if (mAdapter != null) {
1118 adapter = new CheckedItemAdapter(mContext, layout, R.id.text1, mItems);
1122 if (mOnPrepareListViewListener != null) {
1123 mOnPrepareListViewListener.onPrepareListView(listView);
1126 /* Don't directly set the adapter on the ListView as we might
1127 * want to add a footer to the ListView later.
1129 dialog.mAdapter = adapter;
1130 dialog.mCheckedItem = mCheckedItem;
1132 if (mOnClickListener != null) {
1133 listView.setOnItemClickListener(new OnItemClickListener() {
1135 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
1136 position -= listView.getHeaderViewsCount();
1137 mOnClickListener.onClick(dialog.mDialogInterface, position);
1138 if (!mIsSingleChoice) {
1139 dialog.mDialogInterface.dismiss();
1143 } else if (mOnCheckboxClickListener != null) {
1144 listView.setOnItemClickListener(new OnItemClickListener() {
1146 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
1147 position -= listView.getHeaderViewsCount();
1148 if (mCheckedItems != null) {
1149 mCheckedItems[position] = listView.isItemChecked(position);
1151 mOnCheckboxClickListener.onClick(
1152 dialog.mDialogInterface, position, listView.isItemChecked(position));
1157 // Attach a given OnItemSelectedListener to the ListView
1158 if (mOnItemSelectedListener != null) {
1159 listView.setOnItemSelectedListener(mOnItemSelectedListener);
1162 if (mIsSingleChoice) {
1163 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
1164 } else if (mIsMultiChoice) {
1165 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
1167 listView.mRecycleOnMeasure = mRecycleOnMeasure;
1168 dialog.mListView = listView;
1172 private static class CheckedItemAdapter extends ArrayAdapter<CharSequence> {
1173 public CheckedItemAdapter(Context context, int resource, int textViewResourceId,
1174 CharSequence[] objects) {
1175 super(context, resource, textViewResourceId, objects);
1179 public boolean hasStableIds() {
1184 public long getItemId(int position) {