Linear Layout in Android

Bhavya Sharma
3 min readSep 7, 2020

In android, Layout is the basic building block for an application. It is responsible for holding all the User Interface(UI) elements that will be revealed to the user.

Layout has two main components View and ViewGroup.

View is the basic class behind every UI component. A view occupies a rectangular area and performs event handling and drawing.All UI components like TextView, EditText, Buttons are views that are created to make your app more interactive.

ViewGroup is the base class for all the layouts and view containers. A ViewGroup can be seen as a special view that contains other views.

The most commonly used ViewGroup classes :

  • Linear Layout
  • Relative Layout
  • Table Layout
  • Frame Layout
  • Coordinator Layout

In this article, we will learn about LinearLayout the most simple yet most useful layout. LinearLayout basically arranges all the views either horizontally or vertically.

Example of Linear Layout implemented in xml file

As you can see there are a few main attributes which we have defined these are :

layout_width: It is used to specify the overall width of the layout.
layout_height: It is used to specify the overall height of the layout.
orientation: It is used to specify whether child views are displayed in columns or rows.

In this article, we will list some of the attributes which are commonly used. You can check the official documentation to get an idea about all other attributes that you can use in your XML file.

XML Attributes :

  • layout_weigth: This attribute assigns an “importance” value to a view in terms of how much space it should occupy on the screen.
  • weightSum: Defines the maximum weight.
  • gravity: Specifies how an object should position its content, on both the X and Y axis, within its own bounds.
  • padding: It is the space inside the border, between the border and the actual view’s content.
  • layout_margin: It provide a visual buffer between a view’s content and any content outside of the view’s bounds.
  • baselineAligned: It prevents the layout from aligning children’s baselines when set to false.

There are many other attributes with which you can play to make your layout look more creative.

Now try to implement LinearLayout in our app. We will build a simple app having a single TextView and two buttons. Lets code!

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/resauto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"
android:text="Hello World" />
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"
android:text="Button One"/>
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"
android:text="Button Two" />
</LinearLayout>

Try to run this code and tell me your output in comment section. And also if you feel any problem share with me in comment box.

--

--