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

Calci

The document contains an Android layout XML and Java code for a simple calculator application. It includes two EditText fields for user input, four buttons for addition, subtraction, multiplication, and division, and a TextView to display the result. The Java code handles button clicks to perform the respective arithmetic operations and display the results with Toast messages for user feedback.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Calci

The document contains an Android layout XML and Java code for a simple calculator application. It includes two EditText fields for user input, four buttons for addition, subtraction, multiplication, and division, and a TextView to display the result. The Java code handles button clicks to perform the respective arithmetic operations and display the results with Toast messages for user feedback.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="288dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="Enter First Number"/>

<EditText
android:id="@+id/et2"
android:layout_width="288dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:hint="Enter Second Number"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="ADD" />

<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"

android:gravity="center"
android:text="SUB" />

<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="MUL" />

<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DIV" />

</LinearLayout>

<TextView
android:id="@+id/tv1"
android:layout_width="288dp"
android:layout_height="92dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:gravity="center"
android:hint="RESULT"
android:textSize="50dp" />

</LinearLayout>

package com.example.calciapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener


{

EditText et1, et2;


Button btnAdd, btnSub, btnMul, btnDiv;

TextView tv1;

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

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

btnAdd=(Button) findViewById(R.id.btnAdd);
btnSub=(Button) findViewById(R.id.btnSub);
btnMul=(Button) findViewById(R.id.btnMul);
btnDiv=(Button) findViewById(R.id.btnDiv);

tv1=(TextView) findViewById(R.id.tv1);

btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);

@Override
public void onClick(View view) {
//Toast t=Toast.makeText(this,"",Toast.LENGTH_SHORT);
//Toast t=Toast.makeText(MainActivity.this,"",Toast.LENGTH_SHORT);
//Toast t=Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT);
//Toast t=Toast.makeText(getBaseContext(),"",Toast.LENGTH_SHORT);

String s1=et1.getText().toString();
int n1=Integer.parseInt(s1);

int n2=Integer.parseInt(et2.getText().toString());

int result=0;

if(view.getId()==R.id.btnAdd) {
Toast t = Toast.makeText(view.getContext(), "ADD BUTTON CLICKED..",
Toast.LENGTH_SHORT);
t.show();

result=n1+n2;
tv1.setText(String.valueOf(result));
}

else if (view.getId()==R.id.btnSub) {
Toast t = Toast.makeText(view.getContext(), "SUB BUTTON CLICKED..",
Toast.LENGTH_SHORT);
t.show();
result=n1-n2;
tv1.setText(String.valueOf(result));
}

else if (view.getId()==R.id.btnMul) {
Toast t = Toast.makeText(view.getContext(), "MUL BUTTON CLICKED..",
Toast.LENGTH_SHORT);
t.show();
result=n1*n2;
tv1.setText(String.valueOf(result));
}
else if (view.getId()==R.id.btnDiv) {
Toast t = Toast.makeText(view.getContext(), "DIV BUTTON CLICKED..",
Toast.LENGTH_SHORT);
t.show();
result=n1/n2;
tv1.setText(String.valueOf(result));
}
else {
Toast.makeText(view.getContext(), "NO BUTTON CLICKED..",
Toast.LENGTH_SHORT).show();
}
}
}

You might also like