0% found this document useful (0 votes)
9 views4 pages

Practical No 9.2

The document contains an Android application layout defined in XML for a simple calculator with buttons for addition, subtraction, division, and multiplication. The MainActivity.java file implements the functionality for these operations, displaying the results using Toast messages. The app takes two input numbers from the user and performs the selected arithmetic operation when the corresponding button is clicked.

Uploaded by

nihalpatil0021
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)
9 views4 pages

Practical No 9.2

The document contains an Android application layout defined in XML for a simple calculator with buttons for addition, subtraction, division, and multiplication. The MainActivity.java file implements the functionality for these operations, displaying the results using Toast messages. The app takes two input numbers from the user and performs the selected arithmetic operation when the corresponding button is clicked.

Uploaded by

nihalpatil0021
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/ 4

Practical 9.

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

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

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="20dp"

android:paddingTop="10dp">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/e1"

android:layout_marginTop="60dp"

android:hint="Enter 1st no"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/e2"

android:hint="Enter 2nd no"

android:layout_marginTop="20dp"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/b1"

android:layout_marginTop="5dp"

android:layout_gravity="center_horizontal"

android:text="Add"/>

<Button

android:id="@+id/b2"
android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:layout_gravity="center_horizontal"

android:text="Sub"/>

<Button

android:layout_width="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_height="wrap_content"

android:id="@+id/b3"

android:layout_marginTop="5dp"

android:text="Div"/>

<Button

android:layout_width="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_height="wrap_content"

android:id="@+id/b4"

android:layout_marginTop="5dp"

android:text="Mul"/>

</LinearLayout>

MainActivty.java
package com.example.calculatorhype;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

EditText et1=(EditText) findViewById(R.id.e1);

EditText et2=(EditText) findViewById(R.id.e2);

Button bt1=(Button) findViewById(R.id.b1);

Button bt2=(Button) findViewById(R.id.b2);

Button bt3=(Button) findViewById(R.id.b3);

Button bt4=(Button) findViewById(R.id.b4);

bt1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Double d1=Double.parseDouble(et1.getText().toString());

Double d2=Double.parseDouble(et2.getText().toString());

Double d3=d1+d2;

Toast.makeText(MainActivity.this, "Addition="+d3.toString(), Toast.LENGTH_SHORT).show();

});

bt2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Double d1=Double.parseDouble(et1.getText().toString());

Double d2=Double.parseDouble(et2.getText().toString());

Double d3=d1-d2;

Toast.makeText(MainActivity.this, "Substraction="+d3.toString(), Toast.LENGTH_SHORT).show();

});

bt3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Double d1=Double.parseDouble(et1.getText().toString());

Double d2=Double.parseDouble(et2.getText().toString());

Double d3=d1/d2;
Toast.makeText(MainActivity.this, "Division="+d3.toString(), Toast.LENGTH_SHORT).show();

});

bt4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Double d1=Double.parseDouble(et1.getText().toString());

Double d2=Double.parseDouble(et2.getText().toString());

Double d3=d1*d2;

Toast.makeText(MainActivity.this, "Multiplication="+d3.toString(), Toast.LENGTH_SHORT).show();

});

Output:

You might also like