What is LinearLayout in android?

Hey, what’s up, guys. Android provides some layout to organize the views. Android has layouts like ConstraintLayout, RelativeLayout, FrameLayout, and LinearLayout. In this post, we will see what linear layout is in android.

What is LinearLayout in android?

LinearLayout is a view group that aligns all children (Views and layouts) in a single direction, vertically or horizontally.

In simple words, LinearLayout places views sequentially. Consider Your whole device screen as a single row then views will go vertically one after the other. Or, Consider your whole device screen as a single column then views will go horizontally one after the other.

To place the views vertically we set orientation attribute to vertical and to place the views horizontally we set the orientation attribute to horizontal.

Example with vertical orientation –

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/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="195dp"
        app:srcCompat="@android:drawable/ic_lock_lock" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="93dp"
        android:paddingStart="400px"
        android:text="Locked"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="83dp"
        android:text="Unlock"
        android:textSize="18sp" />
</LinearLayout>

MainActivity.java

package com.example.linearlayoutexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Output :

See the output above. I have placed 3 views vertically – ImageView, TextView, and Button. All three views are one after the other vertically. Notice the orientation. I am using vertical orientation here.

Now let’s see the same example with horizontal orientation.

Example with horizontal orientation –

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/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="136dp"
        android:layout_height="168dp"
        android:layout_gravity="center"
        app:srcCompat="@android:drawable/ic_lock_lock" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="121dp"
        android:layout_height="163dp"
        android:layout_gravity="center"
        android:paddingTop="200px"
        android:text="Locked"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="79dp"
        android:layout_gravity="center"
        android:text="Unlock"
        android:textSize="18sp" />
</LinearLayout>

MainActivity.java

package com.example.linearlayoutexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Output :

Here I have used horizontal orientation in the same example. All three views are placed horizontally one after the other.

That’s all about LinearLayout in android.

Goodbye.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: