user601367
user601367

Reputation: 2368

What is the difference between linear and relative layout?

What is the difference between linear and relative layout?

Upvotes: 7

Views: 32158

Answers (10)

Muradtheoz
Muradtheoz

Reputation: 580

RelativeLayout is more flexible than LinearLayout but if you have proper knowledge about LinearLayout you Can use that too. For LinearLayout every attribute has a significant position hardcoded by the developer. For RelativeLayout you can change the position by relating with others attribute.

Upvotes: 0

PraneetNigam
PraneetNigam

Reputation: 997

Linear Layouts

  1. Linear Layouts are great for aligning views in rows and columns.
  2. They are a good way to divide up one place using layout weights that will expand or shrink views depending on the size of the Display.

Relative Layouts

  1. Relative Layouts are great for positioning elements relative to one another.
  2. For example putting B below A or putting C in the lower left hand corner.Check the Screen shoot
  3. Relative layout also make it easy to overlap views. For Example : view A is overlapping view B. Check the Screen-Shoot

Upvotes: 0

Harshit Srivastava
Harshit Srivastava

Reputation: 21

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.

Upvotes: 2

Samarth Shah
Samarth Shah

Reputation: 1131

The difference between linear and relative layout in android is that in linear layout, the "children" can be placed either horizontally or vertically, but, in relative layout, the children can be placed with relative distance from each other. This is the difference between linear and relative layouts.

Upvotes: -1

GOKULDAS
GOKULDAS

Reputation: 1

In the relative layout ,all the content in the layout page is related to other contents in the example_layout.xml page

In the case of Linear Layout the elements are displayed in the linear format

Upvotes: -1

AADProgramming
AADProgramming

Reputation: 6345

One of the characteristic feature of LinearLayout in Android is use of a property called Weight, which app can specify using android:layout_weight. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen.

On the other hand, RelativeLayout do not support weight or in other words, RelativeLayout does not pay attention to android:layout_weight. That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.

Upvotes: 0

Devrath
Devrath

Reputation: 42824

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.

Upvotes: 15

Colcut
Colcut

Reputation: 1

The following link should explain visually how the layouts work "Visually"
http://www.droiddraw.org/
Add some components to the window and mess with layouts to see what happens this is how I learned what each one does.

Upvotes: -1

Quintin Robinson
Quintin Robinson

Reputation: 82335

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)

Upvotes: 4

Vagrant
Vagrant

Reputation: 1716

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.

Upvotes: 10

Related Questions