What is the difference between linear and relative layout?

30,151

Solution 1

Linear layouts put every child, one after the other, in a line, either horizontally or vertically. With a relative layout you can give each child a LayoutParam that specifies exactly where is should go, relative to the parent or relative to other children.

Solution 2

LINEAR LAYOUT ::

  • In a linear layout, like the name suggests, all the elements are displayed in a linear fashion
  • Either Horizontally or Vertically and this behavior is set in android:orientation which is an attribute of the node LinearLayout.
  • Linear layouts put every child, one after the other, in a line, either horizontally or vertically.

Click here ---- for --- Android Docs reference for linear layout

Pictorial representation


RELATIVE LAYOUT::

  • In a relative layout every element arranges itself relative to other elements or a parent element.
  • It is helpful while adding views one next to other etc
  • With a relative layout you can give each child a LayoutParam that specifies exactly where is should go, relative to the parent or relative to other children.
  • Views are layered on top of each other in relative layout

Click here ---- for ---Android Docs reference for Relative layout

Pictorial representation


Optimization::Have a look at Optimizing Layout Hierarchies

The Fewer Views, the Better::

  1. The number one goal for your layouts should be using the fewest number of Views possible. The fewer Views you have to work with, the faster your application will run. Excessive nesting of Views further slows down your application.

  2. A RelativeLayout hierarchy will typically use fewer Views and have a flatter tree than a LinearLayout hierarchy. With LinearLayout, you must create a new LinearLayout every time you want to change the orientation of your views – creating additional Views and a more nested hierarchy. As a result, it is recommended that you first use RelativeLayout for any layout that has any complexity. There is a high probability you will reduce the number of Views – and the depth of your View tree – by doing so.

Solution 3

From Android developer documentation: Common Layout Objects

LinearLayout

LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute.

RelativeLayout

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID)

Solution 4

difference is simple: in LinearLayout we arrange stuff in linear manner (one after another), and in RelativeLayout we can place stuff anywhere on screen.

=> Linear Layout is arranged as a list. Rest they are similar in functionality.

Share:
30,151
user601367
Author by

user601367

Updated on November 15, 2020

Comments

  • user601367
    user601367 over 3 years

    What is the difference between linear and relative layout?

  • Anantha Raju C
    Anantha Raju C over 7 years
    elaborate your answer
  • Elletlar
    Elletlar over 3 years
    Hi Murad. This is a comment not really an answer. Also, this question already has many responses. Please try avoid providing additional answers to questions unless there is a unique solution to present to the community. Some tips for answering here Lastly, RelativeLayout is classified as a 'legacy' layout by Android Studio so developers should avoid it in favour of ConstraintLayout unless they are maintaining an out-dated legacy project. Kind Regards.