I am trying to create text within a FrameLayout, so that it looks like this:
However, I want it to have a different color when touched. So, I created a color state list in XML, like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">
<item android:color="@color/color_pressed" android:state_pressed="true" />
<item android:color="@color/color_default" />
</selector>
This works completely fine when I set it to the textColor attribute of the TextView. However, when I set it to the background attribute of the enclosing FrameLayout, I get an exception!
Here is the XML code in the layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/green_selector"
android:padding="2dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="17sp"
android:textColor="@color/green_selector"
android:layout_gravity="center"
android:text="Some Text"
android:background="@color/white"
android:gravity="center"/>
</FrameLayout>
The line android:textColor="@color/green_selector" works absolutely fine. However, android:background="@color/green_selector" gives me the following exception:
android.content.res.Resources$NotFoundException: File res/color/green_selector.xml from drawable resource ID #0x7f0a00e6
E/AndroidRuntime(25682): at android.content.res.Resources.loadDrawable(Resources.java:3439)
E/AndroidRuntime(25682): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
E/AndroidRuntime(25682): at android.view.View.<init>(View.java:3721)
E/AndroidRuntime(25682): at android.view.ViewGroup.<init>(ViewGroup.java:480)
E/AndroidRuntime(25682): at android.widget.FrameLayout.<init>(FrameLayout.java:101)
E/AndroidRuntime(25682): at android.widget.FrameLayout.<init>(FrameLayout.java:97)
E/AndroidRuntime(25682): ... 28 more
E/AndroidRuntime(25682): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
E/AndroidRuntime(25682): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:181)
E/AndroidRuntime(25682): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
E/AndroidRuntime(25682): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
E/AndroidRuntime(25682): at android.content.res.Resources.loadDrawable(Resources.java:3435)
E/AndroidRuntime(25682): ... 33 more
I get essentially the same error when I move the selector to res/drawable and change the line in the layout to android:background="@drawable/green_selector"
Does anyone have any idea what is going on here?? According to the documentation, this should be working fine.
No comments:
Post a Comment