1

I have a layout:

<com.google.android.material.button.MaterialButtonToggleGroup
    ...
    app:checkedButton="@+id/favorite_color1"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/favorite_color1"
        ... />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/favorite_color2"
        ... />

</com.google.android.material.button.MaterialButtonToggleGroup>

in my fragment I can set background color in this way:

favorite_color1.setBackgroundColor(color)

A MaterialButton has a method background that returns a RippleDrawable and I saw this question but it doesn't work and it is out of date probably.

How Can I get background color for a MaterialButton programmatically?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
capo11
  • 790
  • 3
  • 11
  • 32

1 Answers1

3

In the MaterialButton the background color is defined by the app:backgroundTint attribute (not the background attribute).

The related method to set/get the background color are:

  • setBackgroundColor
  • setBackgroundTintList
  • getBackgroundTintList

In your case you can use:

button.getBackgroundTintList()

This is a ColorStateList.
You can get the color of each state with the method: colorStateList.getColorForState.

For example:

textView.setTextColor(
        colorStateList!!.getColorForState(
      intArrayOf(android.R.attr.state_enabled), 0))

or in java:

textView.setTextColor(colorStateList.getColorForState(
     new int[] { android.R.attr.state_enabled},0));

Just a note.
If you are using the setBackgroundColor method like favorite_color1.setBackgroundColor(color) the code above doesn't work.

You have to use the method setBackgroundTintList

favorite_color1.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color)))
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • ok but `button.getBackgroundTintList()` returns a `ColorStateList`, and then? I Can get the `defaultColor` but the value is a number like 352321535. Is this the right button background color? How Can I pass this number to a `Color` object? – capo11 Jun 04 '20 at 13:08
  • @capo11 What is the final goal? How do you want to use the color? – Gabriele Mariotti Jun 04 '20 at 14:08
  • the final goal is get the backgound color of the button and then change the background color of another view with the same color. – capo11 Jun 04 '20 at 14:20
  • @capo11 if the other view works with a `ColorStateList` just use it. If it works with a single color use the method `colorStateList.getColorForState`. I've just updated the answer. – Gabriele Mariotti Jun 04 '20 at 14:27
  • it doesn't work :( the method `colorStateList.getColorForState` always returns 0. I tried to change `android.R.attr` and the `defaultColor` parameter but the result is always 0. – capo11 Jun 05 '20 at 07:11
  • @capo11 I tried it before writing the answer with the [standard style](https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/res/color/mtrl_btn_bg_color_selector.xml#L19). The method `getColorForState` returns the color starting for a state. It depends also by the original color/selector defined in the button (There isn't the code in your question). Also check if the other view works with a color or a selector. – Gabriele Mariotti Jun 05 '20 at 08:05
  • I have no doubt you tried it. I have not define a selector for my button, do I have to do it for my scope? The other View is a `androidx.appcompat.widget.AppCompatImageView` – capo11 Jun 05 '20 at 08:19
  • @capo11 No, you don't need to define a selector. it is by default. In the ImageView what attribute are you trying to change? – Gabriele Mariotti Jun 05 '20 at 08:22
  • my android running version is 29 and I'm using `com.google.android.material:material:1.2.0-alpha06` library. Can is this the problem for our mismatch? – capo11 Jun 05 '20 at 08:26
  • @ Gabriele Mariotti `val drawable = color_panel.background as GradientDrawable drawable.setColor(color)` and it works fine (because I change this color in another part of my app) but I don't think the problem is in the other view if the color value is 0. Maybe is the problem in how wh call `getColorForState`? – capo11 Jun 05 '20 at 08:37
  • @capo11 Are you using `android:backgroud` in the layout or `button.setBackgroud(drawable)` ? – Gabriele Mariotti Jun 05 '20 at 09:06
  • I use `button.setBackgroudColor(color)` only, with `style="@style/Widget.MaterialComponents.Button.OutlinedButton"` – capo11 Jun 05 '20 at 09:24
  • @capo11 using button.setBackgroudColor(color) the code above doesn't work. I've updated the answer. – Gabriele Mariotti Jun 05 '20 at 10:04
  • 1
    Yes!! It works! Thank you very much, you are a genius! – capo11 Jun 05 '20 at 12:57