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

2

11

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;
    }
Scab answered 19/5, 2010 at 21:57 Comment(1)
For the moment I hacked a solution using DisplayMetrics.density. float den = mDisplayMetrics.density; convertView.setLayoutParams(new Gallery.LayoutParams((int)(den * 150), (int)(den * 116)));Scab
C
26

The trick is simply to use the following version of inflate():

inflater.inflate(R.layout.gallery_item, parent, false);

The last two parameters are mandatory. If you pass "null" as the parent, the inflater does not know what type of layout parameters to create and therefore ignores all the android:layout_ XML attributes. The last parameter simply tells the inflater to not add the inflated view to the parent right away. If you pass true (at least inside an Adapter's getView() method), bad things will happen.

Chinaware answered 27/6, 2010 at 9:28 Comment(0)
A
1

I'm not sure but this might work. Give an id to your imageview in your xml (say it is "@+id/image":

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

    return view;
}

By the way you should optimize it checking if convertView is null in order to recycle views. Check this: http://code.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html

Autoicous answered 19/5, 2010 at 22:31 Comment(2)
Great tip on recycling the view BTW.Scab
I made something similar but instead used a gridview with a custom layout per element: an imageview and a text below. Modifying the width and the height in the xml file was working for me. BTW are you sure there is no difference? try it to put something like 20dip for both and check if you see any changes, and after try some high value like 300dip.Autoicous

© 2022 - 2024 — McMap. All rights reserved.