Professional Documents
Culture Documents
XML Code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:background="#6AF6E9"
android:shrinkColumns="=1"
tools:context=".MainActivity" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30sp"
android:layout_marginTop="40sp"
android:layout_marginRight="30sp"
android:background="#3F51B5">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="70sp"
android:text="Simple Calculator"
android:textColor="#FF5722"
android:textSize="24sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="60sp"
android:layout_marginTop="40sp"
android:layout_marginRight="60sp">
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="60sp"
android:layout_marginTop="50sp"
android:layout_marginRight="60sp">
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="40sp"
android:layout_marginTop="40sp"
android:layout_marginRight="40sp">
<Button
android:id="@+id/addbtn"
android:layout_width="161dp"
android:layout_height="match_parent"
android:layout_marginRight="70sp"
android:layout_weight="1"
android:text="ADD" />
<Button
android:id="@+id/subbtn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:foregroundGravity="right"
android:text="SUB" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="40sp"
android:layout_marginTop="40sp"
android:layout_marginRight="40sp">
<Button
android:id="@+id/mulbtn"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_marginRight="70sp"
android:layout_weight="1"
android:text="MUL" />
<Button
android:id="@+id/divbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIV" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginTop="40sp"
android:layout_marginRight="50sp">
<EditText
android:id="@+id/edit3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
</TableLayout>
JAVA Code
package com.example.s_calulator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1=Integer.parseInt(edit1.getText().toString());
int n2=Integer.parseInt(edit2.getText().toString());
int ans=n1+n2;
edit3.setText(Integer.toString(ans));
}
});
subbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1=Integer.parseInt(edit1.getText().toString());
int n2=Integer.parseInt(edit2.getText().toString());
int ans=n1-n2;
edit3.setText(Integer.toString(ans));
}
});
mulbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1=Integer.parseInt(edit1.getText().toString());
int n2=Integer.parseInt(edit2.getText().toString());
int ans=n1*n2;
edit3.setText(Integer.toString(ans));
}
});
divbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1=Integer.parseInt(edit1.getText().toString());
int n2=Integer.parseInt(edit2.getText().toString());
int ans=n1/n2;
edit3.setText(""+ans);
}
});