2

I have a material text button <Button android:id="@+id/button" style="@style/Widget.MaterialComponents.Button.TextButton"/> that I'd like to change the colour of at runtime. So I set the text colour using button.setTextColor(Color.rgb(10, 10, 10)). Unfortunately, this doesn't change the background drawable, so when I click on the button it's ripple colour is unchanged. I'm guessing I need to change the background with something like attackButton.background = getDrawable(R.drawable.ripple), but I'm not sure how to populate the ripple.xml. Does this method make sense for changing the button text colour and ripple? If so, how should I write the ripple.xml?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Courtney Pattison
  • 1,596
  • 2
  • 19
  • 27

3 Answers3

1

To change the colors in the MaterialButton you can use:

  • button.setBackgroundTintList to change the background tint. You should use a selector.
  • button.setRippleColor to change the ripple color. Also in this case you should use a selector (see below)
  • button.setTextColor to change the color of the text. Also in this case you should use a selector.

It is the default selector used in the ripple color:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:alpha="@dimen/mtrl_low_ripple_pressed_alpha" android:color="?attr/colorPrimary" android:state_pressed="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorPrimary" android:state_focused="true" android:state_hovered="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_hovered_alpha" android:color="?attr/colorPrimary" android:state_hovered="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_default_alpha" android:color="?attr/colorPrimary"/>

</selector>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Thank you :) I forgot to cast the button as MaterialButton so I couldn't see these methods! `(button as MaterialButton).rippleColor = getColorStateList(R.color.button_secondary)`. Also, I set the alpha to .12 because the @dimen alphas are private. – Courtney Pattison Feb 25 '20 at 17:45
0

Have you tried a RippleDrawable? Then just Button.setBackground() to different resource or even a selector xml. If a ripple with selector won't be enough for you, setting the ripple mask itself can be done

myRipple.xml

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

programmatically:

LayerDrawable myRipple = ContextCompat.getDrawable(context, drawable.myRipple.xml);
myRipple.setDrawableByLayerId(R.id.myRippleMask,Color.rgb(10, 10, 10));

with each mask being a different color

SHartley
  • 37
  • 4
0

One line of code programmatic no resource file methods for textcolor, backgroundcolor, and ripplecolor:

 MaterialButton myMaterialButton = new MaterialButton(this);
 myMaterialButton.setTextColor(Color.RED);
 myMaterialButton.setBackgroundColor(Color.GRAY);
 myMaterialButton.setRippleColor(ColorStateList.valueOf(Color.RED));
Androidcoder
  • 4,389
  • 5
  • 35
  • 50