Linear Layout in Android with Program

 Android program to implement Linear Layout

Linear Layout in Android is a view group that aligns all children in either vertical or horizontal direction.You can specify the direction with the android: orientation attribute. This example will help you to understand the need for using Linear Layout and how do we use it while making Android applications. Before I jump on to the program I will be giving you some important attributes which you frequently use while implementing the Linear Layout.

Important Attributes

Below there is simple code which will show the working of Linear Layout, it is basically going to display the seven colors of the rainbow using vertical orientation(i.e one below the other). You just need to add this xml code below there will be no code added for the MainActivity except for setConetentView which is added by default when you create your app if you are using Android Studio.

Steps to create the application:- 

  •  Open Android Studio and create a new Android application and name it as “Vibgyor” and company domain as codedost so your package will be automatically set.
  • Open an empty activity and name it as MainActivity.
  • Copy the default content of res/layout/activity_main.xml file to include in Linear Layout.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:text="RED"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f00"
        android:layout_weight=".14"
        android:gravity="center"
        android:textColor="#000"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ORANGE"
        android:background="#ffa500"
        android:layout_weight=".14"
        android:gravity="center"
        android:textColor="#000"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="YELLOW"
        android:background="#ffff00"
        android:layout_weight=".14"
        android:gravity="center"
        android:textColor="#000"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GREEN"
        android:background="#0f0"
        android:layout_weight=".15"
        android:gravity="center"
        android:textColor="#000"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="BLUE"
        android:background="#00f"
        android:layout_weight=".14"
        android:gravity="center"
        android:textColor="#000"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="INDIGO"
        android:background="#4b0082"
        android:layout_weight=".15"
        android:gravity="center"
        android:textColor="#000"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="VIOLET"
        android:background="#ee82ee"
        android:layout_weight=".14"
        android:gravity="center"
        android:textColor="#000"/>

</LinearLayout>

MainActivity.java

package com.codedost.vibgyor;

import android.support.v7.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

Share Me!