Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
33 views8 pages

NAME: Nabeel Baig Class/Sec: Bscs 5A ROLL NO: 0020 Submitt To: Sir Awais

The document contains the XML code and Java code for a calculator mobile application. The XML code defines the layout and properties of the buttons, edit text, and overall constraint layout. The Java code defines variables for the buttons and edit text, retrieves them by ID, and sets onclicklisteners for the buttons to perform calculation operations.

Uploaded by

Nabeel Baig
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)
33 views8 pages

NAME: Nabeel Baig Class/Sec: Bscs 5A ROLL NO: 0020 Submitt To: Sir Awais

The document contains the XML code and Java code for a calculator mobile application. The XML code defines the layout and properties of the buttons, edit text, and overall constraint layout. The Java code defines variables for the buttons and edit text, retrieves them by ID, and sets onclicklisteners for the buttons to perform calculation operations.

Uploaded by

Nabeel Baig
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/ 8

NAME: Nabeel baig

CLASS/SEC: BSCS 5A
ROLL NO: 0020
SUBMITT TO: SIR AWAIS

XML CODE OF MAIN ACTIVITY


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<Button
android:id="@+id/btnEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="="
app:layout_constraintEnd_toStartOf="@+id/btnDiv"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn0"
app:layout_constraintTop_toBottomOf="@+id/btn9" />

<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="0"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btnEqual"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnDot"
app:layout_constraintTop_toBottomOf="@+id/btn8" />

<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="*"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn9"
app:layout_constraintTop_toBottomOf="@+id/btnSub" />

<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="8"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn7"
app:layout_constraintTop_toBottomOf="@+id/btn5" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="300dp"
android:layout_height="65dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="84dp"
android:layout_marginEnd="62dp"
android:layout_marginRight="62dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="94dp"
android:text="1"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="93dp"
android:text="3"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btnAdd"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="93dp"
android:text="+"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn3"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="94dp"
android:text="2"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="4"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />

<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="5"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn4"
app:layout_constraintTop_toBottomOf="@+id/btn2" />

<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:text="6"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btnSub"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn5"
app:layout_constraintTop_toBottomOf="@+id/btn3" />

<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:text="-"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn6"
app:layout_constraintTop_toBottomOf="@+id/btnAdd" />

<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="7"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn4" />

<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="9"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btnMul"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn8"
app:layout_constraintTop_toBottomOf="@+id/btn6" />

<Button
android:id="@+id/btnDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="."
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/btn0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn7" />

<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="/"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnEqual"
app:layout_constraintTop_toBottomOf="@+id/btnMul" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="165dp"
android:layout_marginLeft="165dp"
android:layout_marginEnd="159dp"
android:layout_marginRight="159dp"
android:text="C"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn0" />

</androidx.constraintlayout.widget.ConstraintLayout>

JAVA CODE OF MAIN ACTIVITY:


package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button
btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btnAdd,btnMul,btnSub,btnDiv,btnClear,b
tnEqual,btnDot;
EditText ed1;
float Res1,Res2;
boolean Add,Sub,Mul,Div;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn8);
btn0=(Button)findViewById(R.id.btn0);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnSub=(Button)findViewById(R.id.btnSub);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
btnClear=(Button)findViewById(R.id.btnClear);
btnEqual=(Button)findViewById(R.id.btnEqual);
btnDot=(Button)findViewById(R.id.btnDot);
ed1=(EditText)findViewById(R.id.editTextTextPersonName);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"7");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"9");
}
});btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"8");
}
});btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"0");
}
});

btnDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText(ed1.getText()+".");
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1==null){
ed1.setText("");
}
else{
Res1=Float.parseFloat(ed1.getText()+"");
Add=true;
ed1.setText(null);
}

}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1==null){
ed1.setText("");
}
else{
Res1=Float.parseFloat(ed1.getText()+"");
Sub =true;
ed1.setText(null);
}

}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1==null){
ed1.setText("");
}
else{
Res1=Float.parseFloat(ed1.getText()+"");
Mul=true;
ed1.setText(null);
}

}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1==null){
ed1.setText("");
}
else{
Res1=Float.parseFloat(ed1.getText()+"");
Div=true;
ed1.setText(null);
}

}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Res2=Float.parseFloat(ed1.getText()+"");
if(Add==true){
ed1.setText(Res1+Res2+"");
Add=false;
}
if(Sub==true){
ed1.setText(Res1-Res2+"");
Sub=false;
}if(Mul==true){
ed1.setText(Res1*Res2+"");
Mul=false;
}if(Div==true){
ed1.setText(Res1/Res2+"");
Div=false;
}
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed1.setText("");
}
});

}
}

You might also like