6

I have seen many types of layout managers like:

  1. LineraLayoutManager
  2. RecyclerView.LayoutManager
  3. ListViewLayoutManager
  4. etc

What actually LayoutManager is and why it is used and what are the different types of LayoutManagers? Do in android all UI components like Button, TextView, EditText etc has their own LayoutManagers?

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Stack Overflow
  • 1,383
  • 9
  • 23
  • https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager – aga Jul 22 '19 at 19:10

3 Answers3

10

Adapters are only responsible for creating and managing views for items (called ViewHolder), these classes do not decide how these views are arranged when displaying them. Instead, they rely on a separate class called LayoutManager.

LayoutManager is a class that tells Adapters how to arrange those items. For example, you might want those items in a single row top to bottom or you may want them arranged in Grids like Gallery. Instead of writing this logic in your adapter, you write it in LayoutManager and pass that LayoutManager to View (RecyclerView).

A beginner might ask, Why does it work like that?
Answer: Flexibility. By using this pattern, whenever you want to change the arrangement of items, you don't have to modify Adapters or RecyclerView. Moreover, without this approach, you would be limited to the functionality provided by the pre-built class. You can build your own LayoutManager by extending the LayoutManager class.

There are also some commonly used LayoutManagers included for you to use, I'll list two of them

  • LinearLayoutManager Arranges items in 1 column or 1 row based on orientation. For example, a horizontal LinearLayoutManager will place items from left to right in 1 row.
  • GridLayoutManager Arranges items in a provided number of rows and columns, much like images in the gallery.
Rahul
  • 2,374
  • 2
  • 22
  • 36
Zohaib Amir
  • 3,108
  • 2
  • 9
  • 28
2

LayoutManager is a inner class of RecyclerView. According to documentation LayoutManager and it's subclasses are responsible for measuring and positioning item views in RecyclerView.

Known direct subclasses

LinearLayoutManager, StaggeredGridLayoutManager

Known indirect subclasses

GridLayoutManager, WearableLinearLayoutManager

So LinearLayoutManager allows RecyclerView to show data as a list and GridLayoutManager allows to organize items in scrollable tables/grids.

Answering your second question - no, other components usually do not have their own LayoutManager's, but there may be some exceptions.

0

The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers.

GridLayout: It arranges all the components in a grid of equally sized cells, adding them from the left to right and top to bottom. Only one component can be placed in a cell and each region of the grid will have the same size.

LinearLayout: It arranges all the components of the layout in a single column/row.