0

I'm trying to create a custom listview row color inside my adapter

Here is my xml

artists_list_backgroundcolor

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

and here is my code inside adapter

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.CategoryPreview, null, false);

        }

        TextView txtCategoryName = row.FindViewById<TextView>(Resource.Id.txtCategoryName);
        txtCategoryName.Text = mitems[position].CategoryName;
        TextView txtCategoryID = row.FindViewById<TextView>(Resource.Id.txtCategoryID);
        txtCategoryID.Text = mitems[position].CategoryID;

        row.SetBackgroundResource(Resource.Drawable.artists_list_backgroundcolor);

        return row;
    }

When activity is going to start i'm getting error

Android.Content.Res.Resources+NotFoundException: Drawable WiOrderAndroid.WiOrderAndroid:drawable/artists_list_backgroundcolor with resource ID #0x7f020053

It work only if i will set my xml with this way.

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

    </shape>
  </item>

</selector>

But is this the right way?

Community
  • 1
  • 1
testsc
  • 71
  • 7

2 Answers2

0

try to use R.drawable.artists_list_backgroundcolor instead

Mirza Ahmed Baig
  • 5,605
  • 3
  • 22
  • 39
  • It is the same in xamarin, i think something is going on with my xml\ – testsc May 17 '18 at 18:53
  • No problem i found a solution i will post it now. if i will set the shape inside my xml it works. But it is this method the right way? – testsc May 17 '18 at 19:00
0
<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:color="#6495ED" />
<item android:state_pressed="true" 
   android:color="#ffffff" />
<item android:state_selected="true"
 android:state_pressed="false" 
     android:color="#E5F5FA" />
</selector> 

From above codes, what you have defined is a ColorStateList, it should be in the res/color/artists_list_backgroundcolor.xml path, not res/drawable/ , and it should be referred by @color/artists_list_backgroundcolor or Resource.Color.artists_list_backgroundcolor.

What you need is a drawable selector. Please refer to this

Update:

SetBackgroundResource method:

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

The resource should be in drawable folder, but your artists_list_backgroundcolor file belongs to ColorStateList, it should be in the /res/color/ folder, not drawable folder.

If you want to use your artists_list_backgroundcolor file ,you need put it in the /res/color/ folder. But you can't use it by row.SetBackgroundResource(Resource.Drawable.artists_list_backgroundcolor);, please refer to this to use the ColorStateList.

Community
  • 1
  • 1
Robbit
  • 4,300
  • 1
  • 13
  • 29