Exp - 9
Exp - 9
23
Aim of the Experiment: Create a simple calculator app.
Introduction:
By building a simple calculator app, this lab experiment's primary goal is to explain the core ideas
of Android app development. It also provides insights into user interface design, event handling,
and the processing of user input and output. A calculator app is a basic piece of software that
shows how user input might be handled and presented.
Code :
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
android:layout_marginRight="52dp"
<androidx.constraintlayout.widget.ConstraintLayout
android:width="10dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:text="*"
xmlns:tools="http://schemas.android.com/tools"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@+id/ET2"
android:layout_height="match_parent"
/>
tools:context=".MainActivity">
<Button
<ImageView
android:id="@+id/sub"
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_width="162dp"
android:layout_height="wrap_content"
android:layout_height="122dp"
android:layout_marginTop="20dp"
android:layout_marginStart="112dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="112dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:width="10dp"
app:layout_constraintStart_toStartOf="parent"
android:height="10dp"
app:layout_constraintTop_toTopOf="parent"
android:text="-"
app:srcCompat="@drawable/cal" />
app:layout_constraintEnd_toStartOf="@+id/mul"
<Button
app:layout_constraintTop_toBottomOf="@+id/ET2"
android:id="@+id/eql"
/>
android:layout_width="wrap_content"
<Button
android:layout_height="wrap_content"
android:id="@+id/sum"
android:layout_marginTop="36dp"
android:layout_width="wrap_content"
android:layout_marginEnd="56dp"
android:layout_height="wrap_content"
android:layout_marginRight="56dp"
android:layout_marginTop="20dp"
android:width="10dp"
android:width="10dp"
android:height="10dp"
android:height="10dp"
android:text="="
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/sub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintTop_toBottomOf="@+id/mul"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.065" />
app:layout_constraintTop_toBottomOf="@+id/ET2"
<EditText
/>
android:id="@+id/ET2"
<Button
android:layout_width="wrap_content"
android:id="@+id/div"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="108dp"
android:layout_height="wrap_content"
android:layout_marginLeft="108dp"
android:layout_marginTop="36dp"
android:layout_marginTop="8dp"
android:width="10dp"
android:ems="10"
android:height="10dp"
android:inputType="number"
android:text="/"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
app:layout_constraintEnd_toStartOf="@+id/eql"
<Button
app:layout_constraintHorizontal_bias="0.603"
android:id="@+id/mul"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/sum"
android:layout_height="wrap_content"
app:layout_constraintVertical_bias="0.065" />
android:layout_marginTop="20dp"
<EditText
android:layout_marginEnd="52dp"
android:id="@+id/ET1"
android:layout_width="wrap_content"
android:layout_marginLeft="28dp" <Button
android:layout_marginTop="264dp" android:id="@+id/clear"
android:text="Enter the first number" android:layout_width="wrap_content"
android:textSize="20sp" android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:background="#FF0707"
app:layout_constraintTop_toTopOf="parent" /> android:text="clear"
<TextView android:textColorLink="#F40404"
android:id="@+id/res" app:backgroundTint="@color/
android:layout_width="316dp" design_default_color_error"
android:layout_height="77dp" app:layout_constraintBottom_toBottomOf="parent"
android:background="#090909" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.566"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
android:text="00"
app:layout_constraintTop_toBottomOf="@+id/sub"
android:textAllCaps="false" app:layout_constraintVertical_bias="0.857" />
android:textColor="#FFFFFF" </androidx.constraintlayout.widget.ConstraintLayout>
android:textSize="48sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.53" />
Mainactivity.java :
package com.example.textcalculator; int i = Integer.parseInt(first.getText().toString());
import androidx.appcompat.app.AppCompatActivity; int j = Integer.parseInt(second.getText().toString());
import android.os.Bundle; int sum;
import android.view.View; sum = i - j;
import android.widget.Button; store=sum;
import android.widget.EditText; result.setText(i+" - "+j);
import android.widget.TextView; }
public class MainActivity extends AppCompatActivity { });
EditText first; mul.setOnClickListener(new View.OnClickListener() {
EditText second; @Override
Button add; public void onClick(View view) {
int i = Integer.parseInt(first.getText().toString());
Button sub;
int j = Integer.parseInt(second.getText().toString());
Button mul;
int sum;
Button div; sum = i * j;
Button equal; store=sum;
TextView result; result.setText(i+" * "+j);
Button clear; }
int store; });
@Override div.setOnClickListener(new View.OnClickListener() {
protected void onCreate(Bundle savedInstanceState) { @Override
super.onCreate(savedInstanceState); public void onClick(View view) {
setContentView(R.layout.activity_main); int i = Integer.parseInt(first.getText().toString());
first=(EditText)findViewById(R.id.ET1); int j = Integer.parseInt(second.getText().toString());
second=(EditText)findViewById(R.id.ET2); int sum;
sum = i / j;
add=(Button) findViewById(R.id.sum);
store=sum;
sub=(Button) findViewById(R.id.sub);
result.setText(i+" / "+j);}
mul=(Button) findViewById(R.id.mul); });
div=(Button) findViewById(R.id.div); equal.setOnClickListener(new View.OnClickListener() {
equal=(Button) findViewById(R.id.eql); @Override
clear=(Button) findViewById(R.id.clear); public void onClick(View view) {
result=(TextView) findViewById(R.id.res); try {
add.setOnClickListener(new View.OnClickListener() { result.setText(String.valueOf(store));
@Override }
public void onClick(View view) { catch (Exception e){
int i = Integer.parseInt(first.getText().toString()); e.printStackTrace();
int j = Integer.parseInt(second.getText().toString()); }}
int sum; });
clear.setOnClickListener(new View.OnClickListener() {
sum = i + j;
@Override
store=sum;
public void onClick(View view) {
result.setText(i+" + "+j); result.setText("00");
} first.setText("");
}); second.setText("");
sub.setOnClickListener(new View.OnClickListener() { }
@Override });
public void onClick(View view) { }
Compilation Error :
In this program, I Faced a small syntax error while writing the code mistakenly written in Image
View giving android src double backslash with file extension and it is resolved by removing one
slash ‘/’.
Final Output:
Conclusion:
In conclusion, developing a straightforward calculator app for Android has taught us a lot about
the foundations of mobile app development. We have explored many facets of design, logic, user
interaction, and testing along the way. Let's consider the main lessons learned from this
undertaking as we come to a close.