229

I have to use a Grid to implement Photo Browser in Android. So, I would like to know the difference between GridView and GridLayout.

So that I shall choose the right one.

Currently I'm using GridView to display the images dynamically.

Lakshmi Sreekanth Chitla
  • 2,487
  • 2
  • 13
  • 16

1 Answers1

278

A GridView is a ViewGroup that displays items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.

This is what you'd want to use (keep using). Because a GridView gets its data from a ListAdapter, the only data loaded in memory will be the one displayed on screen. GridViews, much like ListViews reuse and recycle their views for better performance.

Whereas a GridLayout is a layout that places its children in a rectangular grid.

It was introduced in API level 14, and was recently backported in the Support Library. Its main purpose is to solve alignment and performance problems in other layouts. Check out this tutorial if you want to learn more about GridLayout.

Benito Bertoli
  • 23,530
  • 12
  • 51
  • 61
  • 16
    GridLayout is also a ViewGroup. – Dzmitry Lazerka Oct 08 '12 at 03:01
  • 22
    I never said it wasn't. I was just emphasizing on the fact that it is a layout. – Benito Bertoli Oct 08 '12 at 07:34
  • 2
    small addition: (good) adapters will load a little bit more than what is currently displayed onscreen so as to be able to provide that content as the user scrolls/moves to new, neighbouring content. – straya Apr 29 '13 at 11:22
  • 16
    @laki one scenario would be if you want to load a bunch of images in a Grid like formation. Using GridView would only load in memory the views visible on screen. Whereas if you use a GridLayout you would have to load all the images that will be displayed at anytime causing an outofmemory error in most of devices. – BigBen3216 Jun 06 '13 at 23:43
  • 3
    @BigBen3216 You wouldn't get an OutOfMemoryError if you use a ContentResolver to query the local data store, and scale your bitmaps with BitmapFactory and inSampleSize. – IgorGanapolsky Jan 10 '14 at 19:53
  • 16
    Basic difference seems to be that GridView can accommodate only uniform column width throughout. Grid Layout allows you to have content spanning 2 or more columns. Grid Layout allows you to design screens having content spanning different column sizes – Yasir Apr 24 '14 at 05:20
  • 62
    @laki Scenario one: Draw a calculator, GridLayout. Scenario two: Draw a gallery, GridView. – Mario Velasco Oct 27 '14 at 16:54
  • 1
    1. GridView is kind of equivalent to ListView / recycler view, where almost everything is dynamic and you would have an adapter attached to it. 2. Think of GridLayout as like scrollView where you would have to add child manually and give their position / margin / numOfColumns / rows – Alex Jun 04 '17 at 16:12