1 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="50dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>

<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/num1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="@+id/num2"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="@+id/num2"
android:layout_toRightOf="@+id/plus"/>
<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:layout_below="@+id/plus"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_below="@+id/plus"
android:layout_toRightOf="@+id/multiply"/>
<EditText
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/multiply"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>

 

2. MainActivity.java

package com.example.calculation;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText num1, num2, result;
Button plus, minus, multiply, divide;
double a, b, c=0;
View.OnClickListener cl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
result = (EditText) findViewById(R.id.result);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);

cl = new View.OnClickListener(){
public void onClick(View v){
a = Double.parseDouble(num1.getText().toString());
b = Double.parseDouble(num2.getText().toString());

switch (v.getId()){
case R.id.plus:
c=a+b;
break;
case R.id.minus:
c=a-b;
break;
case R.id.multiply:
c=a*b;
break;
case R.id.divide:
c=a/b;
break;
}
result.setText(String.valueOf(c));
}};
plus.setOnClickListener(cl);
minus.setOnClickListener(cl);
multiply.setOnClickListener(cl);
divide.setOnClickListener(cl);
}}

+ Recent posts