gridView height is too tall when using an imageView inside
Asked Answered
M

4

7

I'm having an issue with the height for the cells inside the GridView. Each of my cells will have an imageView inside like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:id="@+id/eventGridView" android:numColumns="2"/>
</LinearLayout>

Here is the ImageView for each cell

<com.example.scheduling_android.view.RoundedCornerImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:maxWidth="150dp"
        android:maxHeight="150dp"
        xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/eventImageView"
        android:adjustViewBounds="false"/>

I was playing around with different image size for my imageView. What I got was that whenever my image becomes larger (say, 400 x 400px) the height of the cell becomes very tall (around double the size of the width, note that I set the grid to use 2 columns). However, when I save my image as a smaller size (say, 160 x 160px), the gridView came out correctly. The height and the width matches.

Here's a screenshot:

enter image description here

What seems to be the problem? Could it be that the large image is causing gridView to calculate the height of its cell incorrectly? What can I do to fix this?

Marquetry answered 11/9, 2012 at 5:10 Comment(1)
how you solved this question? i have met the same problem now...Pesade
A
20

I think you haven't set adjustViewBounds to true. Can you set as follows?

imageView.setAdjustViewBounds(true);
Autopsy answered 17/6, 2014 at 15:24 Comment(0)
T
2

Here i have user 3 column for in each row and it works fine. Gallery view set the column width at run time,

mPhotoGalleryGridView.setColumnWidth(display.getWidth() / 3);

in layout

<GridView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/photogallerygridview"      
    android:padding="5dp"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:stretchMode="columnWidth"
    android:numColumns="3"
    android:gravity="center">    
</GridView>

in adapter: set imageview height and width,

imageview.setMaxHeight(150);
        imageview.setMinimumHeight(150);

Hope useful to you.

Thoth answered 11/9, 2012 at 5:23 Comment(0)
H
0

Use wrap_content instead of using fill_parent.

Because fill_parent will actually take original height of image and try to fill it in the screen. Even if u use 400x400 image and set layout_width = "160dp" and layout_height = "160dp" , it will work because we are not allowing image to display more than that.

Hexose answered 11/9, 2012 at 5:15 Comment(2)
It would be useful if you provided a reason as to why it is better to use one over the other.Succedaneum
because fill_parent will actually take original height of image and try to fill it in the screen.. even if u use 400x400 image and set layout_width = "160dp" and layout_height = "160dp" .it will work because we are not allowing image to display more than thatHexose
C
0

Try the below code. The width should be in dips

 android:adjustViewBounds="true"
 android:maxWidth="150dip"
 android:maxHeight="150dip"
Cheston answered 11/9, 2012 at 6:10 Comment(2)
I thought dip is the same as dp? #2025782Marquetry
I tried this, this doesn't work. GridView cell does not fit image height.Autopsy

© 2022 - 2024 — McMap. All rights reserved.