简体   繁体   中英

How to change Android Button ripple colors while preserving other style properties?

I want to inherit all Button styles from default material design style and preserve background colors and shadows which appear when pressing a button (so called elevation). The only thing I want to change is the color of ripple effect (when you touch a button and then move your finger around).

If I use the following style:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="android:buttonStyle">@style/MyTheme.Button</item>
</style>

<style name="MyTheme.Button" parent="Widget.AppCompat.Button.Colored">
  <item name="android:background">@drawable/btn_material</item>
</style>

and drawable/btn_material is:

<ripple android:color="#ffff0000">
  <item android:id="@android:id/mask"
     android:drawable="@android:color/white" />
</ripple>

then my button loses its background color and elevation shadow.

Is there any way to tell the button to keep the default colors and shadows inherited from parent and only replace the ripple color with the one given? Essentially I'd like to achieve something like android:backgroundRipple property which would change only the ripple leaving everything else intact.

RippleDrawable ripple= (RippleDrawable)view.getBackground();

int[][] attr= new int[][] { new int[] { android.R.attr.state_enabled} }; int[] colors = new int[] { Color.RED }; // sets the ripple color to red

ColorStateList color = new Color(states, colors); ripple.setColor(color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM