Issue
I'm trying to create my own button style by following this instruction. So I created two drawables (shapes). One for the pressed-style and one for the normal-style. Both just defer in the color.
But when I start the app, it crashes with the exception (see below). Somehow it can't find the button resource. But why?
That's the drawable shape (button_rounded_pressed.xml and 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 I created a 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 then use this style/drawable/selector on an ImageButton like 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 the application throws the following exception when inflating the layout.
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_rounded.xml from drawable resource ID #0x7f02003b at android.content.res.Resources.loadDrawable(Resources.java:1953) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:3328)
Note: You can access my code via GitHub.
Solution
I bet you're testing on a pre-Lollipop device. You need to remove the colorPrimary
attribute.
EDIT: As per this bug report, referencing theme attributes in drawables wasn't supported until Lollipop.
Answered By - timemanx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.