Requesting resource xyz failed because it is complex

Trying to set the background image (drawable) of a View programatically I ran into this ‘complex‘ error.

Weird…it’s just an id!

First, what worked…then a few things I tried that didn’t work…

What Worked

TypedArray array = view.Context.ObtainStyledAttributes(Resource.Styleable.AppTheme);
var image = array.GetDrawable(Resource.Styleable.AppTheme_ic_action_dice1);

Some explanation…

  1. view is a View type variable.
  2. AppTheme is the name of the theme defined in the attrs.xml file.
  3. The important thing to note here is the use of Resource.Stylable.
  4. I can’t directly reference the Drawable image because there are about 5 versions of the same image across the drawable-hdpi, drawable-ldpi etc folders. There is ONE entry in the attrs.xml file for each of the images (i.e. not one for each size), they are part of the theme.

So it seems to access the ‘correct’ resourceId of the Drawable, you have to first load up the theme ‘TypedArray’ and do a GetDrawable against that.

Makes sense now I finally figured out what the hell is going on but I couldn’t find any blogs to guide in the right direction, it was all trial and error…thus this post 🙂

What Caused the ‘Complex’ error?

I also tried the following which were dead ends…

Logger.Debug(“Fails because too complex…”);

Logger.Debug(“Fails because not found…”);
// Exception of type ‘Android.Content.Res.Resources+NotFoundException’ was thrown.

One – I thought the properties in Resource.Attribute would be what I needed. There was only one entry per image, that is essentially how I was setting my default (using background=”?attr/ic_action_dice1″ ) in the .axml layout file. But no, using that resource causes the complex error. Don’t know why…curious to know but no time to investigate…

Two – I thought that since Resource.Styleable.AppTheme_someImage was a valid resource to use, that I could make a call to SetBackgroundResource and directly use that resourceId. Can’t find it. Even though as you can see the resourceId is exactly the same as what works.

So it seems you DO need to create the Drawable first…then call SetBackgroundDrawable.

Not sure if that is a Xamarin limitation…seems to be, otherwise why does SetBackgroundResource exist?

Or maybe I’m doing something wrong still? If you are in the know here…please share 🙂



  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: