Android: ImageButton with drawable resources throws ResourceNotFoundException -


i'm trying create own button style following this instruction. created 2 drawables (shapes). 1 pressed-style , 1 normal-style. both defer in color.

but when start app, crashes exception (see below). somehow can't find button resource. why?

that's drawable shape (button_rounded_pressed.xml , button_rounded_normal.xml):

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle">     <solid android:color="?attr/colorprimary" />     <padding android:left="7dp"              android:top="7dp"              android:right="7dp"              android:bottom="7dp" />     <corners android:radius="8dp" /> </shape> 

then created selector button_rounded.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@drawable/button_rounded_pressed"           android:state_pressed="true" />     <item android:drawable="@drawable/button_rounded_normal" /> </selector> 

and use style/drawable/selector on imagebutton this:

    <imagebutton             android:layout_width="150dp"             android:layout_height="150dp"             android:layout_centerhorizontal="true"             android:layout_centervertical="true"             android:layout_alignparenttop="true"             android:src="@mipmap/ic_launcher"             android:background="@drawable/button_rounded"             /> 

but running application throws following exception when inflating layout.

caused by: android.content.res.resources$notfoundexception: file res/drawable/button_rounded.xml drawable resource id #0x7f02003b @ android.content.res.resources.loaddrawable(resources.java:1953) @ android.content.res.typedarray.getdrawable(typedarray.java:601) @ android.view.view.(view.java:3328)

note: can access code via github.

i bet you're testing on pre-lollipop device. need remove colorprimary attribute.

edit: per this bug report, referencing theme attributes in drawables wasn't supported until lollipop.


Comments