SIGN UP MEMBER LOGIN:    
ARTICLE

Simple Calculator Using Mono for Android

Posted by Manish Tewatia Articles | Android Programming August 02, 2011
In this article you will learn how to create simple calculator using Mono for Android.
Reader Level:

Here in this article you will learn how to create a simple calculator application using Mono for Android The calculator will have the following functionality:

  • Addition
  • Subtraction
  • Division
  • Multiplication

If you want to see how the XML is going to be built step by step you need to use DroidDraw it is a great tool for rapid prototyping of Google Android user interfaces, learn more and download DroidDraw from here (http://www.c-sharpcorner.com/Blogs/6006/droiddraw-for-android-applications.aspx).

Steps to create UI for Calculator

Step 1: Run the DroidDraw.

droiddeaw for android

Step 2: By default it have AbsoluteLayout and we also using AbsoluteLayout here because AbsoluteLayout is using when we want to place each object in an absolute position on the screen.
As per your requirement, if you want change the  background color as I do here.

background property in droiddraw

Step 3: Drag the TextView, EditView and Button Widget from the right panel and drop it in the left panel. We repeat this as many times as we need.

drag widgets in droiddraw

Step 4: Go to properties button and set the all required properties.

set property in droiddraw

Step 5: Now generate the XML code of this UI by just pressing the "Generate" button in the "Output" panel. At this point the XML code will be shown.

generate script in droiddraw

Step 6: Now copy this code and use it in our project.

copy script in droiddraw

Steps to create Calculator in Mono for Android

  1. Create a new Mono for Android application named "Calculator".

    mono for android application
     
  2. Go to main.xml file inside Resources/Layout and overwrite all existing code by pasting the DroidDraw copied code.

    main.axml page in android application
    pest the scrit in main.axml page
     
  3. Here is the complete code of main.xml file.

    <?xml version="1.0" encoding="utf-8"?>
     
    <AbsoluteLayout
     
    android:id="@+id/widget0"
     
    android:layout_width="fill_parent"
     
    android:layout_height="fill_parent"
     
    android:background="#808080ff"
     
    xmlns:android="http://schemas.android.com/apk/res/android"
     
    > 
     
      <TextView
     
      android:id="@+id/Rlable"
     
      android:layout_width="wrap_content"
     
      android:layout_height="wrap_content"
     
      android:textColor="#ffffffff"
     
      android:text="Result"
     
      android:layout_x="11px"
     
      android:layout_y="37px"
     
    />
       <
    EditText
     
      android:id="@+id/Redittbox"
     
      android:layout_width="240px"
     
      android:layout_height="wrap_content"
     
      android:textSize="18sp"
     
      android:layout_x="70px"
     
      android:layout_y="28px"
     
    />
       <
    TextView
      
    android:id="@+id/Fvalue"
     
      android:layout_width="wrap_content"
     
      android:layout_height="wrap_content"
     
      android:text="Enter First Value"
     
      android:layout_x="11px"
     
      android:layout_y="107px"
     
    />
       <
    TextView
     
      android:id="@+id/Svalue"
     
      android:layout_width="wrap_content"
     
      android:layout_height="wrap_content"
     
      android:text="Enter Second Value"
     
      android:layout_x="12px"
     
      android:layout_y="155px"
     
    />
       <
    EditText
     
      android:id="@+id/FVeditbox"
     
      android:layout_width="67px"
     
      android:layout_height="wrap_content"
     
      android:textSize="18sp"
     
      android:layout_x="180px"
     
      android:layout_y="99px"
     
    />
       <
    EditText
     
      android:id="@+id/SVeditbox"
     
      android:layout_width="67px"
     
      android:layout_height="wrap_content"
     
      android:textSize="18sp"
     
      android:layout_x="179px"
     
      android:layout_y="146px"
     
    />
       <
    TextView
     
      android:id="@+id/OPlable"
     
      android:layout_width="wrap_content"
     
      android:layout_height="wrap_content"
     
      android:text="Select the Operation"
     
      android:textSize="16sp"
     
      android:layout_x="77px"
     
      android:layout_y="216px"
     
    />
       <
    Button
     
      android:id="@+id/addition"
     
      android:layout_width="46px"
     
      android:layout_height="wrap_content"
     
      android:text="+"
     
      android:textSize="20sp"
     
      android:layout_x="18px"
     
      android:layout_y="266px"
     
    />
       <
    Button
     
      android:id="@+id/subtraction"
     
      android:layout_width="46px"
     
      android:layout_height="wrap_content"
     
      android:text="-"
     
      android:textSize="20sp"
     
      android:layout_x="91px"
     
      android:layout_y="266px"
     
    />
       <
    Button
     
      android:id="@+id/multiplication"
     
      android:layout_width="46px"
     
      android:layout_height="wrap_content"
     
      android:text="*"
     
      android:textSize="20sp"
     
      android:layout_x="167px"
     
      android:layout_y="266px"
     
    />
       <
    Button
     
      android:id="@+id/division"
     
      android:layout_width="46px"
     
      android:layout_height="wrap_content"
     
      android:text="/"
     
      android:textSize="20sp"
     
      android:layout_x="241px"
     
      android:layout_y="266px"
     
    />
       <
    TextView
     
      android:id="@+id/error"
     
      android:layout_width="wrap_content"
     
      android:layout_height="wrap_content"
     
    />
     </
    AbsoluteLayout>

  4. Now go to Activity1.cs file and inside the OnCreate() method we will creating and initialize all variables and create function for all theclick's.

    avtivity1 class in android application

    oncreate methode of android application

     
  5. First associate with all widget which we create in the XML code.

    Button btnadd = FindViewById<Button>(Resource.Id.addition);
    Button btnsub = FindViewById<Button>(Resource.Id.subtraction);
    Button btnmult = FindViewById<Button>(Resource.Id.multiplication);
    Button btndiv = FindViewById<Button>(Resource.Id.division);
     
    EditText firstvalue = FindViewById<EditText>(Resource.Id.FVeditbox);
    EditText secondvalue = FindViewById<EditText>(Resource.Id.SVeditbox);
    EditText result = FindViewById<EditText>(Resource.Id.Redittbox);

    TextView errormsg = FindViewById<TextView>(Resource.Id.error);
     

    Here we create variable for all the widget's and associate this variable to the widget by using
    FindViewById<widget_type>(Resource.Id.widget_name).
     
  6. Now we create three variable for the operation's and create click event for all the buttons. Like the example below:

    double a, b, c;

    Button_name.Click += delegate
    {
        try
       
    {
            // To Do....
        }
        catch (Exception ex)
        {
            // To Do....
        }
    };
     
  7. Here is the complete code of Activity1.cs file.

    using System; 
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    namespace Calculator
    {
        [Activity(Label ="Calculator", MainLauncher =true, Icon = "@drawable/icon")]

        public classActivity1 : Activity
        { 
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
     
                SetContentView(Resource.Layout.Main);
     
                Button btnadd = FindViewById<Button>(Resource.Id.addition);
                Button btnsub = FindViewById<Button>(Resource.Id.subtraction);
                Button btnmult = FindViewById<Button>(Resource.Id.multiplication);
                Button btndiv = FindViewById<Button>(Resource.Id.division);
     
                EditText firstvalue = FindViewById<EditText>(Resource.Id.FVeditbox);
                EditText secondvalue = FindViewById<EditText>(Resource.Id.SVeditbox);
                EditText result = FindViewById<EditText>(Resource.Id.Redittbox);

                TextView errormsg = FindViewById<TextView>(Resource.Id.error);

                double a, b, c;

                btnadd.Click += delegate
                {
                    try
                    {
                        a = double.Parse(firstvalue.Text);
                        b = double.Parse(secondvalue.Text);
                        c = a + b;
                        result.Text = c.ToString();
                    }
                    catch (Exception ex)
                    {
                        errormsg.Text = ex.Message;
                    }
                };

                btnsub.Click += delegate
                {
                    try
                    {
                        a = double.Parse(firstvalue.Text);
                        b = double.Parse(secondvalue.Text);
                        c = a - b;
                        result.Text = c.ToString();
                    }
                    catch (Exception ex)
                    {
                        errormsg.Text = ex.Message;
                    }
                };

                btnmult.Click += delegate
                {
                    try
                    {
                        a = double.Parse(firstvalue.Text);
                        b = double.Parse(secondvalue.Text);
                        c = a * b;
                        result.Text = c.ToString();
                    }
                    catch (Exception ex)
                    {
                        errormsg.Text = ex.Message;
                    }
                };

                btndiv.Click += delegate
                {
                    try
                    {
                        a = double.Parse(firstvalue.Text);
                        b = double.Parse(secondvalue.Text);
                        c = a / b;
                        result.Text = c.ToString();
                    }
                    catch (Exception ex)
                    {
                        errormsg.Text = ex.Message;
                    }
                };

            }
        }
    }
     
  8. Now Build and compile the application, the output looks like the below picture.

    output of calculator in android

Now enter the both values

         input values for android calculator

Now select operation which you want to perform, like I select Addition first and the result will come out in the Result EditBox

        addition in android application

Now select Subtraction and result will change to subtracted result

        subtraction in android application

Now select Multiplication the multiplication of both values will shows
       
        multiplication in android application

Now click on Division sign the output will comes in result box

        division in android application

Hope you like this article. Post your inspirational comments and feedback so that I can improve myself. 

Thank You,

Login to add your contents and source code to this article
share this article :
post comment
 

hey manish, definitely its a nice article for a beginer..tell me one thing how to deploy this application after developed using vs2010 to our android mobile.

Posted by mahesh kumar B M Oct 10, 2011

Hi Hemant, Thank You Hemant for liking my article, write-now there is no book for mono for android for developing android app using mono, get in touch with c-sharocorner our website or check the mono-site http://android.xamarin.com/Tutorials

Posted by Manish Tewatia Aug 16, 2011

heyy..manish..i liked ur article very much..it gives me inspiration to develop apps..thanx.. i want to know more about developing and also can u mail me some reference ebooks from which u hav studied.. My email id is hemant.bohra@rocketmail.com

Posted by Hemant Bohra Aug 13, 2011

Thanx, for liking my work and appreciation from readers will always encourage authors. So, if you like my upcoming articles then plz give your feedbacks.

Posted by Manish Tewatia Aug 02, 2011

Hi, Manish it looks so interesting to work on Android after reading article. Plz keep going...

Posted by Kayleigh Aug 02, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor