ripple drawable crashes the app on Android API 19
Asked Answered
M

3

8

I am using a custom ripple drawable

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_shortAnimTime"
    android:color="@android:color/white">

   <item android:id="@android:id/mask">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/white" />
        </shape>
   </item>


</ripple>

but it crashes the app on API 19

android.content.res.Resources$NotFoundException: File res / drawable /
    ripple_effect_square2.xml from drawable resource ID #0x7f02017d
at android.content.res.Resources.loadDrawable(Resources.java:2101)
at android.content.res.Resources.getDrawable(Resources.java:700)
at android.view.View.setBackgroundResource(View.java:15303)

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line # 2: invalid drawable tag ripple
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java: 933)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java: 877)
at android.content.res.Resources.loadDrawable(Resources.java: 2097)
at android.content.res.Resources.getDrawable(Resources.java: 700) 
at android.view.View.setBackgroundResource(View.java: 15303) 

What should i do to prevent crashing ?

Maag answered 7/8, 2016 at 12:7 Comment(0)
S
20

The RippleDrawable was added in API 21, so it's not available on earlier SDKs.

You can move your drawable file to res/drawable-v21 to ensure it doesn't crash on earlier releases.

Ship answered 7/8, 2016 at 12:13 Comment(1)
is there any alternative ways to do this on pre 21Maag
K
13

I had the same problem.

Mina Samy's answer solved my problem also.

Atef Hares asked for an alternative in older versions. What works for me is to use a selector instead of ripple

for example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#ccffffff" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <stroke android:color="@android:color/white" />
        </shape>
    </item>
    <item android:drawable="@color/playColor" />
</selector>

where

<color name="playColor">#E8EAF6</color>
Krieg answered 27/3, 2017 at 17:2 Comment(0)
T
2

Ripple is only available from Lollipop (Android API 21). Here is an example for backwards compatibility which includes states and no default background (Transparent):

Create two drawable xml files with the same name, one will be placed in the drawable-v21 folder, and the other in the normal drawable folder.

drawable-v21/ripple_red_solid.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/red">

    <item android:id="@android:id/mask">
        <color android:color="@android:color/red" />
    </item>

    <item>
        <selector>
            <item android:state_selected="true">
                <color android:color="@android:color/red" />
            </item>
            <item android:state_activated="true">
                <color android:color="@android:color/red" />
            </item>
            <item android:state_focused="true">
                <color android:color="@android:color/red" />
            </item>
        </selector>
    </item>
</ripple>

drawable/ripple_red_solid.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />
        </shape>
    </item>
</selector>

And then simply use it on the view like:

<LinearLayout
    ...
    android:background="@drawable/ripple_red_solid" />
Towpath answered 25/10, 2019 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.