I have 12 basically identical views which I want to arrange in a grid that covers the whole screen. Depending on the device's orientation, I want to use a 3x4 or a 4x3 grid.
As far as I understand, there are basically three approaches to this topic:
- Use a
GridView
- Use nested
LinearLayout
instances - Use a
TableLayout
I'd like to have a layout that
- automatically adapts to orientation changes (as
GridView
does) - uses all available screen space (as nested
LinearLayout
instances do) - doesn't allow scrolling (and without that "can't scroll any further" effect of the
GridView
) - allows me to force the same size on all of my items
By default, GridView
has scrolling and doesn't fill the screen, whereas LinearLayout
and TableLayout
don't automatically adapt to orientation changes.
Currently I'm using a GridView
with disabled scrolling and a custom adapter which sets the item views' minimum height depending on the orientation and the container's height to force a filled screen. This works but feels like a really ugly hack.
Dynamically constructing nested LinearLayout
instances depending on the orientation would probably also work, although I haven't tried that.
This seems to be a frequent goal (1, 2, 3, 4), but all the suggested solutions are either as hackish as mine or don't satisfy some of my requirements.
As I'm new to Android development I'm not sure whether I'm missing something.
What is the optimal way of implementing this?
I'm targeting API level 8 and above.