Browse Source

#297 - Added option to limit dialog icon to a default max size (32dp) through builder method or through a global theme attribute. Added option to limit dialog to a specified max size through a builder method and a global theme attribute.

Andrew Lord 10 years ago
parent
commit
9c7e49bca8

+ 29 - 0
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -273,6 +273,23 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             }
             }
         }
         }
 
 
+        int maxIconSize = builder.maxIconSize;
+        if (maxIconSize == -1) {
+            maxIconSize = DialogUtils.resolveDimension(mBuilder.context, R.attr.md_icon_max_size);
+        }
+
+        boolean limitIconToDefaultSize = DialogUtils.resolveBoolean(mBuilder.context, R.attr.md_icon_limit_icon_to_default_size);
+        if (builder.limitIconToDefaultSize || limitIconToDefaultSize) {
+            maxIconSize = mBuilder.context.getResources().getDimensionPixelSize(R.dimen.md_icon_max_size);
+        }
+
+        if (maxIconSize > -1) {
+            icon.setAdjustViewBounds(true);
+            icon.setMaxHeight(maxIconSize);
+            icon.setMaxWidth(maxIconSize);
+            icon.requestLayout();
+        }
+
         if (builder.title == null) {
         if (builder.title == null) {
             titleFrame.setVisibility(View.GONE);
             titleFrame.setVisibility(View.GONE);
         } else {
         } else {
@@ -917,6 +934,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         protected Typeface mediumFont;
         protected Typeface mediumFont;
         protected boolean useCustomFonts;
         protected boolean useCustomFonts;
         protected Drawable icon;
         protected Drawable icon;
+        protected boolean limitIconToDefaultSize;
+        protected int maxIconSize = -1;
         protected ListAdapter adapter;
         protected ListAdapter adapter;
         protected OnDismissListener dismissListener;
         protected OnDismissListener dismissListener;
         protected OnCancelListener cancelListener;
         protected OnCancelListener cancelListener;
@@ -1460,6 +1479,16 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             return this;
             return this;
         }
         }
 
 
+        public Builder limitIconToDefaultSize() {
+            this.limitIconToDefaultSize = true;
+            return this;
+        }
+
+        public Builder maxIconSize(int maxIconSize) {
+            this.maxIconSize = maxIconSize;
+            return this;
+        }
+
         public Builder showListener(@NonNull OnShowListener listener) {
         public Builder showListener(@NonNull OnShowListener listener) {
             this.showListener = listener;
             this.showListener = listener;
             return this;
             return this;

+ 22 - 0
library/src/main/java/com/afollestad/materialdialogs/util/DialogUtils.java

@@ -47,4 +47,26 @@ public class DialogUtils {
             a.recycle();
             a.recycle();
         }
         }
     }
     }
+
+    public static int resolveDimension(Context context, @AttrRes int attr) {
+        return resolveDimension(context, attr, -1);
+    }
+
+    public static int resolveDimension(Context context, @AttrRes int attr, int fallback) {
+        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
+        try {
+            return a.getDimensionPixelSize(0, fallback);
+        } finally {
+            a.recycle();
+        }
+    }
+
+    public static boolean resolveBoolean(Context context, @AttrRes int attr) {
+        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
+        try {
+            return a.getBoolean(0, false);
+        } finally {
+            a.recycle();
+        }
+    }
 }
 }

+ 3 - 0
library/src/main/res/values/attrs.xml

@@ -9,6 +9,9 @@
     <attr name="md_background_color" format="color" />
     <attr name="md_background_color" format="color" />
 
 
     <attr name="md_icon" format="reference" />
     <attr name="md_icon" format="reference" />
+    <attr name="md_icon_max_size" format="dimension" />
+    <attr name="md_icon_limit_icon_to_default_size" format="boolean" />
+
     <attr name="md_title_color" format="color" />
     <attr name="md_title_color" format="color" />
     <attr name="md_content_color" format="color" />
     <attr name="md_content_color" format="color" />
 
 

+ 1 - 0
library/src/main/res/values/dimens.xml

@@ -33,6 +33,7 @@
     <dimen name="md_listitem_height">52dp</dimen>
     <dimen name="md_listitem_height">52dp</dimen>
     <dimen name="md_listitem_control_margin">16dp</dimen>
     <dimen name="md_listitem_control_margin">16dp</dimen>
     <dimen name="md_icon_margin">16dp</dimen>
     <dimen name="md_icon_margin">16dp</dimen>
+    <dimen name="md_icon_max_size">32dp</dimen>
     <dimen name="md_listitem_margin_left">24dp</dimen>
     <dimen name="md_listitem_margin_left">24dp</dimen>
     <dimen name="md_action_corner_radius">2dp</dimen>
     <dimen name="md_action_corner_radius">2dp</dimen>