java - Android: declaring colors in xml -
edit did suggested changes still same error, shown below.
i have layout i'm trying set background color dynamically in response clicks , on.
this xml 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 file trying declare 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 when try run app crashes 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): @ android.view.layoutinflater.createview(layoutinflater.java:626) 03-24 16:29:50.540: e/androidruntime(17643): @ com.android.internal.policy.impl.phonelayoutinflater.oncreateview(phonelayoutinflater.java:56)...
this not color
drawable (.xml), should be:
android:background="@drawable/custom_style"
you should put custom_style.xml
in drawable
folder:
(right click on res
folder , create folder named drawable
)
also change selector
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 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 color in item's selector @color/color_name
.
from reference:
note: color resource can used drawable in xml. example, when creating state list drawable, can reference color resource android:drawable attribute (android:drawable="@color/green").
as @jaumebd (eagle eye ^^) said, problem line android:layout_height="fill_pare"
, android:layout_height="fill_parent"
.
this should work.
Comments
Post a Comment