Calculator
Calculator
Introduction:
If you don’t know how to built then you are for treat as we are going
to share how the App is created in Android. This is the one of
simplest App you can create to understand Android basics. In this
Calculator App tutorial we are going use of multiple Android UI
components to design and step by step developing a Basic Calculator
application in Android Studio.this article.
Do you know when was the first ever mechanical calculator created?
The Calculators that we use today were never the same earlier. The
first calculator was invented in the year 1642 by Blaise Pascal when
he was 19 years old. Okay, now let us begin with our project without
any further delay!!
1
Algorithm:
Step 1: Firstly get the android studio downloaded in your system,
then open it.
Step 3: Open res -> layout -> activity_main.xml (or) main.xml. Here
we are going to create the application interface like add layouts,
Button , TextView and EditText.
Functional Requirements:
This is a basic project for beginners, the android
calculator app will help us to do various arithmetic calculations. This
android application will have a user interface with numbers and
arithmetic operations. For the development of this application, we
will make use of Android Studio. Let us go through its description
about what things the user interface will have:
It will have the number keys that will be created using buttons
Then we’ll have to create two more buttons for the delete and
answer buttons
Then, there will be a screen that will show the number entered by
the user and result when they click on answer
2
This calculator app would be a one time install; once the user installs
it they are always ready to use it
3
Develop a Simple Calculator App in
Android Studio:
Now we’ll begin with the development of a simple
calculator app. Throughout this article, we’ll understand the usage
and working of the code before we implement it. So let us see the
required files & steps for the code.
Also, there are the following things that we have added in the
drawable that are as follows.
The description of the buttons that we use. You can find that code
in Res > drawable > Buttons.xml.
4
code:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="abhiandroid.com.calculater.MainActivity"
android:orientation="vertical"
android:gravity="top"
android:textAlignment="center"
android:background="@android:color/holo_blue_bright"
android:weightSum="1">
<TextView
android:text="@string/enter_two_numbers"
android:layout_width="match_parent"
android:id="@+id/textView"
android:layout_height="30dp"
android:gravity="center_horizontal"
android:textColorLink="?android:attr/editTextColor"
tools:textStyle="bold|italic"
android:textStyle="bold|italic"
android:fontFamily="serif"
android:visibility="visible"
android:textSize="24sp"
android:layout_weight="0.07" />
<EditText
android:layout_width="match_parent"
5
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editOp1"
android:textSize="18sp"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:visibility="visible" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editOp2"
android:textSize="18sp"
android:gravity="center_horizontal"
android:elevation="1dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="+"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:id="@+id/btnadd"
android:layout_weight="0.03" />
<Button
android:text="-"
android:layout_width="78dp"
android:layout_height="wrap_content"
6
android:id="@+id/btnsub"
android:layout_weight="0.03" />
<Button
android:text="*"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:id="@+id/btnmul"
android:layout_weight="0.03"/>
<Button
android:text="/"
android:layout_height="wrap_content"
android:id="@+id/btndiv"
android:layout_width="78dp"
android:layout_weight="0.03" />
<Button
android:text="Clear"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/btnclr"
android:layout_weight="0.03" />
</LinearLayout>
// Addition
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 + oper2;
txtresult.setText(Double.toString(result));
}
7
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
});
//Subtraction
btnsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 - oper2;
txtresult.setText(Double.toString(result));
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
});
// Multiplication
btnmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 * oper2;
txtresult.setText(Double.toString(result));
8
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
});
// Division
btndiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 / oper2;
txtresult.setText(Double.toString(result));
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
})
9
.
Output:
10
Conclusion:
In this work we have developed a calculator for exact real
number computation and performed a theoretical analysis of the
algorithms and experimented on their implementation.
11
12