Friday, 14 February 2020

What is difference between Linear layout and Relative layout in android XML

Both layout are used for UI design for Android Application. both layout are main layout and other widget are child layout or child item like Text View, View, Edit Text and etc. Only main difference listed not all. also listed similarity between both.

1. Linear Layout :
 a). In this layout user can set it's child item in one direction like vertically or horizontally.
 b). In this layout all child show after top or left item.
 c). In this layout user set orientation like vertically or horizontally for his/her
       require manage UI  design.
 d). In this layout set position of layout item set gravity and layout gravity like center,
      start, end ,top and bottom.

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="@dimen/activity_horizontal_margin">
    <Button
        android:id="@+id/button_back"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_title"
        android:text="Title"
        android:layout_width="wrap_content"
        android:textSize="18sp"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_next"
            android:text="Next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/text_value"
            android:text="Value"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

2. Relative Layout :
 a). In this layout user can set it's child item in multiple direction like vertically or horizontally.
      using id of widget set toLeft, toRight, toTop, toBottom.
 b). toLeft is deprecated  and change to toStart, toRight is also deprecated and
      change to toEnd.
 c). In this layout set horizontally or vertically only not like top or start or below or end etc.
      like layout_centerHorizontal is true or false and similar in vertically
      layout_centerVertical is true or false.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/button_back"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_first_name"
        android:text="First Name"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_back" />
    <TextView
        android:id="@+id/text_last_name"
        android:text="JournalDev"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text_first_name"
        android:layout_below="@id/button_back"/>
</RelativeLayout>

and Similarity between both layout below listed.

1). Both layout have many similarity between both layout.
  a). Width, height and many parameter also compulsory.
  b). Margin for top, bottom, start, and end.
  c).  Padding for top, bottom, start and end.
and also many similarity so I am only list out main point.

Thanks.
 

2 comments:

  1. Very nice valuable information

    ReplyDelete
    Replies
    1. Thanks dear. Please follow this blog for more details information about android application development problems.

      Delete