Write a program to create a simple calculator.

 XML CODE:

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

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".Main21Activity">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="35dp"

        android:layout_y="52dp"

        android:fontFamily="cursive"

        android:text="Simple calculator"

        android:textColor="#000"

        android:textSize="50dp"

        android:textStyle="bold" />

    <EditText

        android:id="@+id/et1"

        android:layout_width="300dp"        android:layout_height="wrap_content"

        android:layout_x="40dp"

        android:layout_y="187dp"

        android:hint="Enter first Number" />

    <EditText

        android:id="@+id/et2"

        android:layout_width="300dp"       android:layout_height="wrap_content"

        android:layout_x="42dp"

        android:layout_y="250dp"

        android:hint="Enter Second Number" />

    <Button

        android:id="@+id/bt1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"

        android:layout_x="8dp"

        android:layout_y="348dp"

        android:text="add" />

    <Button

        android:id="@+id/bt2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="103dp"

        android:layout_y="348dp"

        android:text="subtract" />

    <Button

        android:id="@+id/bt3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="207dp"

        android:layout_y="346dp"

        android:text="mutliply" />

    <Button

        android:id="@+id/bt4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="307dp"

        android:layout_y="348dp"

        android:text="devide" />

</AbsoluteLayout>


Java Code:

package com.example.myapplication;


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;


import static android.app.ProgressDialog.show;

public class Main21Activity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main21);

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

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

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

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

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

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

        b1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                double num1=Double.parseDouble(e1.getText().toString());

                double num2=Double.parseDouble(e2.getText().toString());

                double res=num1+num2;                Toast.makeText(Main21Activity.this, "Addition is="+res, Toast.LENGTH_SHORT).show();

            }

        });

        b2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

double num1=Double.parseDouble(e1.getText().toString());

double num2=Double.parseDouble(e2.getText().toString());

double res=num1-num2;               Toast.makeText(Main21Activity.this, "Subtraction is="+res, Toast.LENGTH_SHORT).show();

}

});

b3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

double num1=Double.parseDouble(e1.getText().toString());

double num2=Double.parseDouble(e2.getText().toString());

double res=num1*num2;               Toast.makeText(Main21Activity.this, "Mutiplication is="+res, Toast.LENGTH_SHORT).show();

}

});

b4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

double num1=Double.parseDouble(e1.getText().toString());

double num2=Double.parseDouble(e2.getText().toString());

 double res=num1/num2;            Toast.makeText(Main21Activity.this, "Division is="+res, Toast.LENGTH_SHORT).show();

}

});

} }

Output: 

 


Comments

Popular posts from this blog

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a Program to accept and display the student information

Write a program to create a first display screen on any search engine using auto complete text View