0% found this document useful (0 votes)
10 views11 pages

Shri Sai Institute of Technology, Aurangabad: "'Hyhors$Q$Qgurlg$Ssolfdwlrq) Ru0Dnlqj$&Dofxodwru"

This document describes the development of a basic calculator Android application. It includes 3 steps: 1. Designing the user interface with XML layouts, including input fields, buttons for arithmetic operations, and an output field. 2. Adding functionality to the buttons to perform calculations when clicked, including parsing input to numbers and handling invalid input. 3. Opening the main Java activity file to program the button click logic to perform the correct arithmetic operations and display the result.

Uploaded by

RUGVED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Shri Sai Institute of Technology, Aurangabad: "'Hyhors$Q$Qgurlg$Ssolfdwlrq) Ru0Dnlqj$&Dofxodwru"

This document describes the development of a basic calculator Android application. It includes 3 steps: 1. Designing the user interface with XML layouts, including input fields, buttons for arithmetic operations, and an output field. 2. Adding functionality to the buttons to perform calculations when clicked, including parsing input to numbers and handling invalid input. 3. Opening the main Java activity file to program the button click logic to perform the correct arithmetic operations and display the result.

Uploaded by

RUGVED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SHRI SAI INSTITUTE OF TECHNOLOGY,

AURANGABAD
A Project Report
On
“'HYHORS$Q$QGURLG$SSOLFDWLRQ)RU0DNLQJ$&DOFXODWRU”
Submitted in partial fulfillment for ‘I’ Scheme sixth Semester

2022-23

Diploma in
COMPUTER SCIENCE AND ENGINEERING
By

Rugved Raikwar (2015420089)


Harshal Ghusekar (2015420093)
Ishika Supekar (2015420091)

Under the guidance of

Miss.V.M.Gaikwad.
(Guide)

In Pursuit of Excellence
Step 1: Firstly get the android studio downloaded in your system, then open it.

Step 2: Create a new project and name it Calculator.

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.

i of Step 3 – Create a Linearlayout vertical, add a textview followed by two textfields


Number(decimal) for writing numbers in it. Starting code of activity_main.xml

<?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"
android:layout_height="wrap_content"

1
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>

The UI will currently look like this:

ii of Step 3 – Then before closing the above layout define another layout as Linearlayout
horizontal, add five button ( +, -, *, / and Clear) define their properties like id , width, height
etc. in it and close the linearlayout.

Following code of activity_main.xml . This code will be inserted in main layout:

<LinearLayout

2
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"
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>

The UI will now look like this:

3
iii of Step 3 – Further in continuation with previous linearlayout add a textview,
textfield(Number) for displaying result which makes the interface complete.

The complete interface code of activity_main.xml:

<?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"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editOp1"

4
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
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"
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"

5
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>

<TextView
android:text="@string/result"
android:layout_width="332dp"
android:id="@+id/textView1"
android:layout_marginTop="10dp"
android:layout_height="50dp"
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="30sp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/result"
android:textSize="18sp"
android:text="0.00"
android:gravity="center_horizontal" />
</LinearLayout>

So now we have designed the complete UI of the


Calculator App.

6
Step 4: Open src -> package -> MainActivity.java. The interface part of the application is
over, let’s focus on adding functionality to the application. This calculator app basically
perform five operations i.e addition, subtraction, multiplication, division and reset. So for that
we need to define these operation over button click. For that we use setOnClickListener()
function.

parseDouble() is used to convert String value to double. By default the value is String and we
need to convert it into Double to perform operation over it.

If person doesn’t enter the value and directly click on the any button then a Toast message will
appear on the screen telling user to enter the required numbers.

package abhiandroid.com.calculater;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText opr1;
private EditText opr2;
private Button btnadd;
private Button btnsub;
private Button btnmul;
private Button btndiv;
private Button btnclr;
private TextView txtresult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
opr1 = (EditText) findViewById(R.id.editOp1);
opr2 = (EditText) findViewById(R.id.editOp2);
btnadd = (Button) findViewById(R.id.btnadd);
btnsub = (Button) findViewById(R.id.btnsub);
btnmul = (Button) findViewById(R.id.btnmul);
btndiv = (Button) findViewById(R.id.btndiv);
btnclr = (Button) findViewById(R.id.btnclr);
txtresult= (TextView) findViewById(R.id.result);

7
// 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));
}
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());

8
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();
}
}
});
// 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();
}
}
});
// Reset Feilds
btnclr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
opr1.setText("");
opr2.setText("");
txtresult.setText("0.00");
opr1.requestFocus();
}
});

}
}

9
OUTPUT:
Now run the App and you will see the basic calculator App. Enter any number and
do the operations.

10

You might also like