0% found this document useful (0 votes)
60 views8 pages

17 Calculator

The document describes developing a native calculator application for Android. It includes steps to create a new Android project in Android Studio, add layout components like TextViews and Buttons to activity_main.xml, fetch input from EditTexts and perform calculations on button clicks in MainActivity.java, and display the output in a TextView. Code snippets for the layout and activity files are provided to show how calculations for addition, subtraction, multiplication and division are implemented when respective buttons are clicked. The application is then run on an emulator to test the output.

Uploaded by

Lovely BGM
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)
60 views8 pages

17 Calculator

The document describes developing a native calculator application for Android. It includes steps to create a new Android project in Android Studio, add layout components like TextViews and Buttons to activity_main.xml, fetch input from EditTexts and perform calculations on button clicks in MainActivity.java, and display the output in a TextView. Code snippets for the layout and activity files are provided to show how calculations for addition, subtraction, multiplication and division are implemented when respective buttons are clicked. The application is then run on an emulator to test the output.

Uploaded by

Lovely BGM
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/ 8

Ex.

No: 12 Develop a native calculator application

Aim

To develop a native calculator application.

Procedure

1. Open Android Studio.


2. Create a new project named as exno12.
3. In activity_main.xml add TextViews, EditTexts tor enter the no1, no2 and Button to add,
sub, mul and Div.
4. In MainActivity.java, create the objects for the components defined in the
activity_main.xml file.
5. Fetching values from the elements into the working of our app.
6. Run the exno12 application using an emulator.

Source Code

MainActivity.java

package com.example.exno12;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

81
import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements OnClickListener

//Defining the Views

EditText Num1;

EditText Num2;

Button Add;

Button Sub;

Button Mul;

Button Div;

TextView Result;

@Override

public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Referring the Views

Num1 = (EditText) findViewById(R.id.editText1);

Num2 = (EditText) findViewById(R.id.editText2);

Add = (Button) findViewById(R.id.Add);

Sub = (Button) findViewById(R.id.Sub);

Mul = (Button) findViewById(R.id.Mul);

Div = (Button) findViewById(R.id.Div);

Result = (TextView) findViewById(R.id.textView);

82
// set a listener

Add.setOnClickListener(this);

Sub.setOnClickListener(this);

Mul.setOnClickListener(this);

Div.setOnClickListener(this);

@Override

public void onClick (View v)

float num1 = 0;

float num2 = 0;

float result = 0;

String oper = "";

// check if the fields are empty

if (TextUtils.isEmpty(Num1.getText().toString()) ||
TextUtils.isEmpty(Num2.getText().toString()))

return;

// read EditText and fill variables with numbers

num1 = Float.parseFloat(Num1.getText().toString());

num2 = Float.parseFloat(Num2.getText().toString());

// defines the button that has been clicked and performs the corresponding operation

// write operation into oper, we will use it later for output

switch (v.getId())

83
case R.id.Add:

oper = "+";

result = num1 + num2;

break;

case R.id.Sub:

oper = "-";

result = num1 - num2;

break;

case R.id.Mul:

oper = "*";

result = num1 * num2;

break;

case R.id.Div:

oper = "/";

result = num1 / num2;

break;

default:

break;

// form the output line

Result.setText(num1 + " " + oper + " " + num2 + " = " + result);

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

84
xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="20dp">

<LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp">

<EditText

android:id="@+id/editText1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:inputType="numberDecimal"

android:textSize="20sp"

android:importantForAutofill="no" />

<EditText

android:id="@+id/editText2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:inputType="numberDecimal"

android:textSize="20sp"

85
android:autofillHints="" />

</LinearLayout>

<LinearLayout

android:id="@+id/linearLayout2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp">

<Button

android:id="@+id/Add"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/addition"

android:textSize="30sp"/>

<Button

android:id="@+id/Sub"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/subtract"

android:textSize="30sp"/>

<Button

android:id="@+id/Mul"

86
android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/product"

android:textSize="30sp"/>

<Button

android:id="@+id/Div"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/divide"

android:textSize="30sp"/>

</LinearLayout>

<TextView

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="50dp"

android:text="@string/answer_is"

android:textSize="30sp"

android:gravity="center"/>

</LinearLayout>

87
Output

Conclusion

Thus, the android application to perform simple calculation has been written, executed and output is
also verified.

88

You might also like