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
Steps to create UI for
Calculator
Steps to create Calculator in
Android
-Create a new Mono for Android application named "Calculator".
Here is the 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>
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.<?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>
Here is the 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;
}
};
}
}
}
Now Build and compile the application, the output looks like the below
pictureusing 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;
}
};
}
}
}
out put:
Excellent work! I am really impressed by this incredible precious site.This post is really very very useful.
ReplyDeleteandroid application development