[automerger] Add an API to AlertDialog to enable button images. am: 70a61be63f
Change-Id: I10a4ca44a0dd6dc6a2ad2164b9102d77ea79fe22
diff --git a/samples/SupportWearDemos/src/main/java/com/example/android/support/wear/app/AlertDialogDemo.java b/samples/SupportWearDemos/src/main/java/com/example/android/support/wear/app/AlertDialogDemo.java
index 4ea448a..35a32ce 100644
--- a/samples/SupportWearDemos/src/main/java/com/example/android/support/wear/app/AlertDialogDemo.java
+++ b/samples/SupportWearDemos/src/main/java/com/example/android/support/wear/app/AlertDialogDemo.java
@@ -17,6 +17,7 @@
package com.example.android.support.wear.app;
import android.app.Activity;
+import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
@@ -55,10 +56,12 @@
}
private AlertDialog createV7Dialog() {
+ Drawable drawable = getDrawable(R.drawable.app_sample_code);
return new AlertDialog.Builder(this)
.setTitle("AppCompatDialog")
.setMessage("Lorem ipsum dolor...")
.setPositiveButton("Ok", null)
+ .setPositiveButtonIcon(drawable)
.setNegativeButton("Cancel", null)
.create();
}
diff --git a/samples/SupportWearDemos/src/main/res/layout/alert_dialog_demo.xml b/samples/SupportWearDemos/src/main/res/layout/alert_dialog_demo.xml
index 833d489..5df4f4c 100644
--- a/samples/SupportWearDemos/src/main/res/layout/alert_dialog_demo.xml
+++ b/samples/SupportWearDemos/src/main/res/layout/alert_dialog_demo.xml
@@ -41,4 +41,4 @@
android:text="Show Framework dialog"/>
</LinearLayout>
-</android.support.wear.widget.BoxInsetLayout>
\ No newline at end of file
+</android.support.wear.widget.BoxInsetLayout>
diff --git a/v7/appcompat/api/current.txt b/v7/appcompat/api/current.txt
index 892ff0b..5d4fa6e 100644
--- a/v7/appcompat/api/current.txt
+++ b/v7/appcompat/api/current.txt
@@ -161,6 +161,7 @@
method public android.widget.ListView getListView();
method public void setButton(int, java.lang.CharSequence, android.os.Message);
method public void setButton(int, java.lang.CharSequence, android.content.DialogInterface.OnClickListener);
+ method public void setButton(int, java.lang.CharSequence, android.graphics.drawable.Drawable, android.content.DialogInterface.OnClickListener);
method public void setCustomTitle(android.view.View);
method public void setIcon(int);
method public void setIcon(android.graphics.drawable.Drawable);
@@ -192,14 +193,17 @@
method public android.support.v7.app.AlertDialog.Builder setMultiChoiceItems(android.database.Cursor, java.lang.String, java.lang.String, android.content.DialogInterface.OnMultiChoiceClickListener);
method public android.support.v7.app.AlertDialog.Builder setNegativeButton(int, android.content.DialogInterface.OnClickListener);
method public android.support.v7.app.AlertDialog.Builder setNegativeButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener);
+ method public android.support.v7.app.AlertDialog.Builder setNegativeButtonIcon(android.graphics.drawable.Drawable);
method public android.support.v7.app.AlertDialog.Builder setNeutralButton(int, android.content.DialogInterface.OnClickListener);
method public android.support.v7.app.AlertDialog.Builder setNeutralButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener);
+ method public android.support.v7.app.AlertDialog.Builder setNeutralButtonIcon(android.graphics.drawable.Drawable);
method public android.support.v7.app.AlertDialog.Builder setOnCancelListener(android.content.DialogInterface.OnCancelListener);
method public android.support.v7.app.AlertDialog.Builder setOnDismissListener(android.content.DialogInterface.OnDismissListener);
method public android.support.v7.app.AlertDialog.Builder setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener);
method public android.support.v7.app.AlertDialog.Builder setOnKeyListener(android.content.DialogInterface.OnKeyListener);
method public android.support.v7.app.AlertDialog.Builder setPositiveButton(int, android.content.DialogInterface.OnClickListener);
method public android.support.v7.app.AlertDialog.Builder setPositiveButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener);
+ method public android.support.v7.app.AlertDialog.Builder setPositiveButtonIcon(android.graphics.drawable.Drawable);
method public android.support.v7.app.AlertDialog.Builder setSingleChoiceItems(int, int, android.content.DialogInterface.OnClickListener);
method public android.support.v7.app.AlertDialog.Builder setSingleChoiceItems(android.database.Cursor, int, java.lang.String, android.content.DialogInterface.OnClickListener);
method public android.support.v7.app.AlertDialog.Builder setSingleChoiceItems(java.lang.CharSequence[], int, android.content.DialogInterface.OnClickListener);
diff --git a/v7/appcompat/res/values/attrs.xml b/v7/appcompat/res/values/attrs.xml
index 485d92b..7d26a32 100644
--- a/v7/appcompat/res/values/attrs.xml
+++ b/v7/appcompat/res/values/attrs.xml
@@ -1142,6 +1142,7 @@
<attr name="singleChoiceItemLayout" format="reference" />
<attr name="listItemLayout" format="reference" />
<attr name="showTitle" format="boolean" />
+ <attr name="buttonIconDimen" format="dimension" />
</declare-styleable>
<!-- @hide -->
diff --git a/v7/appcompat/res/values/dimens.xml b/v7/appcompat/res/values/dimens.xml
index 6dddc62..6357caa 100644
--- a/v7/appcompat/res/values/dimens.xml
+++ b/v7/appcompat/res/values/dimens.xml
@@ -96,6 +96,9 @@
<!-- Minimum "smallest width" of the display for cascading menus to be enabled. -->
<dimen name="abc_cascading_menus_min_smallest_width">720dp</dimen>
+ <!-- Dimension of the AlertDialog button icon -->
+ <dimen name="abc_alert_dialog_button_dimen">48dp</dimen>
+
<!-- Tooltip dimensions. -->
<!-- Vertical offset from the edge of the anchor view for a touch-triggered tooltip. -->
<dimen name="tooltip_y_offset_touch">16dp</dimen>
diff --git a/v7/appcompat/res/values/styles_base.xml b/v7/appcompat/res/values/styles_base.xml
index adaaae0..c1aa3a7 100644
--- a/v7/appcompat/res/values/styles_base.xml
+++ b/v7/appcompat/res/values/styles_base.xml
@@ -520,6 +520,7 @@
<item name="listItemLayout">@layout/select_dialog_item_material</item>
<item name="multiChoiceItemLayout">@layout/select_dialog_multichoice_material</item>
<item name="singleChoiceItemLayout">@layout/select_dialog_singlechoice_material</item>
+ <item name="buttonIconDimen">@dimen/abc_alert_dialog_button_dimen</item>
</style>
<style name="Base.AlertDialog.AppCompat.Light" parent="Base.AlertDialog.AppCompat" />
diff --git a/v7/appcompat/src/main/java/android/support/v7/app/AlertController.java b/v7/appcompat/src/main/java/android/support/v7/app/AlertController.java
index 5ff4537..01bc449 100644
--- a/v7/appcompat/src/main/java/android/support/v7/app/AlertController.java
+++ b/v7/appcompat/src/main/java/android/support/v7/app/AlertController.java
@@ -65,6 +65,7 @@
private final Context mContext;
final AppCompatDialog mDialog;
private final Window mWindow;
+ private final int mButtonIconDimen;
private CharSequence mTitle;
private CharSequence mMessage;
@@ -82,14 +83,17 @@
Button mButtonPositive;
private CharSequence mButtonPositiveText;
Message mButtonPositiveMessage;
+ private Drawable mButtonPositiveIcon;
Button mButtonNegative;
private CharSequence mButtonNegativeText;
Message mButtonNegativeMessage;
+ private Drawable mButtonNegativeIcon;
Button mButtonNeutral;
private CharSequence mButtonNeutralText;
Message mButtonNeutralMessage;
+ private Drawable mButtonNeutralIcon;
NestedScrollView mScrollView;
@@ -192,6 +196,7 @@
.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);
mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);
mShowTitle = a.getBoolean(R.styleable.AlertDialog_showTitle, true);
+ mButtonIconDimen = a.getDimensionPixelSize(R.styleable.AlertDialog_buttonIconDimen, 0);
a.recycle();
@@ -298,8 +303,8 @@
}
/**
- * Sets a click listener or a message to be sent when the button is clicked.
- * You only need to pass one of {@code listener} or {@code msg}.
+ * Sets an icon, a click listener or a message to be sent when the button is clicked.
+ * You only need to pass one of {@code icon}, {@code listener} or {@code msg}.
*
* @param whichButton Which button, can be one of
* {@link DialogInterface#BUTTON_POSITIVE},
@@ -308,9 +313,11 @@
* @param text The text to display in positive button.
* @param listener The {@link DialogInterface.OnClickListener} to use.
* @param msg The {@link Message} to be sent when clicked.
+ * @param icon The (@link Drawable) to be used as an icon for the button.
+ *
*/
public void setButton(int whichButton, CharSequence text,
- DialogInterface.OnClickListener listener, Message msg) {
+ DialogInterface.OnClickListener listener, Message msg, Drawable icon) {
if (msg == null && listener != null) {
msg = mHandler.obtainMessage(whichButton, listener);
@@ -321,16 +328,19 @@
case DialogInterface.BUTTON_POSITIVE:
mButtonPositiveText = text;
mButtonPositiveMessage = msg;
+ mButtonPositiveIcon = icon;
break;
case DialogInterface.BUTTON_NEGATIVE:
mButtonNegativeText = text;
mButtonNegativeMessage = msg;
+ mButtonNegativeIcon = icon;
break;
case DialogInterface.BUTTON_NEUTRAL:
mButtonNeutralText = text;
mButtonNeutralMessage = msg;
+ mButtonNeutralIcon = icon;
break;
default:
@@ -752,35 +762,45 @@
mButtonPositive = (Button) buttonPanel.findViewById(android.R.id.button1);
mButtonPositive.setOnClickListener(mButtonHandler);
- if (TextUtils.isEmpty(mButtonPositiveText)) {
+ if (TextUtils.isEmpty(mButtonPositiveText) && mButtonPositiveIcon == null) {
mButtonPositive.setVisibility(View.GONE);
} else {
mButtonPositive.setText(mButtonPositiveText);
+ if (mButtonPositiveIcon != null) {
+ mButtonPositiveIcon.setBounds(0, 0, mButtonIconDimen, mButtonIconDimen);
+ mButtonPositive.setCompoundDrawables(mButtonPositiveIcon, null, null, null);
+ }
mButtonPositive.setVisibility(View.VISIBLE);
whichButtons = whichButtons | BIT_BUTTON_POSITIVE;
}
- mButtonNegative = (Button) buttonPanel.findViewById(android.R.id.button2);
+ mButtonNegative = buttonPanel.findViewById(android.R.id.button2);
mButtonNegative.setOnClickListener(mButtonHandler);
- if (TextUtils.isEmpty(mButtonNegativeText)) {
+ if (TextUtils.isEmpty(mButtonNegativeText) && mButtonNegativeIcon == null) {
mButtonNegative.setVisibility(View.GONE);
} else {
mButtonNegative.setText(mButtonNegativeText);
+ if (mButtonNegativeIcon != null) {
+ mButtonNegativeIcon.setBounds(0, 0, mButtonIconDimen, mButtonIconDimen);
+ mButtonNegative.setCompoundDrawables(mButtonNegativeIcon, null, null, null);
+ }
mButtonNegative.setVisibility(View.VISIBLE);
-
whichButtons = whichButtons | BIT_BUTTON_NEGATIVE;
}
mButtonNeutral = (Button) buttonPanel.findViewById(android.R.id.button3);
mButtonNeutral.setOnClickListener(mButtonHandler);
- if (TextUtils.isEmpty(mButtonNeutralText)) {
+ if (TextUtils.isEmpty(mButtonNeutralText) && mButtonNeutralIcon == null) {
mButtonNeutral.setVisibility(View.GONE);
} else {
mButtonNeutral.setText(mButtonNeutralText);
+ if (mButtonPositiveIcon != null) {
+ mButtonPositiveIcon.setBounds(0, 0, mButtonIconDimen, mButtonIconDimen);
+ mButtonPositive.setCompoundDrawables(mButtonPositiveIcon, null, null, null);
+ }
mButtonNeutral.setVisibility(View.VISIBLE);
-
whichButtons = whichButtons | BIT_BUTTON_NEUTRAL;
}
@@ -852,10 +872,13 @@
public View mCustomTitleView;
public CharSequence mMessage;
public CharSequence mPositiveButtonText;
+ public Drawable mPositiveButtonIcon;
public DialogInterface.OnClickListener mPositiveButtonListener;
public CharSequence mNegativeButtonText;
+ public Drawable mNegativeButtonIcon;
public DialogInterface.OnClickListener mNegativeButtonListener;
public CharSequence mNeutralButtonText;
+ public Drawable mNeutralButtonIcon;
public DialogInterface.OnClickListener mNeutralButtonListener;
public boolean mCancelable;
public DialogInterface.OnCancelListener mOnCancelListener;
@@ -923,17 +946,17 @@
if (mMessage != null) {
dialog.setMessage(mMessage);
}
- if (mPositiveButtonText != null) {
+ if (mPositiveButtonText != null || mPositiveButtonIcon != null) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
- mPositiveButtonListener, null);
+ mPositiveButtonListener, null, mPositiveButtonIcon);
}
- if (mNegativeButtonText != null) {
+ if (mNegativeButtonText != null || mNegativeButtonIcon != null) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
- mNegativeButtonListener, null);
+ mNegativeButtonListener, null, mNegativeButtonIcon);
}
- if (mNeutralButtonText != null) {
+ if (mNeutralButtonText != null || mNeutralButtonIcon != null) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,
- mNeutralButtonListener, null);
+ mNeutralButtonListener, null, mNeutralButtonIcon);
}
// For a list, the client can either supply an array of items or an
// adapter or a cursor
diff --git a/v7/appcompat/src/main/java/android/support/v7/app/AlertDialog.java b/v7/appcompat/src/main/java/android/support/v7/app/AlertDialog.java
index 4b87dcc..1712f20 100644
--- a/v7/appcompat/src/main/java/android/support/v7/app/AlertDialog.java
+++ b/v7/appcompat/src/main/java/android/support/v7/app/AlertDialog.java
@@ -207,7 +207,7 @@
* @param msg The {@link Message} to be sent when clicked.
*/
public void setButton(int whichButton, CharSequence text, Message msg) {
- mAlert.setButton(whichButton, text, null, msg);
+ mAlert.setButton(whichButton, text, null, msg, null);
}
/**
@@ -222,7 +222,25 @@
* @param listener The {@link DialogInterface.OnClickListener} to use.
*/
public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
- mAlert.setButton(whichButton, text, listener, null);
+ mAlert.setButton(whichButton, text, listener, null, null);
+ }
+
+ /**
+ * Sets an icon to be displayed along with the button text and a listener to be invoked when
+ * the positive button of the dialog is pressed. This method has no effect if called after
+ * {@link #show()}.
+ *
+ * @param whichButton Which button to set the listener on, can be one of
+ * {@link DialogInterface#BUTTON_POSITIVE},
+ * {@link DialogInterface#BUTTON_NEGATIVE}, or
+ * {@link DialogInterface#BUTTON_NEUTRAL}
+ * @param text The text to display in positive button.
+ * @param listener The {@link DialogInterface.OnClickListener} to use.
+ * @param icon The {@link Drawable} to be set as an icon for the button.
+ */
+ public void setButton(int whichButton, CharSequence text, Drawable icon,
+ OnClickListener listener) {
+ mAlert.setButton(whichButton, text, listener, null, icon);
}
/**
@@ -470,6 +488,16 @@
}
/**
+ * Set an icon to be displayed for the positive button.
+ * @param icon The icon to be displayed
+ * @return This Builder object to allow for chaining of calls to set methods
+ */
+ public Builder setPositiveButtonIcon(Drawable icon) {
+ P.mPositiveButtonIcon = icon;
+ return this;
+ }
+
+ /**
* Set a listener to be invoked when the negative button of the dialog is pressed.
* @param textId The resource id of the text to display in the negative button
* @param listener The {@link DialogInterface.OnClickListener} to use.
@@ -496,6 +524,16 @@
}
/**
+ * Set an icon to be displayed for the negative button.
+ * @param icon The icon to be displayed
+ * @return This Builder object to allow for chaining of calls to set methods
+ */
+ public Builder setNegativeButtonIcon(Drawable icon) {
+ P.mNegativeButtonIcon = icon;
+ return this;
+ }
+
+ /**
* Set a listener to be invoked when the neutral button of the dialog is pressed.
* @param textId The resource id of the text to display in the neutral button
* @param listener The {@link DialogInterface.OnClickListener} to use.
@@ -522,6 +560,16 @@
}
/**
+ * Set an icon to be displayed for the neutral button.
+ * @param icon The icon to be displayed
+ * @return This Builder object to allow for chaining of calls to set methods
+ */
+ public Builder setNeutralButtonIcon(Drawable icon) {
+ P.mNeutralButtonIcon = icon;
+ return this;
+ }
+
+ /**
* Sets whether the dialog is cancelable or not. Default is true.
*
* @return This Builder object to allow for chaining of calls to set methods