0

I want to use color.xml file for my button, but it show this error and app crash. It says, it cannot find the color.xml file.

Caused by: android.content.res.Resources$NotFoundException: File res/color/color.xml from drawable resource ID #0x7f0e00dd
  at android.content.res.Resources.loadDrawableForCookie(Resources.java:3783)
  at android.content.res.Resources.loadDrawable(Resources.java:3651)
  at android.content.res.TypedArray.getDrawable(TypedArray.java:762)
  at android.view.View.<init>(View.java:3983)
  at android.widget.TextView.<init>(TextView.java:1021)
  at android.widget.Button.<init>(Button.java:115)
  at android.widget.Button.<init>(Button.java:108)
  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:5

Here is my button xml:

<Button
    android:id="@+id/btn_buy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@color/color"
    android:textColor="@color/mdtp_white"
    android:text="@string/buy"/>

This is my color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/grey" />
    <item android:color="@color/myRedColor"/>
</selector>
10
  • add selector file in drawable Commented Sep 6, 2016 at 4:03
  • what is @color/buy? simply color-code or xml file? Commented Sep 6, 2016 at 4:15
  • Do you mean add the color.xml file to folder drawable? I try it, but still get the same error. Commented Sep 6, 2016 at 4:16
  • @K.Sopheak I don't see where the color/buy is declared. Your color.xml file does'n contains it. Commented Sep 6, 2016 at 4:18
  • @vrundpurohit I edit my post. Actually, it is @color/color. It is the color.xml file. Commented Sep 6, 2016 at 4:18

5 Answers 5

2

android:background attribute requires a drawable resource. All you need to do is moving your color.xml file to drawable folder.

Sign up to request clarification or add additional context in comments.

2 Comments

@K.Sopheak Please update your code first. I still see the word "buy"
@K.Sopheak I still see "/color/color.xml". You need to move color.xml file to folder drawable. And for your Button use this line: android:background="@drawable/color"
2

Create color.xml file in Drawable folder.

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

Use color.xml as background in button

<Button
    android:id="@+id/btn_buy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    // here
    android:background="@drawable/color"
    android:textColor="@color/mdtp_white"
    android:text="@string/buy"/>

Comments

1

Paste your color.xml file in drawble folder and add color.xml as backgrouned to your button like this

<Button
    android:id="@+id/btn_buy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/color"
    android:textColor="@color/mdtp_white"
    android:text="@string/buy"/>

Comments

1

put your color.xml file in drawable

make changes in drawable\color.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@color/grey" />
    <item android:drawable="@color/myRedColor"/>
</selector>

button xml

<Button
    android:id="@+id/btn_buy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/color"
    android:textColor="@color/mdtp_white"
    android:text="@string/buy"/>

Comments

0

Finally, I fixed the error. I look strange but it work. First, move the color.xml to drawable folder. Then update the color.xml like following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <color android:color="@color/grey"/>
    </item>
    <item>
        <color android:color="@color/myRedColor"/>
    </item>
</selector>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.