Browse Source

Added the ability to change action button's ripple color. Resolves https://github.com/afollestad/material-dialogs/issues/709.

Aidan Follestad 10 năm trước cách đây
mục cha
commit
519ebc9ba3

+ 10 - 1
README.md

@@ -525,6 +525,7 @@ new MaterialDialog.Builder(this)
         .neutralColorRes(R.color.material_red_500)
         .negativeColorRes(R.color.material_red_500)
         .widgetColorRes(R.color.material_red_500)
+        .buttonRippleColorRes(R.color.material_red_500)
         .show();
 ```
 
@@ -535,7 +536,7 @@ color attributes.
 
 ## Selectors
 
-Theming selectors allows you to change colors for pressable things:
+Selectors are drawables that change state when pressed or focused.
 
 ```java
 new MaterialDialog.Builder(this)
@@ -553,6 +554,8 @@ used when the buttons become stacked, either because there's not enough room to
 or because you used `forceStacked(true)` on the `Builder`. `listSelector` is used for list items, when
 you are NOT using a custom adapter.
 
+***Note***: 
+
 ***An important note related to using custom action button selectors***: make sure your selector drawable references
 inset drawables like the default ones do - this is important for correct action button padding.
 
@@ -694,6 +697,12 @@ you show from an Activity which has a theme containing any of these attributes:
     -->
     <item name="md_divider_color">#E91E63</item>
 
+    <!--
+        This overrides the color used for the ripple displayed on action buttons (Lollipop and above).
+        Defaults to the colorControlHighlight attribute from AppCompat OR the Material theme.
+    -->
+    <item name="md_btn_ripple_color">#E91E63</item>
+
     <!--
         This overrides the selector used on list items.
     -->

+ 38 - 14
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -7,6 +7,7 @@ import android.content.res.ColorStateList;
 import android.graphics.Paint;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.RippleDrawable;
 import android.os.Build;
 import android.os.Handler;
 import android.support.annotation.ArrayRes;
@@ -282,23 +283,32 @@ public class MaterialDialog extends DialogBase implements
                 default: {
                     if (mBuilder.btnSelectorPositive != 0)
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorPositive, null);
-                    final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_positive_selector);
+                    Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_positive_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_positive_selector);
+                    d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_positive_selector);
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && d instanceof RippleDrawable)
+                        ((RippleDrawable) d).setColor(ColorStateList.valueOf(mBuilder.buttonRippleColor));
+                    return d;
                 }
                 case NEUTRAL: {
                     if (mBuilder.btnSelectorNeutral != 0)
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNeutral, null);
-                    final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_neutral_selector);
+                    Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_neutral_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_neutral_selector);
+                    d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_neutral_selector);
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && d instanceof RippleDrawable)
+                        ((RippleDrawable) d).setColor(ColorStateList.valueOf(mBuilder.buttonRippleColor));
+                    return d;
                 }
                 case NEGATIVE: {
                     if (mBuilder.btnSelectorNegative != 0)
                         return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNegative, null);
-                    final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_negative_selector);
+                    Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_negative_selector);
                     if (d != null) return d;
-                    return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_negative_selector);
+                    d = DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_negative_selector);
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && d instanceof RippleDrawable)
+                        ((RippleDrawable) d).setColor(ColorStateList.valueOf(mBuilder.buttonRippleColor));
+                    return d;
                 }
             }
         }
@@ -372,6 +382,7 @@ public class MaterialDialog extends DialogBase implements
         protected GravityEnum btnStackedGravity = GravityEnum.END;
         protected GravityEnum itemsGravity = GravityEnum.START;
         protected GravityEnum buttonsGravity = GravityEnum.START;
+        protected int buttonRippleColor = 0;
         protected int titleColor = -1;
         protected int contentColor = -1;
         protected CharSequence content;
@@ -454,10 +465,6 @@ public class MaterialDialog extends DialogBase implements
             return context;
         }
 
-        public final GravityEnum getItemsGravity() {
-            return itemsGravity;
-        }
-
         public final int getItemColor() {
             return itemColor;
         }
@@ -480,6 +487,12 @@ public class MaterialDialog extends DialogBase implements
             this.negativeColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
             this.neutralColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
 
+            int fallback = 0;
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
+                fallback = DialogUtils.resolveColor(context, android.R.attr.colorControlActivated);
+            this.buttonRippleColor = DialogUtils.resolveColor(context, R.attr.md_btn_ripple_color,
+                    DialogUtils.resolveColor(context, R.attr.colorControlActivated, fallback));
+
             this.progressPercentFormat = NumberFormat.getPercentInstance();
             this.progressNumberFormat = "%1d/%2d";
 
@@ -575,6 +588,19 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder buttonRippleColor(@ColorInt int color) {
+            this.buttonRippleColor = color;
+            return this;
+        }
+
+        public Builder buttonRippleColorRes(@ColorRes int colorRes) {
+            return buttonRippleColor(ContextCompat.getColor(this.context, colorRes));
+        }
+
+        public Builder buttonRippleColorAttr(@AttrRes int colorAttr) {
+            return buttonRippleColor(DialogUtils.resolveColor(this.context, colorAttr));
+        }
+
         public Builder titleColor(@ColorInt int color) {
             this.titleColor = color;
             this.titleColorSet = true;
@@ -582,13 +608,11 @@ public class MaterialDialog extends DialogBase implements
         }
 
         public Builder titleColorRes(@ColorRes int colorRes) {
-            titleColor(ContextCompat.getColor(this.context, colorRes));
-            return this;
+            return titleColor(ContextCompat.getColor(this.context, colorRes));
         }
 
         public Builder titleColorAttr(@AttrRes int colorAttr) {
-            titleColor(DialogUtils.resolveColor(this.context, colorAttr));
-            return this;
+            return titleColor(DialogUtils.resolveColor(this.context, colorAttr));
         }
 
         /**

+ 2 - 0
core/src/main/res/values/attrs.xml

@@ -29,6 +29,8 @@
     <attr name="md_btn_neutral_selector" format="reference" />
     <attr name="md_btn_negative_selector" format="reference" />
 
+    <attr name="md_btn_ripple_color" format="color" />
+
     <attr name="md_title_gravity" format="enum">
         <enum name="start" value="0" />
         <enum name="center" value="1" />

+ 2 - 0
sample/src/main/res/values-v21/styles.xml

@@ -6,6 +6,8 @@
         <item name="colorPrimaryDark">@color/material_indigo_600</item>
         <item name="colorAccent">@color/material_teal_500</item>
 
+        <item name="colorControlHighlight">@color/material_teal_500</item>
+
         <item name="android:navigationBarColor">@color/material_indigo_500</item>
     </style>
 

+ 2 - 0
sample/src/main/res/values/styles.xml

@@ -5,6 +5,8 @@
         <item name="colorPrimary">@color/material_indigo_500</item>
         <item name="colorPrimaryDark">@color/material_indigo_600</item>
         <item name="colorAccent">@color/material_teal_500</item>
+
+        <item name="colorControlHighlight">@color/material_teal_500</item>
     </style>
 
 </resources>