spacitron
spacitron

Reputation: 2183

Android: declaring colors in xml

EDIT I did the suggested changes but I still get the same error, as shown below.

I have a layout to which I'm trying to set the background color dynamically in response to clicks and so on.

This is the XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_pare"
    android:gravity="center_vertical"
    android:background="@drawable/custom_style"
   >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TextView"/>
</RelativeLayout>

And this is the file where I am trying to declare the colors, stored in Drawable/custom_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  android:color="#ff0000" android:state_selected="true"/>
    <item android:color="#ffffff" android:state_pressed="true" />
    <item android:color="#e3e3e3"/>
</selector>

The problem is that when I try to run this the app crashes with the following error:

03-24 16:29:50.540: E/AndroidRuntime(17643): FATAL EXCEPTION: main
03-24 16:29:50.540: E/AndroidRuntime(17643): android.view.InflateException: Binary XML                 file line #7: Error inflating class <unknown>
03-24 16:29:50.540: E/AndroidRuntime(17643):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
03-24 16:29:50.540: E/AndroidRuntime(17643):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)...

Upvotes: 0

Views: 2412

Answers (5)

Blo
Blo

Reputation: 11978

This is not a color but a drawable (.xml), it should be:

android:background="@drawable/custom_style"  

You should put your custom_style.xml in a drawable folder:
(Right click on res folder and create a folder named drawable)

enter image description here

Also change your selector like the following:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  android:drawable="@color/color_one" android:state_selected="true"/>
    <item android:drawable="@color/color_two" android:state_pressed="true" />
    <item android:drawable="@color/color_three"/>
</selector>

Into /res/values create a file named colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color_one" >#ff0000</color>
    <color name="color_two" >#ffffff</color>
    <color name="color_three" >#e3e3e3</color>
</resources>  

Finally, referencing your color in your item's selector by @color/color_name.

From the reference:

Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").


As @jaumebd (eagle eye ^^) said, your problem is this line android:layout_height="fill_pare", this is android:layout_height="fill_parent".

This should work.

Upvotes: 3

VikramV
VikramV

Reputation: 1111

Your custom_style.xml should be in the drawable folder. Hence your layout XML file would change to

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_pare"
    android:gravity="center_vertical"
    android:background="@drawable/custom_style"
   >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TextView"/>
</RelativeLayout>

Upvotes: 0

BSK-Team
BSK-Team

Reputation: 1810

Change

android:background="@color/custom_style"

to

android:background="@drawable/custom_style"`

And place custom_style.xml in the folder "drawable"

Upvotes: 0

jaumebd
jaumebd

Reputation: 696

First of all put your custom_style in res/drawable folder (if it not exists in your project, create it).

In your code you are trying to use a drawable xml as a color, this is the error.

To set the drawable to the background correctly you have to use:

android:background="@drawable/custom_style"

Upvotes: 0

user3456044
user3456044

Reputation: 1

You should place the custom_style.xml into your drawable folder.

Change following code in your xml file :

android:background="@color/custom_style"

to

android:background="@drawable/custom_style"

Upvotes: 0

Related Questions