1

I am currently developing a small Android app in Xamarin, using C#.
The following shows up when I run it on my emulator:
Screenshot of Xamarin error
This line of code is called when the first tab is selected, which is by default, meaning that this error occurs as soon as I run the program. The XML snippet which seems to be causing the error is this, in one of my layout files:

android:background="@color/done"

This is in line 111 of Dialer.axml, the entirety of which is below. Here, I am trying to reference a color state list for my button, so that the color changes depending on whether it is being touched or not. I know that this line causes the error, because removing it from the button tag which it's in causes the program to run perfectly. Here is the code for done.xml, in my color folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="#ff2222ff"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="#ff4444ff"/>
    <item android:state_enabled="false" android:color="#ff000000"/>
    <item android:color="#ff0000ff"/>
</selector>

And here is the code for Dialer.axml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tableLayout1"
    android:background="#2ec0ff">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:layout_marginBottom="40sp"
        android:id="@+id/number"
        android:editable="false"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:gravity="right"
        android:textColor="#fff" />
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_weight="1">
        <Button
            android:text="1"
            android:layout_column="0"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="2"
            android:layout_column="1"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="3"
            android:layout_column="2"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="1">
        <Button
            android:text="4"
            android:layout_column="0"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="5"
            android:layout_column="1"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="6"
            android:layout_column="2"
            android:id="@+id/button6"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_weight="1">
        <Button
            android:text="7"
            android:layout_column="0"
            android:id="@+id/button7"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="8"
            android:layout_column="1"
            android:id="@+id/button8"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="9"
            android:layout_column="2"
            android:id="@+id/button9"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_weight="1">
        <Button
            android:text="*"
            android:layout_column="0"
            android:id="@+id/buttonStar"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="0"
            android:layout_column="1"
            android:id="@+id/button0"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <Button
            android:text="#"
            android:layout_column="2"
            android:id="@+id/buttonPound"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </TableRow>
    <Button
        android:text="Done"
        android:id="@+id/buttonDone"
        android:background="@color/done"
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:textSize="35sp"
        android:layout_margin="5sp" />
</TableLayout>

What is causing the error? Am I referencing done.xml incorrectly? Did I place it in the wrong folder?

Thanks in advance.

Daniel Bezden
  • 426
  • 3
  • 8
  • 20

1 Answers1

2

Your done.xml is color state list but color state lists can not be used as backgrounds. You need to use state-list-drawable for background.

Here is the state-list-drawable version of your color state list:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff2222ff" /> 
        </shape>
    </item>
    <item android:state_pressed="true" android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#ff4444ff" /> 
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#ff000000" /> 
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff0000ff" /> 
        </shape>
    </item>
</selector>

Put this into an xml file in your drawable file and make your layout button xml like this:

android:background="@drawable/done"

You can find similar question in this link: How to set custom button state background color?

Community
  • 1
  • 1
erkinyldz
  • 678
  • 1
  • 9
  • 18
  • Thank you, this works! However, for the second item, I had to change android:state_enabled to be "true", otherwise the color would not change at all. – Daniel Bezden Aug 15 '14 at 05:08
  • Yes I realized that was the case but did not want to change that thinking that it might be intentional :) – erkinyldz Aug 15 '14 at 07:25