How to Divide LinearLayout in Android Equally

In this article, I will show you How to Divide LinearLayout in Android Equally. I use Android Studio version 3.6.1. Build #AI-192.7142.36.36.6241897, built on February 27, 2020. Runtime version: 1.8.0_212-release-1586-b4-5784211 amd64. VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Powered by open-source software.

Follow this article step by step to create linear layout in Android:

  1. Create a project in Android Studio.
  2. Select Empty Activity when Select a Project Template window appears, then click Next.
  3. Then give the Name of your project when Configure Your Project window appears, then click Finish. In this example, I give the name Divide Equally.

After that, your screen will appear as shown below:

How to Divide LinearLayout in Android Equally
Divide LinearLayout in Android Equally

XML for Divide LinearLayout Equally

  1. Change the layout from Constraint Layout to Linear Layout.
  2. Add this: android: orientation = “vertical”. Please see the code below for more understanding.
<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"
    >
  1. Add TextView
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="ROW 1"
        android:layout_weight="1"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="ROW 2"
        android:layout_weight="1"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="ROW 3"
        android:layout_weight="1"
        />

For example, after adding 3 TextView, here is how it looks:

Vertical Divide
  1. Now we change it to horizontal.
<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"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 1"
        android:layout_weight="1"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 2"
        android:layout_weight="1"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 3"
        android:layout_weight="1"
        />

</LinearLayout>
  1. You can change the value of the android:layout_weight=”2″
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 1"
        android:layout_weight="2"
        />

Then click Run app. The display will be like this.

Divide LinearLayout in Android Equally Finish
Divide LinearLayout in Android Equally Finish

To change the color add: android:background=”#FFFF00″

<?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"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 1"
        android:layout_weight="2"
        android:background="#FFFF00"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 2"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="COLUMN 3"
        android:layout_weight="1"
        android:background="#5EA538"
        />

</LinearLayout>

You can add the background to the code. Read this article android studio gradient background.

Hope this article helpful for many people. If this article useful for you, you can share to other.

Thank you.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *