Forráskód Böngészése

Merge pull request #590 from DaGenix/color-state-lists

Add support for using ColorStateLists to specify button text colors
Aidan Follestad 9 éve
szülő
commit
b1cd79af4a

+ 8 - 23
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -85,11 +85,11 @@ class DialogInit {
 
         // Retrieve action button colors from theme attributes or the Builder
         if (!builder.positiveColorSet)
-            builder.positiveColor = DialogUtils.resolveColor(builder.context, R.attr.md_positive_color, builder.positiveColor);
+            builder.positiveColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_positive_color, builder.positiveColor);
         if (!builder.neutralColorSet)
-            builder.neutralColor = DialogUtils.resolveColor(builder.context, R.attr.md_neutral_color, builder.neutralColor);
+            builder.neutralColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_neutral_color, builder.neutralColor);
         if (!builder.negativeColorSet)
-            builder.negativeColor = DialogUtils.resolveColor(builder.context, R.attr.md_negative_color, builder.negativeColor);
+            builder.negativeColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_negative_color, builder.negativeColor);
         if (!builder.widgetColorSet)
             builder.widgetColor = DialogUtils.resolveColor(builder.context, R.attr.md_widget_color, builder.widgetColor);
 
@@ -199,7 +199,7 @@ class DialogInit {
             dialog.content.setMovementMethod(new LinkMovementMethod());
             dialog.setTypeface(dialog.content, builder.regularFont);
             dialog.content.setLineSpacing(0f, builder.contentLineSpacingMultiplier);
-            if (builder.positiveColor == 0)
+            if (builder.positiveColor == null)
                 dialog.content.setLinkTextColor(DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorPrimary));
             else
                 dialog.content.setLinkTextColor(builder.positiveColor);
@@ -235,7 +235,7 @@ class DialogInit {
         dialog.setTypeface(positiveTextView, builder.mediumFont);
         positiveTextView.setAllCapsCompat(textAllCaps);
         positiveTextView.setText(builder.positiveText);
-        positiveTextView.setTextColor(getActionTextStateList(builder.context, builder.positiveColor));
+        positiveTextView.setTextColor(builder.positiveColor);
         dialog.positiveButton.setStackedSelector(dialog.getButtonSelector(DialogAction.POSITIVE, true));
         dialog.positiveButton.setDefaultSelector(dialog.getButtonSelector(DialogAction.POSITIVE, false));
         dialog.positiveButton.setTag(DialogAction.POSITIVE);
@@ -246,7 +246,7 @@ class DialogInit {
         dialog.setTypeface(negativeTextView, builder.mediumFont);
         negativeTextView.setAllCapsCompat(textAllCaps);
         negativeTextView.setText(builder.negativeText);
-        negativeTextView.setTextColor(getActionTextStateList(builder.context, builder.negativeColor));
+        negativeTextView.setTextColor(builder.negativeColor);
         dialog.negativeButton.setStackedSelector(dialog.getButtonSelector(DialogAction.NEGATIVE, true));
         dialog.negativeButton.setDefaultSelector(dialog.getButtonSelector(DialogAction.NEGATIVE, false));
         dialog.negativeButton.setTag(DialogAction.NEGATIVE);
@@ -257,7 +257,7 @@ class DialogInit {
         dialog.setTypeface(neutralTextView, builder.mediumFont);
         neutralTextView.setAllCapsCompat(textAllCaps);
         neutralTextView.setText(builder.neutralText);
-        neutralTextView.setTextColor(getActionTextStateList(builder.context, builder.neutralColor));
+        neutralTextView.setTextColor(builder.neutralColor);
         dialog.neutralButton.setStackedSelector(dialog.getButtonSelector(DialogAction.NEUTRAL, true));
         dialog.neutralButton.setDefaultSelector(dialog.getButtonSelector(DialogAction.NEUTRAL, false));
         dialog.neutralButton.setTag(DialogAction.NEUTRAL);
@@ -427,19 +427,4 @@ class DialogInit {
             dialog.inputMinMax = null;
         }
     }
-
-    private static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {
-        final int fallBackButtonColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
-        if (newPrimaryColor == 0) newPrimaryColor = fallBackButtonColor;
-        int[][] states = new int[][]{
-                new int[]{-android.R.attr.state_enabled}, // disabled
-                new int[]{} // enabled
-        };
-        int[] colors = new int[]{
-                DialogUtils.adjustAlpha(newPrimaryColor, 0.4f),
-                newPrimaryColor
-        };
-        return new ColorStateList(states, colors);
-    }
-
-}
+}

+ 38 - 25
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -3,6 +3,7 @@ package com.afollestad.materialdialogs;
 import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.ColorStateList;
 import android.graphics.Paint;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
@@ -370,9 +371,9 @@ public class MaterialDialog extends DialogBase implements
         protected CharSequence negativeText;
         protected View customView;
         protected int widgetColor;
-        protected int positiveColor;
-        protected int negativeColor;
-        protected int neutralColor;
+        protected ColorStateList positiveColor;
+        protected ColorStateList negativeColor;
+        protected ColorStateList neutralColor;
         protected ButtonCallback callback;
         protected ListCallback listCallback;
         protected ListCallbackSingleChoice listCallbackSingleChoice;
@@ -464,9 +465,9 @@ public class MaterialDialog extends DialogBase implements
                 this.widgetColor = DialogUtils.resolveColor(context, android.R.attr.colorAccent, this.widgetColor);
             }
 
-            this.positiveColor = this.widgetColor;
-            this.negativeColor = this.widgetColor;
-            this.neutralColor = this.widgetColor;
+            this.positiveColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
+            this.negativeColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
+            this.neutralColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
 
             this.progressPercentFormat = NumberFormat.getPercentInstance();
             this.progressNumberFormat = "%1d/%2d";
@@ -515,11 +516,11 @@ public class MaterialDialog extends DialogBase implements
                 this.titleColor = s.titleColor;
             if (s.contentColor != 0)
                 this.contentColor = s.contentColor;
-            if (s.positiveColor != 0)
+            if (s.positiveColor != null)
                 this.positiveColor = s.positiveColor;
-            if (s.neutralColor != 0)
+            if (s.neutralColor != null)
                 this.neutralColor = s.neutralColor;
-            if (s.negativeColor != 0)
+            if (s.negativeColor != null)
                 this.negativeColor = s.negativeColor;
             if (s.itemColor != 0)
                 this.itemColor = s.itemColor;
@@ -783,17 +784,21 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder positiveColor(@ColorInt int color) {
-            this.positiveColor = color;
-            this.positiveColorSet = true;
-            return this;
+            return positiveColor(DialogUtils.getActionTextStateList(context, color));
         }
 
         public Builder positiveColorRes(@ColorRes int colorRes) {
-            return positiveColor(this.context.getResources().getColor(colorRes));
+            return positiveColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
         }
 
         public Builder positiveColorAttr(@AttrRes int colorAttr) {
-            return positiveColor(DialogUtils.resolveColor(this.context, colorAttr));
+            return positiveColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
+        }
+
+        public Builder positiveColor(ColorStateList colorStateList) {
+            this.positiveColor = colorStateList;
+            this.positiveColorSet = true;
+            return this;
         }
 
         public Builder neutralText(@StringRes int neutralRes) {
@@ -806,17 +811,21 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder negativeColor(@ColorInt int color) {
-            this.negativeColor = color;
-            this.negativeColorSet = true;
-            return this;
+            return negativeColor(DialogUtils.getActionTextStateList(context, color));
         }
 
         public Builder negativeColorRes(@ColorRes int colorRes) {
-            return negativeColor(this.context.getResources().getColor(colorRes));
+            return negativeColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
         }
 
         public Builder negativeColorAttr(@AttrRes int colorAttr) {
-            return negativeColor(DialogUtils.resolveColor(this.context, colorAttr));
+            return negativeColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
+        }
+
+        public Builder negativeColor(ColorStateList colorStateList) {
+            this.negativeColor = colorStateList;
+            this.negativeColorSet = true;
+            return this;
         }
 
         public Builder negativeText(@StringRes int negativeRes) {
@@ -829,17 +838,21 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder neutralColor(@ColorInt int color) {
-            this.neutralColor = color;
-            this.neutralColorSet = true;
-            return this;
+            return neutralColor(DialogUtils.getActionTextStateList(context, color));
         }
 
         public Builder neutralColorRes(@ColorRes int colorRes) {
-            return neutralColor(this.context.getResources().getColor(colorRes));
+            return neutralColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
         }
 
         public Builder neutralColorAttr(@AttrRes int colorAttr) {
-            return neutralColor(DialogUtils.resolveColor(this.context, colorAttr));
+            return neutralColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
+        }
+
+        public Builder neutralColor(ColorStateList colorStateList) {
+            this.neutralColor = colorStateList;
+            this.neutralColorSet = true;
+            return this;
         }
 
         public Builder listSelector(@DrawableRes int selectorRes) {
@@ -1684,4 +1697,4 @@ public class MaterialDialog extends DialogBase implements
 
         void onInput(MaterialDialog dialog, CharSequence input);
     }
-}
+}

+ 4 - 3
library/src/main/java/com/afollestad/materialdialogs/ThemeSingleton.java

@@ -1,5 +1,6 @@
 package com.afollestad.materialdialogs;
 
+import android.content.res.ColorStateList;
 import android.graphics.drawable.Drawable;
 import android.support.annotation.ColorInt;
 import android.support.annotation.DrawableRes;
@@ -27,11 +28,11 @@ public class ThemeSingleton {
     @ColorInt
     public int contentColor = 0;
     @ColorInt
-    public int positiveColor = 0;
+    public ColorStateList positiveColor = null;
     @ColorInt
-    public int neutralColor = 0;
+    public ColorStateList neutralColor = null;
     @ColorInt
-    public int negativeColor = 0;
+    public ColorStateList negativeColor = null;
     @ColorInt
     public int widgetColor = 0;
     @ColorInt

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

@@ -2,11 +2,13 @@ package com.afollestad.materialdialogs.util;
 
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.ColorStateList;
 import android.content.res.TypedArray;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.support.annotation.AttrRes;
+import android.support.annotation.ColorRes;
 import android.util.TypedValue;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
@@ -40,6 +42,41 @@ public class DialogUtils {
         }
     }
 
+    // Try to resolve the colorAttr attribute.
+    public static ColorStateList resolveActionTextColorStateList(Context context, @AttrRes int colorAttr, ColorStateList fallback) {
+        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{colorAttr});
+        try {
+            final TypedValue value = a.peekValue(0);
+            if (value == null) {
+                return fallback;
+            }
+            if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
+                return getActionTextStateList(context, value.data);
+            } else {
+                final ColorStateList stateList = a.getColorStateList(0);
+                if (stateList != null) {
+                    return stateList;
+                } else {
+                    return fallback;
+                }
+            }
+        } finally {
+            a.recycle();
+        }
+    }
+
+    // Get the specified color resource, creating a ColorStateList if the resource
+    // points to a color value.
+    public static ColorStateList getActionTextColorStateList(Context context, @ColorRes int colorId) {
+        final TypedValue value = new TypedValue();
+        context.getResources().getValue(colorId, value, true);
+        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
+            return getActionTextStateList(context, value.data);
+        } else {
+            return context.getResources().getColorStateList(colorId);
+        }
+    }
+
     public static String resolveString(Context context, @AttrRes int attr) {
         TypedValue v = new TypedValue();
         context.getTheme().resolveAttribute(attr, v, true);
@@ -156,4 +193,19 @@ public class DialogUtils {
             }
         });
     }
+
+
+    public static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {
+        final int fallBackButtonColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
+        if (newPrimaryColor == 0) newPrimaryColor = fallBackButtonColor;
+        int[][] states = new int[][]{
+                new int[]{-android.R.attr.state_enabled}, // disabled
+                new int[]{} // enabled
+        };
+        int[] colors = new int[]{
+                DialogUtils.adjustAlpha(newPrimaryColor, 0.4f),
+                newPrimaryColor
+        };
+        return new ColorStateList(states, colors);
+    }
 }

+ 4 - 3
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -29,6 +29,7 @@ import com.afollestad.materialdialogs.ThemeSingleton;
 import com.afollestad.materialdialogs.internal.MDTintHelper;
 import com.afollestad.materialdialogs.simplelist.MaterialSimpleListAdapter;
 import com.afollestad.materialdialogs.simplelist.MaterialSimpleListItem;
+import com.afollestad.materialdialogs.util.DialogUtils;
 
 import java.io.File;
 
@@ -574,9 +575,9 @@ public class MainActivity extends AppCompatActivity implements
         selectedColorIndex = index;
         //noinspection ConstantConditions
         getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
-        ThemeSingleton.get().positiveColor = color;
-        ThemeSingleton.get().neutralColor = color;
-        ThemeSingleton.get().negativeColor = color;
+        ThemeSingleton.get().positiveColor = DialogUtils.getActionTextStateList(this, color);
+        ThemeSingleton.get().neutralColor = DialogUtils.getActionTextStateList(this, color);
+        ThemeSingleton.get().negativeColor = DialogUtils.getActionTextStateList(this, color);
         ThemeSingleton.get().widgetColor = color;
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             getWindow().setStatusBarColor(darker);