Cas
Cas

Reputation: 356

How to make an ImageView's height be the image's height? Android

I've got a GridView full of images. Here's my custom adapter:

    public class ImageAdapter extends ArrayAdapter<String> {

    private final String LOG_TAG = ImageAdapter.class.getSimpleName();

    public ImageAdapter(Activity context, List<String> urls){
        super(context, 0, urls);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String url = getItem(position);
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);

        ImageView img = (ImageView) rootView.findViewById(R.id.grid_view_item);
        img.setLayoutParams(
                new GridView.LayoutParams(
                        GridView.AUTO_FIT,
                        500));
        img.setScaleType(ImageView.ScaleType.FIT_XY);

        Picasso.with(getContext())
                .load(url)
                .into(img);

        return rootView;
    }
}


You can see that I set the height to be 500. But I don't want this.
I want the height to be the actual height of the image, just like the width.
How can I do that?

Upvotes: 3

Views: 53

Answers (1)

kris larson
kris larson

Reputation: 30985

Use adjustViewBounds property to force the height to the correct dimension.

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

Upvotes: 1

Related Questions