Xamarin : Simple Calculator Tutorial Visual Studio C#

Simple Calculator in Android (Xamarin) using VisualStudio C#

In Brief: This article will help you on building a simple calculator in android Xamarin with simple steps using C# Language in Visual Studio.
Detail:

Building this simple calculator will let you learn how to work with the Android Buttons, Text View and simple functions. The UI is not so beautiful but is a great way to learn the basic functionalities.



Steps:
1. Edit the Main.axml file.
The code in Main.axml file 

<?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="wrap_content"
        android:id="@+id/resultText"
        android:background="@android:color/darker_gray"
        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" />
    <EditText
        android:inputType="number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/resultText2"
        android:background="@android:color/darker_gray"
        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" />
    <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" />
        <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" />
        <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" />
        <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" />
    </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" />
        <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" />
        <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" />
        <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" />
    </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" />
        <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" />
        <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" />
        <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" />
    </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" />
        <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" />
        <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" />
        <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" />
        <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" />
    </LinearLayout>
</LinearLayout>


2. Preview the design in the designer mode in Visual Studio.
3. The code portion that will perform the events is written in the MainActivity.cs File.
4. Edit the MainActivity.cs File.
5. Add the following References

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.Data;

6. Write the code
The code in MainActivity.cs is below

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.Data;


namespace CalculatorApp
{
    [Activity(Label = "CalculatorApp", 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();
        }
    }
}


7. Build the app.

Complete Source code can be Downloaded from :-Download Cacluator App Android Source Code using Xamarin C#

Enjoy.... Happy Coding... :-)
Thanks for your time, although it is lengthier but is simple. Feel free to post any issues or Suggestions.


Comments