Browse Source

When using system attributes for title and content color, it will automatically use inverse if needed (e.g. if dialog theme is light and activity theme is dark)

Aidan Follestad 10 years ago
parent
commit
8633fc1661

+ 20 - 1
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -2,7 +2,6 @@ package com.afollestad.materialdialogs;
 
 import android.content.res.Resources;
 import android.content.res.TypedArray;
-import android.graphics.Color;
 import android.graphics.PorterDuff;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
@@ -85,6 +84,26 @@ class DialogInit {
         builder.negativeColor = DialogUtils.resolveColor(builder.context, R.attr.md_negative_color, builder.negativeColor);
         builder.progressColor = DialogUtils.resolveColor(builder.context, R.attr.md_progress_color, builder.progressColor);
 
+        // Retrieve default title/content colors
+        if (!builder.titleColorSet) {
+            builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimary);
+            if (DialogUtils.isColorDark(builder.titleColor)) {
+                if (builder.theme == Theme.DARK)
+                    builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
+            } else if (builder.theme == Theme.LIGHT)
+                builder.titleColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorPrimaryInverse);
+        }
+        if (!builder.contentColorSet) {
+            builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondary);
+            if (DialogUtils.isColorDark(builder.contentColor)) {
+                if (builder.theme == Theme.DARK)
+                    builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
+            } else if (builder.theme == Theme.LIGHT)
+                builder.contentColor = DialogUtils.resolveColor(builder.context, android.R.attr.textColorSecondaryInverse);
+        }
+        if (!builder.itemColorSet)
+            builder.itemColor = builder.contentColor;
+
         // Retrieve references to views
         dialog.title = (TextView) dialog.view.findViewById(R.id.title);
         dialog.icon = (ImageView) dialog.view.findViewById(R.id.icon);

+ 71 - 69
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -857,6 +857,10 @@ public class MaterialDialog extends DialogBase implements
         protected int mProgress = -2;
         protected int mProgressMax = 0;
 
+        protected boolean titleColorSet = false;
+        protected boolean contentColorSet = false;
+        protected boolean itemColorSet = false;
+
         @DrawableRes
         protected int listSelector;
         @DrawableRes
@@ -889,11 +893,6 @@ public class MaterialDialog extends DialogBase implements
             // Load theme values from the ThemeSingleton if needed
             checkSingleton();
 
-            // Retrieve default title/content colors
-            this.titleColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
-            this.contentColor = DialogUtils.resolveColor(context, android.R.attr.textColorSecondary);
-            this.itemColor = this.contentColor;
-
             // Retrieve gravity settings from global theme attributes if needed
             this.titleGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_title_gravity, this.titleGravity);
             this.contentGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_content_gravity, this.contentGravity);
@@ -960,6 +959,7 @@ public class MaterialDialog extends DialogBase implements
 
         public Builder titleColor(int color) {
             this.titleColor = color;
+            this.titleColorSet = true;
             return this;
         }
 
@@ -1031,33 +1031,34 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
-        public Builder contentColor(int color) {
-            this.contentColor = color;
+        public Builder content(@StringRes int contentRes) {
+            content(this.context.getString(contentRes));
             return this;
         }
 
-        public Builder contentColorRes(@ColorRes int colorRes) {
-            contentColor(this.context.getResources().getColor(colorRes));
+        public Builder content(@NonNull CharSequence content) {
+            this.content = content;
             return this;
         }
 
-        public Builder contentColorAttr(@AttrRes int colorAttr) {
-            contentColor(DialogUtils.resolveColor(this.context, colorAttr));
+        public Builder content(@StringRes int contentRes, Object... formatArgs) {
+            content(this.context.getString(contentRes, formatArgs));
             return this;
         }
 
-        public Builder content(@StringRes int contentRes) {
-            content(this.context.getString(contentRes));
+        public Builder contentColor(int color) {
+            this.contentColor = color;
+            this.contentColorSet = true;
             return this;
         }
 
-        public Builder content(CharSequence content) {
-            this.content = content;
+        public Builder contentColorRes(@ColorRes int colorRes) {
+            contentColor(this.context.getResources().getColor(colorRes));
             return this;
         }
 
-        public Builder content(@StringRes int contentRes, Object... formatArgs) {
-            content(this.context.getString(contentRes, formatArgs));
+        public Builder contentColorAttr(@AttrRes int colorAttr) {
+            contentColor(DialogUtils.resolveColor(this.context, colorAttr));
             return this;
         }
 
@@ -1088,6 +1089,20 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder itemColor(int color) {
+            this.itemColor = color;
+            this.itemColorSet = true;
+            return this;
+        }
+
+        public Builder itemColorRes(@ColorRes int colorRes) {
+            return itemColor(this.context.getResources().getColor(colorRes));
+        }
+
+        public Builder itemColorAttr(@AttrRes int colorAttr) {
+            return itemColor(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder itemsGravity(@NonNull GravityEnum gravity) {
             this.itemsGravity = gravity;
             return this;
@@ -1164,6 +1179,19 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder positiveColor(int color) {
+            this.positiveColor = color;
+            return this;
+        }
+
+        public Builder positiveColorRes(@ColorRes int colorRes) {
+            return positiveColor(this.context.getResources().getColor(colorRes));
+        }
+
+        public Builder positiveColorAttr(@AttrRes int colorAttr) {
+            return positiveColor(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder neutralText(@StringRes int neutralRes) {
             return neutralText(this.context.getString(neutralRes));
         }
@@ -1173,6 +1201,19 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder negativeColor(int color) {
+            this.negativeColor = color;
+            return this;
+        }
+
+        public Builder negativeColorRes(@ColorRes int colorRes) {
+            return negativeColor(this.context.getResources().getColor(colorRes));
+        }
+
+        public Builder negativeColorAttr(@AttrRes int colorAttr) {
+            return negativeColor(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder negativeText(@StringRes int negativeRes) {
             return negativeText(this.context.getString(negativeRes));
         }
@@ -1182,6 +1223,19 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder neutralColor(int color) {
+            this.neutralColor = color;
+            return this;
+        }
+
+        public Builder neutralColorRes(@ColorRes int colorRes) {
+            return neutralColor(this.context.getResources().getColor(colorRes));
+        }
+
+        public Builder neutralColorAttr(@AttrRes int colorAttr) {
+            return neutralColor(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder listSelector(@DrawableRes int selectorRes) {
             this.listSelector = selectorRes;
             return this;
@@ -1281,45 +1335,6 @@ public class MaterialDialog extends DialogBase implements
             return progressColorRes(DialogUtils.resolveColor(this.context, colorAttr));
         }
 
-        public Builder positiveColor(int color) {
-            this.positiveColor = color;
-            return this;
-        }
-
-        public Builder positiveColorRes(@ColorRes int colorRes) {
-            return positiveColor(this.context.getResources().getColor(colorRes));
-        }
-
-        public Builder positiveColorAttr(@AttrRes int colorAttr) {
-            return positiveColor(DialogUtils.resolveColor(this.context, colorAttr));
-        }
-
-        public Builder negativeColor(int color) {
-            this.negativeColor = color;
-            return this;
-        }
-
-        public Builder negativeColorRes(@ColorRes int colorRes) {
-            return negativeColor(this.context.getResources().getColor(colorRes));
-        }
-
-        public Builder negativeColorAttr(@AttrRes int colorAttr) {
-            return negativeColor(DialogUtils.resolveColor(this.context, colorAttr));
-        }
-
-        public Builder neutralColor(int color) {
-            this.neutralColor = color;
-            return this;
-        }
-
-        public Builder neutralColorRes(@ColorRes int colorRes) {
-            return neutralColor(this.context.getResources().getColor(colorRes));
-        }
-
-        public Builder neutralColorAttr(@AttrRes int colorAttr) {
-            return neutralColor(DialogUtils.resolveColor(this.context, colorAttr));
-        }
-
         public Builder dividerColor(int color) {
             this.dividerColor = color;
             return this;
@@ -1346,19 +1361,6 @@ public class MaterialDialog extends DialogBase implements
             return backgroundColor(DialogUtils.resolveColor(this.context, colorAttr));
         }
 
-        public Builder itemColor(int color) {
-            this.itemColor = color;
-            return this;
-        }
-
-        public Builder itemColorRes(@ColorRes int colorRes) {
-            return itemColor(this.context.getResources().getColor(colorRes));
-        }
-
-        public Builder itemColorAttr(@AttrRes int colorAttr) {
-            return itemColor(DialogUtils.resolveColor(this.context, colorAttr));
-        }
-
         public Builder callback(@NonNull ButtonCallback callback) {
             this.callback = callback;
             return this;

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

@@ -102,4 +102,9 @@ public class DialogUtils {
     public static boolean resolveBoolean(Context context, @AttrRes int attr) {
         return resolveBoolean(context, attr, false);
     }
+
+    public static boolean isColorDark(int color) {
+        double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
+        return darkness >= 0.5;
+    }
 }