Inflated ImageView to put in GalleryView isn't the right size

Posted by Richard on Stack Overflow See other posts from Stack Overflow or by Richard
Published on 2010-05-19T21:57:30Z Indexed on 2010/05/19 22:00 UTC
Read the original article Hit count: 402

Filed under:
|
|

I am trying to inflate an ImageView that scales a Drawable that I can display in a GalleryView. My code to inflate the view seems to work fine, except that the attributes of the ImageView are not applied. Specifically, the inflated ImageView does not have the width/height that I set for it via the android:layout params in XML.

Can someone show me what I'm doing wrong?

I want to set the width/height of the image in dp, so that it is the correct size across multiple screen dpis and support Android 1.5+. As a result I cannot use something like:

i.setLayoutParams(new Gallery.LayoutParams(150, 116)

My layout definition is:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dp" android:layout_height="116dp"
    android:background="@drawable/gallery_item_background"
    android:scaleType="fitXY" />
</ImageView>

And the snippet I am using to inflate the ImageView is:

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        ImageView i = (ImageView) inflater.inflate(R.layout.gallery_item, null);
        i.setImageResource(mImageIds.get(position));
        i.setScaleType(ImageView.ScaleType.FIT_XY);

        return i;
    }

© Stack Overflow or respective owner

Related posts about android

Related posts about imageview