Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit Image into grid view

I am trying to fit the image into gridview. But I don't understand from where my gridview is getting the height of the view.

The cell of the grid view is smaller than the image. I want to display the image full in a cell. But I don't want to hard code the size of the ImageView.

My codes

gridView in xml

<GridView
    android:id="@+id/gridview_movie_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:numColumns="auto_fit"
    android:columnWidth="185dp"
    android:stretchMode="columnWidth"/>

My ImageView which I am inflating.

 <ImageView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:scaleType="centerCrop"/>

And my getView method in my adapter class

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
            mContext.LAYOUT_INFLATER_SERVICE);
    ImageView imageView;

    if (convertView == null) {
        imageView = (ImageView) inflater.inflate(R.layout.grid_view_item, null);
    } else {
        imageView = (ImageView) convertView;
    }

    Picasso.with(mContext).setLoggingEnabled(false);
    Picasso.with(mContext)
            .load(imageResource[position])
            .into(imageView);

    return imageView;
}

This is what I get

This is what I get

And this is what is required

enter image description here

like image 285
Saran Sankaran Avatar asked May 11 '16 23:05

Saran Sankaran


Video Answer


1 Answers

What worked for me was adding this line to my image view xml.

android:adjustViewBounds="true"

Now my output is just like what I wanted. Thanks for the help every one

like image 58
Saran Sankaran Avatar answered Oct 23 '22 10:10

Saran Sankaran