Close
Topic : User Interface - Linear Layout: Horizontal Orientation Vertical Orientation



Linear Layout:

The Linear Layout is the Most Basic Layout Manager Provided by Android. The Linear Layout Organizes the child views either Horizontally or Vertically based on the specified orientation property. The Value for Orientation property can be either -



Horizontal Vs Vertical: infobrother

Declaration:

Here is the Basic Syntax to Declare Linear Layout in Android:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
       android:orientation="vertical"     
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content">

<!-- add children here--> 

</LinearLayout>

Vertical Orientations:

In Vertical Orientations, The Child are arranged Vertically in a line one after the other. Below, we used Vertical Orientation, so the Childs of Linear Layout are displayed vertically.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical">
<!-- Vertical Orientation set -->

<!-- Child Views (In this case we have 3 Button) are here -->
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:id="@+id/button"
        android:background="#008080" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:id="@+id/button2"
        android:background="#184757" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:id="@+id/button3"
        android:background="#00b0f0" />
</LinearLayout>


Vertical Orientation: infobrother

Horizontal Orientations:

In Horizontal Orientations, The Child are arranged Horizontally in a line one after the other. Below, we used Horizontal Orientation, so the Childs of Linear Layout are displayed Horizontally.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
              
    android:orientation="horizontal">
<!-- horizontal Orientation set -->
  
<!-- Child Views (In this case we have 3 Button) are here -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button1"
        android:id="@+id/button"
        android:background="#008080" />
  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button2"
        android:id="@+id/button2"
        android:background="#184757" />
  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button3"
        android:id="@+id/button3"
        android:background="#00b0f0" />
</LinearLayout>



Horizontal Orientation: infobrother

NOTE: In Above Example, We also change Height and Width properties to display the Linear Layout Orientations. Read our XML Tutorial Layout Params to Learn how we can use Wrap_content and Fill_parent properties to control the layout and its child.


Example:

Simple Calculator Using Linear Layout:


Linear Layout Example : infobrother
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:inputType="text"
        android:layout_width="fill_parent"
        android:layout_height="250px"
        android:id="@+id/resultText"
        android:textColor="@android:color/background_dark"
        android:textSize="@android:dimen/app_icon_size"
        android:cursorVisible="false"
        android:clickable="false"
        android:editable="false"
        android:enabled="false"
        android:backgroundTint="#ff70efef"
        android:background="#70efef" />

    <EditText
        android:inputType="number"
        android:layout_width="fill_parent"
        android:layout_height="300px"
        android:id="@+id/resultText2"
        android:background="#70efef"
        android:textColor="@android:color/background_dark"
        android:textSize="@android:dimen/app_icon_size"
        android:cursorVisible="false"
        android:enabled="false"
        android:editable="false"
        android:clickable="false"
        android:backgroundTint="#ff70efef" />

    <LinearLayout
        android:id="@+id/wrapper1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wrapper0"
        android:weightSum="1.0">

        <Button
            android:text="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:id="@+id/btn1"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn1"
            android:layout_alignTop="@+id/btn1"
            android:layout_weight=".25"
            android:id="@+id/btn2"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn2"
            android:layout_alignTop="@+id/btn2"
            android:layout_weight=".25"
            android:id="@+id/btn3"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="DEL"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn3"
            android:layout_alignTop="@+id/btn3"
            android:layout_weight=".25"
            android:id="@+id/btnDel"
            android:backgroundTint="#ffff0000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/wrapper2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wrapper1"
        android:weightSum="1.0">
        <Button
            android:text="4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/wrapper2"
            android:layout_weight=".25"
            android:id="@+id/btn4"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn4"
            android:layout_alignTop="@+id/btn4"
            android:layout_weight=".25"
            android:id="@+id/btn5"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn5"
            android:layout_alignTop="@+id/btn5"
            android:layout_weight=".25"
            android:id="@+id/btn6"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="-"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn6"
            android:layout_alignTop="@+id/btn6"
            android:layout_weight=".25"
            android:id="@+id/btnSub"
            android:backgroundTint="#ffa52a2a" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/wrapper3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wrapper2"
        android:weightSum="1.0">
        <Button
            android:text="7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/wrapper3"
            android:layout_weight=".25"
            android:id="@+id/btn7"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn7"
            android:layout_alignTop="@+id/btn7"
            android:layout_weight=".25"
            android:id="@+id/btn8"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn8"
            android:layout_alignTop="@+id/btn8"
            android:layout_weight=".25"
            android:id="@+id/btn9"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="x"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn9"
            android:layout_alignTop="@+id/btn9"
            android:layout_weight=".25"
            android:id="@+id/btnMul"
            android:backgroundTint="#ffa52a2a" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/wrapper4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wrapper3"
        android:weightSum="1.0">
        <Button
            android:text="."
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn7"
            android:layout_weight=".20"
            android:id="@+id/btnDot"
            android:backgroundTint="#ffff7f50" />
        <Button
            android:text="0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnDot"
            android:layout_alignTop="@+id/btnDot"
            android:layout_weight=".20"
            android:id="@+id/btn0"
            android:backgroundTint="#ff008080" />
        <Button
            android:text="="
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn0"
            android:layout_alignTop="@+id/btnDot"
            android:layout_weight=".20"
            android:id="@+id/btnEql"
            android:backgroundTint="#ffa52a2a" />
        <Button
            android:text="/"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnEql"
            android:layout_alignTop="@+id/btnEql"
            android:layout_weight=".20"
            android:id="@+id/btnDiv"
            android:backgroundTint="#ffa52a2a" />
        <Button
            android:text="+"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnDiv"
            android:layout_alignTop="@+id/btnDiv"
            android:layout_weight=".20"
            android:id="@+id/btnAdd"
            android:backgroundTint="#ffa52a2a" />
    </LinearLayout>
</LinearLayout>
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.Data;


namespace CalculatorApp
{
    [Activity(Label = "Calculator App", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
           
            //Buttons to receive user input
            Button num1 = (Button)FindViewById(Resource.Id.btn1);
            Button num2 = (Button)FindViewById(Resource.Id.btn2);
            Button num3 = (Button)FindViewById(Resource.Id.btn3);
            Button num4 = (Button)FindViewById(Resource.Id.btn4);
            Button num5 = (Button)FindViewById(Resource.Id.btn5);
            Button num6 = (Button)FindViewById(Resource.Id.btn6);
            Button num7 = (Button)FindViewById(Resource.Id.btn7);
            Button num8 = (Button)FindViewById(Resource.Id.btn8);
            Button num9 = (Button)FindViewById(Resource.Id.btn9);
            Button num0 = (Button)FindViewById(Resource.Id.btn0);
            
            //Buttons that receive user mathematical operators
            Button equ = (Button)FindViewById(Resource.Id.btnEql);
            Button clr = (Button)FindViewById(Resource.Id.btnDel);
            Button dot = (Button)FindViewById(Resource.Id.btnDot);
            Button div = (Button)FindViewById(Resource.Id.btnDiv);
            Button mul = (Button)FindViewById(Resource.Id.btnMul);
            Button add = (Button)FindViewById(Resource.Id.btnAdd);
            Button sub = (Button)FindViewById(Resource.Id.btnSub);

            //text area to receive and display the user input
            EditText resu = (EditText)FindViewById(Resource.Id.resultText);
            
            //Text area to display the result generated after calculations
            EditText resu2 = (EditText)FindViewById(Resource.Id.resultText2);
            
            //Whenever the text in the EditText Changes the expression in the EditText is being computed.
            resu.TextChanged += delegate 
            {
                if (resu.Text == "")
                {
                    resu2.Text = "";
                }

                string x = resu.Text;
                try
                {
                    //Computation of the expression
                    double result = Convert.ToDouble(new DataTable().Compute(x, null));
                    resu2.Text = result.ToString();
                }
                catch (Exception exc)
                {
                    //No action to be performed
                }
            };

            num1.Click += delegate { resu.Text = resu.Text + num1.Text.ToString(); };
            num2.Click += delegate { resu.Text = resu.Text + num2.Text.ToString(); };
            num3.Click += delegate { resu.Text = resu.Text + num3.Text.ToString(); };
            num4.Click += delegate { resu.Text = resu.Text + num4.Text.ToString(); };
            num5.Click += delegate { resu.Text = resu.Text + num5.Text.ToString(); };
            num6.Click += delegate { resu.Text = resu.Text + num6.Text.ToString(); };
            num7.Click += delegate { resu.Text = resu.Text + num7.Text.ToString(); };
            num8.Click += delegate { resu.Text = resu.Text + num8.Text.ToString(); };
            num9.Click += delegate { resu.Text = resu.Text + num9.Text.ToString(); };
            num0.Click += delegate { resu.Text = resu.Text + num0.Text.ToString(); };

            dot.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(l - 1, 1);
                    if (x2 != ".")
                    {
                        if (x2 == "-" || x2 == "*" || x2 == "/" || x2 == "+")
                        {
                            string s1 = x.Substring(0, l - 1);
                            resu.Text = s1;
                        }
                        resu.Text = resu.Text + dot.Text.ToString();
                    }
                }
            };

            add.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(l - 1, 1);
                    if (x2 != "+")
                    {
                        if (x2 == "-" || x2 == "*" || x2 == "/" || x2 == ".")
                        {
                            string s1 = x.Substring(0, l - 1);
                            resu.Text = s1;
                        }
                        resu.Text = resu.Text + add.Text.ToString();
                    }
                }
            };
            sub.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(l - 1, 1);
                    if (x2 != "-")
                    {
                        if (x2 == "+" || x2 == "*" || x2 == "/" || x2 == ".")
                        {
                            string s1 = x.Substring(0, l - 1);
                            resu.Text = s1;
                        }
                        resu.Text = resu.Text + sub.Text.ToString();
                    }
                }
            };
            mul.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(l - 1, 1);
                    if (x2 != "*")
                    {
                        if (x2 == "-" || x2 == "+" || x2 == "/" || x2 == ".")
                        {
                            string s1 = x.Substring(0, l - 1);
                            resu.Text = s1;
                        }
                        resu.Text = resu.Text + "*";
                    }
                }
            };
            div.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(l - 1, 1);
                    if (x2 != "/")
                    {
                        if (x2 == "-" || x2 == "*" || x2 == "+" || x2 == ".")
                        {
                            string s1 = x.Substring(0, l - 1);
                            resu.Text = s1;
                        }
                        resu.Text = resu.Text + div.Text.ToString();
                    }
                }
            };
            clr.Click += delegate 
            {
                string x = resu.Text;
                int l = x.Length;
                if (l != 0)
                {
                    string x2 = x.Substring(0, l - 1);
                    resu.Text = x2;
                    if (x2.Length != 0)
                    {
                        string x3 = x2.Substring(l - 2, 1);
                        if (x3 == "+" || x3 == "-" || x3 == "*" || x3 == "/" || x3 == ".")
                        {
                            try
                            {
                                double result = Convert.ToDouble(new DataTable().Compute(x.Substring(0, l - 2), null));
                                resu2.Text = result.ToString();
                            }
                            catch (Exception exc)
                            {
                            }
                        }
                    }
                }
            };
            equ.Click += delegate 
            {
                if (resu2.Text != "")
                {
                    resu.Text = resu2.Text;
                    resu2.Text = "";
                }
            };

        }

        private void Resu_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
        {
            throw new System.NotImplementedException();
        }
    }
}


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement