1

I'm facing this (for me) strange issue. After a couple of years of Xamarin Studio, I've opted to work with the plugin for VisualStudio 2015. I'm currently working on a project for field maintenance, so this is the first I've migrated. After having resolved some problems with the RecyclerView and CardView widgets, I'm facing this new issue, never seen with Xamarin Studio. I have this simple layout for a ViewHolder item, that uses a selector as background. This is the selector (res/color/item_bg_selector.xml) :

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="#fffff2e6" />
    <item android:state_selected="true" android:color="#ffabfdc3" />
    <item android:color="@android:color/transparent" />
</selector>

This is the item layout (res/layout/rowSpare.xml) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:background="@color/item_bg_selector">
    <TextView
        android:id="@+id/tvSpareCode"
        android:layout_width="0dp"
        android:layout_weight="@integer/spareCode"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/tvSpareDescription"
        android:layout_width="0dp"
        android:layout_weight="@integer/spareDescPrice"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/tvSparePrice"
        android:layout_width="0dp"
        android:layout_weight="@integer/sparePrice"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textColor="@color/black"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/tvSpareQuantity"
        android:layout_width="0dp"
        android:layout_weight="@integer/spareQuant"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textColor="@color/black"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

This is the excerpt from my custom adapter where the error arises :

    public class adSpareList : RecyclerView.Adapter
    {
....
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.rowSpare, parent, false);
            itemView.Clickable = true;
            SpareItemViewHolder vh = new SpareItemViewHolder(itemView, OnClick);

            return (vh);
        }
    }

Whenever executed, the first line of the OnCreateView gives this error :

Unhandled Exception
Android.Views.InflateException: Bonary XML file line #1: Error inflating class <unknown>.

without other info. The same layout worked well on Xamarin Studio. If I remove android:background="@color/item_bg_selector", or I substitute it with a simple android:background="@android:color/transparent" the whole thing works without error, but it's not the result I achieved before. Any help would be very appreciate.

Rodolfo.

pallares
  • 160
  • 4
  • 13
  • Where did you put your selector? If it is under the drawable folder, maybe you can try `@drawable/item_bg_selector`. – Grace Feng Apr 05 '17 at 02:22

1 Answers1

0

I don't know how that is working in Xamarin and same thing doesn't working in Visual Studio but anyway you are using color state list but color state lists can't be used as background. You have to create state list drawable instead for background. So change your code to something like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true">
     <shape android:shape="rectangle">
        <solid android:color="#fffff2e6" /> 
    </shape>
</item>
<item android:state_selected="true">
    <shape android:shape="rectangle">
        <solid android:color="#ffabfdc3" /> 
    </shape>
     </item>
  <item android:color="@android:color/transparent" />
 </selector>

Refer to the developer page: https://developer.android.com/guide/topics/resources/color-list-resource.html

And similar posted question: How to set custom button state background color?

Community
  • 1
  • 1
Yupi
  • 4,402
  • 3
  • 18
  • 37
  • You are right. Actually under Xamarin Studio the xml was a drawable list (each drawable was a simple color); perhaps it doesn't work with simple colors becaus it's a LinearLayout background. Anyway I had to remove the default item with "@android:color/transparent", as if I leave it I obtain the very same error. Thanks. – Rodolfo Redolfi Apr 05 '17 at 10:41