Skip to content
Advertisement

android.content.res.Resources$NotFoundException – /res/color/file.xml

I’m trying to implement the Color State List Resources in my android application, following Android developer guide Color State List Resource | Android Developers. So I created two resource files button_text.xml and button_background.xml in the directory res/color/ and referred them in the with @color/button_text and @color/button_background respectively in the activity_main.xml. Here’s the layout code:

JavaScript

On running the App, I’m getting a Resources Not Found Exception in the logs as shown below:

JavaScript

I’ve tried to clean and rebuild the project but it didn’t work. Also, putting the xml files in res/drawable/ instead of res/color/ is showing the same error.Please help solving the issue. Thanks in advance!

EDIT:

Here is the button_text.xml file code:

JavaScript

And, button_background.xml file code:

JavaScript

Advertisement

Answer

So, here it is. Please read the full answer. The android:background attribute does not supports the Color State List unlike android:textColor. To make android:background property work, you need State List Drawables. Thus, instead of res/color/ create the file in res/drawable/ directory. Here’s the code:

JavaScript

Note the use of android:drawable instead of android:color. Android will use the color resource and make a drawable out of it. To finish this off, you need to add the color resources to your res/values/colors.xml file.

For More information : CLICK HERE!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement