Difference Between LinearLayout and RelativeLayout in Android - GeeksforGeeks

07 Dec, 2021
LinearLayout is a type of view group which is responsible for holding views in it either Horizontally or vertically. It is a type of Layout where one can arrange groups either Horizontally or Vertically.
Example Diagram:
Syntax:
XML
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="either vertical or horizontal"> <!--ImageView, TextView, ButtonView etc.--> </LinearLayout>
RelativeLayout is a layout in which we can arrange views/widgets according to the position of other view/widgets. It is independent of horizontal and vertical view and we can arrange it according to one’s satisfaction.
Example Diagram: 
Syntax:
XML
<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"> <!--ImageView, TextView, ButtonView    etc with specified position-->     </RelativeLayout>
LinearLayout   
RelativeLayout   
layout_weight attribute in the linear layout is used to specify the equal or specific size to the particular widget and view by using the following attribute.
android:layout_weight = ‘0’ 
Here Weight is specified as 0 in order to give equal size or space to each view or widget.
Syntax:
<LinearLayout>  
<!–Views, widgets–>
</LinearLayout>
Syntax:
<RelativeLayout>
<!–Views, Widgets–>
</RelativeLayout>
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/white"    android:orientation="horizontal">     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginLeft="35dp"        android:layout_marginTop="20sp"        android:layout_marginRight="10sp"        android:layout_weight="0"        android:background="#004d00"        android:text=" Geeks"        android:textColor="#ffffff"        android:textSize="40sp"        android:textStyle="bold" />     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="20sp"        android:layout_marginRight="10sp"        android:layout_weight="0"        android:background="#f2f2f2"        android:text="For"        android:textColor="#004d00"        android:textSize="40sp"        android:textStyle="bold" />     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="20sp"        android:layout_marginRight="10sp"        android:layout_weight="0"        android:background="#004d00"        android:text="Geeks"        android:textColor="@color/white"        android:textSize="40sp"        android:textStyle="bold" />     </LinearLayout>
Output: 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/white">     <ImageView        android:id="@+id/image_gfg"        android:layout_width="100dp"        android:layout_height="110dp"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:layout_marginRight="10dp"        android:scaleType="centerCrop"        android:src="@drawable/gfg" />     <TextView        android:id="@+id/gfg_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_toRightOf="@id/image_gfg"        android:paddingTop="5dp"        android:text="Geeks For Geeks"        android:textColor="#004d00"        android:textSize="32sp"        android:textStyle="bold" />     <TextView        android:id="@+id/gfg_location"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/gfg_text"        android:layout_marginLeft="10dp"        android:layout_toRightOf="@id/image_gfg"        android:text="Noida,UttarPradesh"        android:textColor="#00b300"        android:textSize="25sp" />     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/gfg_location"        android:layout_marginLeft="10dp"        android:layout_toRightOf="@id/image_gfg"        android:text="Portal for CS Student"        android:textColor="#009900"        android:textSize="24sp" />     </RelativeLayout>
Output: 
 
sonuckpacc
kk9826225
android
Technical Scripter 2020
Android
Difference Between
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Content Providers in Android with Example
How to change the color of Action Bar in an Android App?
How to Read Data from SQLite Database in Android?
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Differences between IPv4 and IPv6
Difference Between Method Overloading and Method Overriding in Java