LinearLayout vs RelativeLayout

71,222

Solution 1

Read this article:

The Android UI toolkit offers several layout managers that are rather easy to use and, most of the time, you only need the basic features of these layout managers to implement a user interface. Sticking to the basic features is unfortunately not the most efficient way to create user interfaces. A common example is the abuse of LinearLayout, which leads to a proliferation of views in the view hierarchy. Every view, or worse every layout manager, you add to your application comes at a cost: initialization, layout and drawing become slower. The layout pass can be especially expensive when you nest several LinearLayout that use the weight parameter, which requires the child to be measured twice...

In a RelativeLayout, views are aligned either with their parent, the RelativeLayout itself, or other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called alignWithParentIfMissing.

This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.

...the difference will be much more important when you use such a layout for every item in a ListView for instance...

Solution 2

By the name you can come to know LinearLayout adds view linearly either vertical or horizontal by the orientation set by you. It will add views one after another which will depend on the requirement of the design. And RelativeLayout adds view related with each other,there is no need to declare orientation in RelativeLayout.

As for example you want to add two TextViews one under another so you will add first TextView and refer the second TextView with the first TextView by adding android:layout_below="first textview's id" in the XML file. This way you can deal with RelativeLayout.

Share:
71,222
Soumyadip Das
Author by

Soumyadip Das

Java/Spring application Developer from Kolkata, India. Languages/Technology/Frameworks: Java/J2EE Servlet Spring Struts Web Tools: HTML JSP Flex XML json App server and DB: Tomcat MySQL Oracle Others: REST API Android Amazon AWS

Updated on July 05, 2022

Comments

  • Soumyadip Das
    Soumyadip Das almost 2 years

    What are the advantages of RelativeLayout over LinearLayout in android ? For a particular design which one would you prefer and what's the reason behind of that ??

    Is it(RelativeLayout) comparable or similar like HTML <div> ??

  • ashish.n
    ashish.n over 9 years