Koitt
Koitt

Reputation: 1074

Can't access children of RippleDrawable

I have an ImageView that uses a shape as background

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#fedc13" />
    <stroke
        android:width="2dp"
        android:color="#cccccc" />
    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

And through code I can change the background color easily

((GradientDrawable) img.getBackground()).setColor(newColor);

This works perfectly, but I want to add a ripple effect for >Lollipop when the user presses the view

So I modified my xml for v21 to

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FF0000">
    <shape android:shape="oval">
        <solid android:color="#fedc13" />
        <stroke
            android:width="1dp"
            android:color="#00FF00" />
        <size
            android:width="120dp"
            android:height="120dp" />
    </shape>
</ripple>

And now to access the shape, the img.getBackground() now returns a RippleDrawable so I thought to get the contained shape. But when i do

((GradientDrawable)((RippleDrawable) img.getBackground()).getDrawable(0))
.setColor(newColor)

the RippleDrawable doesn't have any contained drawables, so it throws an exception.

Why can't I access the contained shape?

Upvotes: 0

Views: 154

Answers (1)

Koitt
Koitt

Reputation: 1074

Found it, I wasn't adding an <item> tag to the ripple child. Wrapping the shape with <item> solved it.

Upvotes: 1

Related Questions