how do I change the solid color of a ripple drawable?
Asked Answered
C

2

1
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
    <item>
        <selector>
            <item android:state_selected="true">
                <layer-list>
                    <item android:left="-5dp"
                          android:top="-5dp"
                          android:right="-5dp">
                        <shape android:shape="rectangle">
                            <stroke android:width="3dp"
                                    android:color="@android:color/white"/>
                            <solid android:color="@android:color/transparent"/>
                        </shape>
                    </item>
                </layer-list>
            </item>
            <item android:state_selected="false">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/transparent"/>
                </shape>
            </item>

        </selector>
    </item>
</ripple>

here is my ripple drawable and i want to change the state_selected, solid color.

code i've tried:

RippleDrawable rippleDrawable = (RippleDrawable) textView.getBackground(); // assumes bg is a RippleDrawable
        int[][] states = new int[][]{new int[]{android.R.attr.state_selected}};
        int[] colors = new int[]{R.color.white}; 
        ColorStateList colorStateList = new ColorStateList(states, colors);
        rippleDrawable.setColor(colorStateList);

unfortuantely it doesn't work.. what am I missing and is this possible?

Cuckoopint answered 20/4, 2017 at 23:32 Comment(1)
#26968372 use a mask.Cuckoopint
F
5

You should add an id to items to access them via java/kotlin.
check this background XML file

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#e0e0e0">
   <item android:id="@+id/fab_shape">
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle">
         <corners android:radius="25dp" />
        <solid android:color="@color/colorAccent" />
     </shape>
  </item>
</ripple>

to change the solid color of this, on constraintLayout background, this drawable XML is applied

val background = constraintLayout.background as RippleDrawable
val bgShape = background.findDrawableByLayerId(R.id.fab_shape) as GradientDrawable
bgShape.color = color

for reference read this

Flin answered 17/12, 2017 at 18:1 Comment(0)
B
1

That's weird. I have a very similar code and it works fine:

val background = textView.background!!.mutate() as RippleDrawable
background.setColor(ColorStateList.valueOf(0xffff0000.toInt()))
textView.background = background

Try it.

I've made an easier function too:

fun View.setBackgroundTintColor(@ColorInt color: Int) {
    val background = background
    if (background is RippleDrawable) {
        val newBackground = background.mutate() as RippleDrawable
        newBackground.setColor(ColorStateList.valueOf(color))
        this.background = newBackground
    } else ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
}
Brenna answered 8/3, 2023 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.