Browse Source

Title, content, and list item colors will be derived from system attributes by default. However this will currently cause issues if you use a dark dialog with a light activity theme, or vice versa.

Aidan Follestad 10 years ago
parent
commit
7a96180c7f

+ 2 - 31
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -134,12 +134,7 @@ class DialogInit {
         } else {
             dialog.title.setText(builder.title);
             dialog.setTypeface(dialog.title, builder.mediumFont);
-            if (builder.titleColorSet) {
-                dialog.title.setTextColor(builder.titleColor);
-            } else {
-                final int fallback = DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorPrimary);
-                dialog.title.setTextColor(DialogUtils.resolveColor(dialog.getContext(), R.attr.md_title_color, fallback));
-            }
+            dialog.title.setTextColor(builder.titleColor);
             dialog.title.setGravity(MaterialDialog.gravityEnumToGravity(builder.titleGravity));
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                 //noinspection ResourceType
@@ -158,31 +153,16 @@ class DialogInit {
             } else {
                 dialog.content.setLinkTextColor(builder.positiveColor);
             }
+            dialog.content.setTextColor(builder.contentColor);
             dialog.content.setGravity(MaterialDialog.gravityEnumToGravity(builder.contentGravity));
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                 //noinspection ResourceType
                 dialog.content.setTextAlignment(MaterialDialog.gravityEnumToTextAlignment(builder.contentGravity));
             }
-            if (builder.contentColorSet) {
-                dialog.content.setTextColor(builder.contentColor);
-            } else {
-                final int fallback = DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorSecondary);
-                final int contentColor = DialogUtils.resolveColor(dialog.getContext(), R.attr.md_content_color, fallback);
-                dialog.content.setTextColor(contentColor);
-            }
         } else if (dialog.content != null) {
             dialog.content.setVisibility(View.GONE);
         }
 
-        // Load default list item text color
-        if (builder.itemColorSet) {
-            dialog.defaultItemColor = builder.itemColor;
-        } else if (builder.theme == Theme.LIGHT) {
-            dialog.defaultItemColor = Color.BLACK;
-        } else {
-            dialog.defaultItemColor = Color.WHITE;
-        }
-
         // Setup list dialog stuff
         if (builder.listCallbackMultiChoice != null)
             dialog.selectedIndicesList = new ArrayList<>();
@@ -288,15 +268,6 @@ class DialogInit {
                         }
                     }
                 });
-
-        // Gingerbread compatibility stuff
-        if (builder.theme == Theme.LIGHT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
-            dialog.setInverseBackgroundForced(true);
-            if (!builder.titleColorSet)
-                dialog.title.setTextColor(Color.BLACK);
-            if (!builder.contentColorSet && dialog.content != null)
-                dialog.content.setTextColor(Color.BLACK);
-        }
     }
 
     private static void setupProgressDialog(final MaterialDialog dialog) {

+ 20 - 39
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -7,7 +7,6 @@ import android.content.Context;
 import android.content.DialogInterface;
 import android.content.res.ColorStateList;
 import android.content.res.Resources;
-import android.content.res.TypedArray;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.Typeface;
@@ -858,12 +857,6 @@ public class MaterialDialog extends DialogBase implements
         protected int mProgress = -2;
         protected int mProgressMax = 0;
 
-        // Since 0 is black and -1 is white, no default value is good for indicating if a color was set.
-        // So this is a decent solution to this problem.
-        protected boolean titleColorSet;
-        protected boolean contentColorSet;
-        protected boolean itemColorSet;
-
         @DrawableRes
         protected int listSelector;
         @DrawableRes
@@ -878,39 +871,30 @@ public class MaterialDialog extends DialogBase implements
         public Builder(@NonNull Context context) {
             this.context = context;
             final int materialBlue = context.getResources().getColor(R.color.md_material_blue_600);
-            boolean useAppCompat = true;
+
+            // Retrieve default accent colors, which are used on the action buttons and progress bars
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-                TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
-                try {
-                    this.progressColor = a.getColor(0, materialBlue);
-                    this.positiveColor = this.progressColor;
-                    this.negativeColor = this.progressColor;
-                    this.neutralColor = this.progressColor;
-                    useAppCompat = false;
-                } catch (Exception e) {
-                    e.printStackTrace();
-                } finally {
-                    a.recycle();
-                }
-            }
-            if (useAppCompat) {
-                TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
-                try {
-                    this.progressColor = a.getColor(0, materialBlue);
-                    this.positiveColor = this.progressColor;
-                    this.negativeColor = this.progressColor;
-                    this.neutralColor = this.progressColor;
-                } catch (Exception e) {
-                    this.progressColor = materialBlue;
-                    this.positiveColor = materialBlue;
-                    this.negativeColor = materialBlue;
-                    this.neutralColor = materialBlue;
-                } finally {
-                    a.recycle();
-                }
+                final int fallback = DialogUtils.resolveColor(context, R.attr.colorAccent, materialBlue);
+                this.progressColor = DialogUtils.resolveColor(context, android.R.attr.colorAccent, fallback);
+                this.positiveColor = this.progressColor;
+                this.negativeColor = this.progressColor;
+                this.neutralColor = this.progressColor;
+            } else {
+                this.progressColor = DialogUtils.resolveColor(context, R.attr.colorAccent, materialBlue);
+                this.positiveColor = this.progressColor;
+                this.negativeColor = this.progressColor;
+                this.neutralColor = this.progressColor;
             }
 
+            // 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);
             this.btnStackedGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_btnstacked_gravity, this.btnStackedGravity);
@@ -976,7 +960,6 @@ public class MaterialDialog extends DialogBase implements
 
         public Builder titleColor(int color) {
             this.titleColor = color;
-            this.titleColorSet = true;
             return this;
         }
 
@@ -1050,7 +1033,6 @@ public class MaterialDialog extends DialogBase implements
 
         public Builder contentColor(int color) {
             this.contentColor = color;
-            this.contentColorSet = true;
             return this;
         }
 
@@ -1366,7 +1348,6 @@ public class MaterialDialog extends DialogBase implements
 
         public Builder itemColor(int color) {
             this.itemColor = color;
-            this.itemColorSet = true;
             return this;
         }
 

+ 1 - 3
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogAdapter.java

@@ -18,7 +18,6 @@ import com.afollestad.materialdialogs.util.DialogUtils;
 
 class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
 
-    private final int itemColor;
     private final MaterialDialog dialog;
     private final GravityEnum itemGravity;
 
@@ -26,7 +25,6 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
         super(dialog.mBuilder.context, resource, textViewResourceId, objects);
         this.dialog = dialog;
         this.itemGravity = dialog.mBuilder.itemsGravity;
-        this.itemColor = DialogUtils.resolveColor(getContext(), R.attr.md_item_color, dialog.defaultItemColor);
     }
 
     @Override
@@ -59,7 +57,7 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
             }
         }
         tv.setText(dialog.mBuilder.items[index]);
-        tv.setTextColor(itemColor);
+        tv.setTextColor(dialog.mBuilder.itemColor);
         dialog.setTypeface(tv, dialog.mBuilder.regularFont);
         view.setTag(index + ":" + dialog.mBuilder.items[index]);
         setupGravity((ViewGroup) view);