1

I have an ImageView as a child of a ViewGroup that is part of a GridView. The drawable of the ImageView does not appear, however I can see the background color, and if I use the drawable in setBackgroundDrawable instead, it is displayed. I've tried changing every property and calling invalidate() but none have made the drawable appear.

The Log.i prints

 android.graphics.drawable.BitmapDrawable@407bd4c0

setImageResource(int) doesn't work either.

I'm using Honeycomb.

public MyViewGroup(Context context) {
    super(context);

    myImageView = new ImageView(context);
    myImageView.setBackgroundColor(Color.RED);
    myImageView.setScaleType(ImageView.ScaleType.CENTER);
    Drawable drawable = this.getResources().getDrawable(R.drawable.myimage);
    myImageView.setImageDrawable(drawable);
    Log.i("",drawable+"");
    //myImageView.setImageResource(R.drawable.myimage);
    addView(myImageView);
}

I omitted the onLayout code where I call setLeft() setTop() setRight() and setBottom().

Any help is much appreciated. Thanks.

Update: Also doesn't work with android.R.drawable images. I'm stumped.

Dave
  • 168
  • 1
  • 17

3 Answers3

1

Maybe the problem is, that you don't set the layout parameters. Try:

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, WRAP_CONTENT);
myImageView .setLayoutParams(params);
peter.bartos
  • 11,855
  • 3
  • 51
  • 62
  • I had tried that, also with fixed values. I see the ImageView where I set it, because I can see the backgroundColor, just not the image itself. – Dave Aug 12 '11 at 12:39
0

Try setting getting the image as bitmap, then setting it as drawable.

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myimage)
Drawable drawable = new BitmapDrawable(bmp);"
 myImageView.setImageDrawable(drawable);

Just out of curiosity: Why you do all of this in your constructor?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Setting the image drawable from a bitmap didn't work, nor setting the image as the bitmap directly. It is strange because if I add an ImageView to a LinearLayout in the main.xml GUI editor, the image appears fine. – Dave Aug 12 '11 at 13:01
  • I do it in the constructor just to make sure it works while I arrange my layout. It will be invisible in the constructor in the final implementation, and the image set when a property is set elsewhere in code. – Dave Aug 12 '11 at 13:05
0

If the parent layout has width/height of fill_parent, changing it to wrap_content might do the trick.

I was having this problem in Honeycomb using an ImageView with a drawable in a layout - the image was there judging from the width of the dialog I had it appearing in, but I couldn't see the image itself. The ImageView's parent was a LinearLayout with fill_parent. Switching to wrap_content fixed it right up.

whalabi
  • 1,675
  • 3
  • 12
  • 16
  • 1
    That didn't seem to help, but could be the issue. I had a thought that it might be drawing somewhere outside of the ImageView's bounds. When I set the background color it shows up exactly where I want it, just with no drawable. The workaround is to use setBackgroundDrawable instead of setImageDrawable or Resource. But then it always stretches to fill, and you lose the scaleType. Since I want to load different drawables with different sizes I can either resize them or the ImageView or deal with the stretching. – Dave Sep 15 '11 at 12:57