Browse Source

Added progressColor(), progressColorRes(), and progressColorAttr() to the Builder, along with a md_progress_color global theme attribute.

Aidan Follestad 10 years ago
parent
commit
92eac3e554

+ 15 - 0
README.md

@@ -507,6 +507,13 @@ or operating system. This behavior can be overridden in your Activity themes:
     -->
     -->
     <item name="md_negative_color">#673AB7</item>
     <item name="md_negative_color">#673AB7</item>
 
 
+    <!--
+        By default, a progress dialog's progress indicator color is derived
+        from the colorAccent attribute of AppCompat or android:colorAccent
+        attribute of the Material theme.
+    -->
+    <item name="md_progress_color">#673AB7</item>
+
     <!--
     <!--
         By default, the list item text color is black for the light
         By default, the list item text color is black for the light
         theme and white for the dark theme.
         theme and white for the dark theme.
@@ -671,6 +678,14 @@ dialog.setContent(getString(R.string.done));
 
 
 See the sample project for this dialog in action, with the addition of threading.
 See the sample project for this dialog in action, with the addition of threading.
 
 
+##### Coloring
+
+Like action buttons and many other elements of the Material dialog, you can customize the color of a 
+ progress dialog's progress indicator. The `Builder` class contains a `progressColor()`, `progressColorRes()`,
+ and `progressColorAttr()` method. Their names and parameter annotations make them self explanatory.
+ 
+There's also a global theming attribute as shown in the Global Theming section of this README: `md_progress_color`.
+
 ---
 ---
 
 
 ### Preference Dialogs
 ### Preference Dialogs

+ 11 - 12
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -83,6 +83,7 @@ class DialogInit {
         builder.positiveColor = DialogUtils.resolveColor(builder.context, R.attr.md_positive_color, builder.positiveColor);
         builder.positiveColor = DialogUtils.resolveColor(builder.context, R.attr.md_positive_color, builder.positiveColor);
         builder.neutralColor = DialogUtils.resolveColor(builder.context, R.attr.md_neutral_color, builder.neutralColor);
         builder.neutralColor = DialogUtils.resolveColor(builder.context, R.attr.md_neutral_color, builder.neutralColor);
         builder.negativeColor = DialogUtils.resolveColor(builder.context, R.attr.md_negative_color, builder.negativeColor);
         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 references to views
         // Retrieve references to views
         dialog.title = (TextView) dialog.view.findViewById(R.id.title);
         dialog.title = (TextView) dialog.view.findViewById(R.id.title);
@@ -303,18 +304,16 @@ class DialogInit {
         if (builder.mIndeterminateProgress || builder.mProgress > -2) {
         if (builder.mIndeterminateProgress || builder.mProgress > -2) {
             dialog.mProgress = (ProgressBar) dialog.view.findViewById(android.R.id.progress);
             dialog.mProgress = (ProgressBar) dialog.view.findViewById(android.R.id.progress);
 
 
-            // Manually color progress bar on pre-Lollipop, since Material/AppCompat themes only do it on API 21+
-            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
-                Drawable indDraw = dialog.mProgress.getIndeterminateDrawable();
-                if (indDraw != null) {
-                    indDraw.setColorFilter(builder.accentColor, PorterDuff.Mode.SRC_ATOP);
-                    dialog.mProgress.setIndeterminateDrawable(indDraw);
-                }
-                Drawable regDraw = dialog.mProgress.getProgressDrawable();
-                if (regDraw != null) {
-                    regDraw.setColorFilter(builder.accentColor, PorterDuff.Mode.SRC_ATOP);
-                    dialog.mProgress.setProgressDrawable(regDraw);
-                }
+            // Color the progress bar
+            Drawable indDraw = dialog.mProgress.getIndeterminateDrawable();
+            if (indDraw != null) {
+                indDraw.setColorFilter(builder.progressColor, PorterDuff.Mode.SRC_ATOP);
+                dialog.mProgress.setIndeterminateDrawable(indDraw);
+            }
+            Drawable regDraw = dialog.mProgress.getProgressDrawable();
+            if (regDraw != null) {
+                regDraw.setColorFilter(builder.progressColor, PorterDuff.Mode.SRC_ATOP);
+                dialog.mProgress.setProgressDrawable(regDraw);
             }
             }
 
 
             if (!builder.mIndeterminateProgress) {
             if (!builder.mIndeterminateProgress) {

+ 34 - 23
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -820,7 +820,7 @@ public class MaterialDialog extends DialogBase implements
         protected CharSequence neutralText;
         protected CharSequence neutralText;
         protected CharSequence negativeText;
         protected CharSequence negativeText;
         protected View customView;
         protected View customView;
-        protected int accentColor;
+        protected int progressColor;
         protected int positiveColor;
         protected int positiveColor;
         protected int negativeColor;
         protected int negativeColor;
         protected int neutralColor;
         protected int neutralColor;
@@ -878,30 +878,30 @@ public class MaterialDialog extends DialogBase implements
         public Builder(@NonNull Context context) {
         public Builder(@NonNull Context context) {
             this.context = context;
             this.context = context;
             final int materialBlue = context.getResources().getColor(R.color.md_material_blue_600);
             final int materialBlue = context.getResources().getColor(R.color.md_material_blue_600);
+            boolean useAppCompat = true;
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                 TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
                 TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
                 try {
                 try {
-                    this.accentColor = a.getColor(0, materialBlue);
-                    this.positiveColor = this.accentColor;
-                    this.negativeColor = this.accentColor;
-                    this.neutralColor = this.accentColor;
+                    this.progressColor = a.getColor(0, materialBlue);
+                    this.positiveColor = this.progressColor;
+                    this.negativeColor = this.progressColor;
+                    this.neutralColor = this.progressColor;
+                    useAppCompat = false;
                 } catch (Exception e) {
                 } catch (Exception e) {
-                    this.accentColor = materialBlue;
-                    this.positiveColor = materialBlue;
-                    this.negativeColor = materialBlue;
-                    this.neutralColor = materialBlue;
+                    e.printStackTrace();
                 } finally {
                 } finally {
                     a.recycle();
                     a.recycle();
                 }
                 }
-            } else {
+            }
+            if (useAppCompat) {
                 TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
                 TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
                 try {
                 try {
-                    this.accentColor = a.getColor(0, materialBlue);
-                    this.positiveColor = this.accentColor;
-                    this.negativeColor = this.accentColor;
-                    this.neutralColor = this.accentColor;
+                    this.progressColor = a.getColor(0, materialBlue);
+                    this.positiveColor = this.progressColor;
+                    this.negativeColor = this.progressColor;
+                    this.neutralColor = this.progressColor;
                 } catch (Exception e) {
                 } catch (Exception e) {
-                    this.accentColor = materialBlue;
+                    this.progressColor = materialBlue;
                     this.positiveColor = materialBlue;
                     this.positiveColor = materialBlue;
                     this.negativeColor = materialBlue;
                     this.negativeColor = materialBlue;
                     this.neutralColor = materialBlue;
                     this.neutralColor = materialBlue;
@@ -950,6 +950,8 @@ public class MaterialDialog extends DialogBase implements
                 this.btnSelectorNeutral = s.btnSelectorNeutral;
                 this.btnSelectorNeutral = s.btnSelectorNeutral;
             if (s.btnSelectorNegative != 0)
             if (s.btnSelectorNegative != 0)
                 this.btnSelectorNegative = s.btnSelectorNegative;
                 this.btnSelectorNegative = s.btnSelectorNegative;
+            if (s.progressColor != 0)
+                this.progressColor = s.progressColor;
             this.titleGravity = s.titleGravity;
             this.titleGravity = s.titleGravity;
             this.contentGravity = s.contentGravity;
             this.contentGravity = s.contentGravity;
             this.btnStackedGravity = s.btnStackedGravity;
             this.btnStackedGravity = s.btnStackedGravity;
@@ -1284,19 +1286,30 @@ public class MaterialDialog extends DialogBase implements
             return progress(indeterminate, max);
             return progress(indeterminate, max);
         }
         }
 
 
+        public Builder progressColor(int color) {
+            this.progressColor = color;
+            return this;
+        }
+
+        public Builder progressColorRes(@ColorRes int colorRes) {
+            return progressColor(this.context.getResources().getColor(colorRes));
+        }
+
+        public Builder progressColorAttr(@AttrRes int colorAttr) {
+            return progressColorRes(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder positiveColor(int color) {
         public Builder positiveColor(int color) {
             this.positiveColor = color;
             this.positiveColor = color;
             return this;
             return this;
         }
         }
 
 
         public Builder positiveColorRes(@ColorRes int colorRes) {
         public Builder positiveColorRes(@ColorRes int colorRes) {
-            positiveColor(this.context.getResources().getColor(colorRes));
-            return this;
+            return positiveColor(this.context.getResources().getColor(colorRes));
         }
         }
 
 
         public Builder positiveColorAttr(@AttrRes int colorAttr) {
         public Builder positiveColorAttr(@AttrRes int colorAttr) {
-            positiveColor(DialogUtils.resolveColor(this.context, colorAttr));
-            return this;
+            return positiveColor(DialogUtils.resolveColor(this.context, colorAttr));
         }
         }
 
 
         public Builder negativeColor(int color) {
         public Builder negativeColor(int color) {
@@ -1305,13 +1318,11 @@ public class MaterialDialog extends DialogBase implements
         }
         }
 
 
         public Builder negativeColorRes(@ColorRes int colorRes) {
         public Builder negativeColorRes(@ColorRes int colorRes) {
-            negativeColor(this.context.getResources().getColor(colorRes));
-            return this;
+            return negativeColor(this.context.getResources().getColor(colorRes));
         }
         }
 
 
         public Builder negativeColorAttr(@AttrRes int colorAttr) {
         public Builder negativeColorAttr(@AttrRes int colorAttr) {
-            negativeColor(DialogUtils.resolveColor(this.context, colorAttr));
-            return this;
+            return negativeColor(DialogUtils.resolveColor(this.context, colorAttr));
         }
         }
 
 
         public Builder neutralColor(int color) {
         public Builder neutralColor(int color) {

+ 2 - 1
library/src/main/java/com/afollestad/materialdialogs/ThemeSingleton.java

@@ -26,6 +26,7 @@ public class ThemeSingleton {
     public int positiveColor = 0;
     public int positiveColor = 0;
     public int neutralColor = 0;
     public int neutralColor = 0;
     public int negativeColor = 0;
     public int negativeColor = 0;
+    public int progressColor = 0;
     public int itemColor = 0;
     public int itemColor = 0;
     public Drawable icon = null;
     public Drawable icon = null;
     public int backgroundColor = 0;
     public int backgroundColor = 0;
@@ -47,4 +48,4 @@ public class ThemeSingleton {
     public GravityEnum btnStackedGravity = GravityEnum.START;
     public GravityEnum btnStackedGravity = GravityEnum.START;
     public GravityEnum itemsGravity = GravityEnum.START;
     public GravityEnum itemsGravity = GravityEnum.START;
     public GravityEnum buttonsGravity = GravityEnum.START;
     public GravityEnum buttonsGravity = GravityEnum.START;
-}
+}

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

@@ -18,6 +18,7 @@
     <attr name="md_positive_color" format="color" />
     <attr name="md_positive_color" format="color" />
     <attr name="md_neutral_color" format="color" />
     <attr name="md_neutral_color" format="color" />
     <attr name="md_negative_color" format="color" />
     <attr name="md_negative_color" format="color" />
+    <attr name="md_progress_color" format="color" />
 
 
     <attr name="md_item_color" format="color" />
     <attr name="md_item_color" format="color" />
     <attr name="md_divider_color" format="color" />
     <attr name="md_divider_color" format="color" />

+ 1 - 0
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -489,6 +489,7 @@ public class MainActivity extends ActionBarActivity implements
         ThemeSingleton.get().positiveColor = color;
         ThemeSingleton.get().positiveColor = color;
         ThemeSingleton.get().neutralColor = color;
         ThemeSingleton.get().neutralColor = color;
         ThemeSingleton.get().negativeColor = color;
         ThemeSingleton.get().negativeColor = color;
+        ThemeSingleton.get().progressColor = color;
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
             getWindow().setStatusBarColor(darker);
             getWindow().setStatusBarColor(darker);
     }
     }