27

I have a GridView that displays images, which are, unfortunately, of different sizes. They are shown in between two lines of text:

text1.1                text1.2
ImageView(IMAGE1)      ImageView(IMAGE2)
text2.1                text2.2

text3.1
ImageView(IMAGE3)
text4.1

etc....

If IMAGE1 is the same height as IMAGE2, everything is fine, but if IMAGE1 is longer than IMAGE2, text2.1 will run into text3.1 (padding doesn't seem to help much, as there's too much of it when images are of the same height).

I know there's a way to stretch the images in the ImageView so they are the same height, but is it possible to keep images as is and set the row height somehow?

kozyr
  • 1,334
  • 1
  • 20
  • 30

3 Answers3

28

You are in control over your row heights, by virtue of what you put in them. Since your cells appear to have more than one widget, they are presumably wrapped in a LinearLayout or something. Set your LinearLayouts to be some specific height, and the rows will all be that height.

Personally, I think you should be resizing your images if you are going to have text above and below each image on a per-cell basis.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I found out that out as well -- basically, I didn't see that you can set the width and height for a component in layout instead of wrap_component or fill_parent.... – kozyr Mar 05 '10 at 16:05
  • 27
    this is not working for me, no matter what I put on the linearlayout height the GridView behaves the same... – M Rajoy Jan 05 '13 at 10:08
  • layout_height for LinearLayout in terms of dimension values does not work. – lalitm Mar 21 '14 at 07:25
  • 7
    in the `LinearLayout`, don't set values to `wight` and `height`, but to `minWidth` and `minHeight`. – mbmc May 29 '14 at 19:03
  • 1
    This doesn't answer the root problem, that GridView only takes account of the last column when calculating the row height. – Nathan Adams Jul 24 '16 at 20:00
  • @NathanAdams Can you tell us what method is called once we reach the last column in order to calculate row height. Probably we can override that method and provide a fixed height with the tallest grid in the row. – ArtiomLK Nov 20 '16 at 02:22
  • @ArtiomLK sorry I've no idea, interesting idea though – Nathan Adams Dec 06 '16 at 20:31
  • Any tips on how to make one cell's height equal to the row's as determined by the layout of the other columns in that row? – androidguy Jul 21 '17 at 00:56
  • @user3175580: That sounds like something that `RecyclerView` would have a much better chance of handling. – CommonsWare Jul 21 '17 at 01:07
7

Use drawable as background on Layout that is your grid cell and define that drawable with:

<size android:height="<some height>" />

For example:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<solid android:color="#ffffff"/>
<size android:height="60dip" />
</shape>
CloudWalker
  • 313
  • 4
  • 12
5

Here is an alternative solution that pre-measures items and sets the height of each cell to the max height in that row.

GridView rows overlapping: how to make row height fit the tallest item?

Community
  • 1
  • 1
Chase
  • 11,161
  • 8
  • 42
  • 39