K.Sopheak
K.Sopheak

Reputation: 23144

android.view.InflateException: Binary XML file line #158: Error inflating class Button

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>

Upvotes: 0

Views: 2001

Answers (5)

K.Sopheak
K.Sopheak

Reputation: 23144

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>

Upvotes: 0

W4R10CK
W4R10CK

Reputation: 5550

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"/>

Upvotes: 2

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

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"/>

Upvotes: 1

TOP
TOP

Reputation: 2614

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

Upvotes: 2

Hasan shaikh
Hasan shaikh

Reputation: 740

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"/>

Upvotes: 1

Related Questions