tags:

views:

183

answers:

1

Hi,

I have a layout with an ImageView defined like:

<ImageView
  android:layout_width="45dip"
  android:layout_height="45dip"
  android:scaleType="fitXY" />

now I just want to set the imageview to be a static color, like red or green. I'm trying:

ColorDrawable cd = new ColorDrawable("FF0000");
cd.setAlpha(255);
ImageView iv = ...;
iv.setImageDrawable(cd);

the imageview is just empty though, no color. The 45dip space is being used up though. What do I need to do to get the color to be rendered?

Thanks

A: 

Looking at the constructor for ColorDrawable I don't see a version that takes a string like in your example. I see one that takes an int. Try this:

ColorDrawable cd = new ColorDrawable(0xffff0000);

Notice I used 8 hex digits, not 6 like in your example. This sets the alpha value as well.

Edit: Looking back at some of my own code where I've done something similar, I always used setBackgroundDrawable() instead of setImageDrawable() to initialize an ImageView with a solid color. Not sure if that would make a difference.

mbaird
Thanks the color int I was supplying was off, I used Color.parseColor() and now it works.