0% found this document useful (0 votes)
18 views

MinorProject AndroidStudioBasedSimpleCalculator January2024Batch Academor

Android App development Simple Calculator

Uploaded by

sud.qub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

MinorProject AndroidStudioBasedSimpleCalculator January2024Batch Academor

Android App development Simple Calculator

Uploaded by

sud.qub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Name: Sudeep Kumar Behera

Stream: BTech Biotechnology


Year: 3rd
University: Techno India University, West Bengal
Course: Android App Development
Batch: January 2024
Minor Project: Android based Simple Calculator
INTRODUCTION
Welcome to our exciting project, where innovation meets practicality! In the ever-
evolving landscape of technology, mobile applications play a pivotal role in simplifying
everyday tasks. Our project focuses on harnessing the power of Android Studio to
create a user-friendly and efficient solution – a simple calculator application.

In a world where smartphones are an indispensable part of our lives, having a versatile
calculator on your device can be incredibly convenient. Whether you're a student
tackling complex mathematical problems, a professional in need of quick calculations, or
simply someone looking for a handy tool, our Android calculator app aims to cater to
your needs seamlessly.

Throughout this project, we will explore the fundamentals of Android app development,
leveraging the robust features of Android Studio. From designing a user interface that is
intuitive and visually appealing to implementing the logic that powers the calculator's
functionality, we will delve into the intricacies of creating a reliable and efficient
application.

By the end of this journey, you'll not only have a functional calculator app but also gain
valuable insights into the world of Android development. So, fasten your seatbelts, and
let's embark on this coding adventure to build a simple yet powerful calculator app that
can be a handy companion in your day-to-day life!
METHODOLOGY
The development of the simple calculator using Android Studio involves a systematic and structured
approach to ensure a successful outcome. The methodology comprises several key stages, each
contributing to the overall creation of a functional and user-friendly calculator application.

1. Project Planning and Requirement Analysis:

- Defining the project scope, objectives, and target audience.

- Identifing and analyzing the functional requirements of the calculator app.

- Planning the features, user interface, and design elements based on user needs.

2. Environment Setup:

- Installing and set up Android Studio, ensuring that all necessary dependencies are configured.

- Creating a new Android project and set up the development environment.

3. UI Design:

- Designing the user interface of the calculator using XML layout files.

- Implementing an intuitive and visually appealing design that accommodates standard calculator
functionalities.

4. User Input Handling:

- Incorporating mechanisms to capture user input from the UI elements (buttons, text fields, etc.).

- Implementing event listeners to respond to user interactions effectively.

5. Logic Implementation:

- Developing the core logic for performing basic arithmetic operations (addition, subtraction,
multiplication, division).

- Implementing error handling for invalid input and division by zero scenarios.

- Ensuring accurate and reliable calculation results.


6. Testing:

- Conducting thorough testing of each component and feature to identify and rectify bugs.

- Testing the app on various Android devices and screen sizes to ensure compatibility.

- Performing user acceptance testing to gather feedback and make necessary adjustments.

7. Optimization and Performance Enhancement:

- Optimizing the code for efficiency and responsiveness.

- Enhance the app's performance by minimizing resource usage and improving responsiveness during
calculations.

8. Documentation:

- Documenting the code comprehensively, providing comments and explanations for each module.

- Creating a user guide or documentation detailing the app's features, functionality, and usage
instructions.

9. Deployment:

- Packaging the application for deployment on Android devices.

- Distributing the app through appropriate channels, such as the Google Play Store or alternative
distribution platforms.
PROGRAM CODE
Code in the activity_main.xml file
<?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"
android:background="#8BC34A"
android:backgroundTint="@android:color/darker_gray"
tools:context=".MainActivity">

<!-- Text View to display our basic heading of "calculator"-->


<TextView
android:layout_width="194dp"
android:layout_height="43dp"
android:layout_marginStart="114dp"
android:layout_marginLeft="114dp"
android:layout_marginTop="58dp"
android:layout_marginEnd="103dp"
android:layout_marginRight="103dp"
android:layout_marginBottom="502dp"
android:scrollbarSize="30dp"
android:text=" Calculator"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- Edit Text View to input the values -->


<EditText
android:id="@+id/num1"
android:layout_width="364dp"
android:layout_height="28dp"
android:layout_marginStart="72dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="416dp"
android:background="@android:color/white"
android:ems="10"
android:onClick="clearTextNum1"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- Edit Text View to input 2nd value-->


<EditText
android:id="@+id/num2"
android:layout_width="363dp"
android:layout_height="30dp"
android:layout_marginStart="72dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="374dp"
android:background="@android:color/white"
android:ems="10"
android:onClick="clearTextNum2"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- Text View to display result -->


<TextView
android:id="@+id/result"
android:layout_width="356dp"
android:layout_height="71dp"
android:layout_marginStart="41dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="48dp"
android:layout_marginBottom="287dp"
android:background="@android:color/white"
android:text="result"
android:textColorLink="#673AB7"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A button to perform 'sum' operation -->
<Button
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doSum"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- A button to perform subtraction operation. -->

<!-- A button to perform division. -->

<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="210dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="113dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doSub"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.507" />

<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="307dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doDiv"
android:text="/"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- A button to perform multiplication. -->


<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="307dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMul"
android:text="x"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!-- A button to perform a modulus function. -->

<!-- A button to perform a power function. -->

<Button
android:id="@+id/button"
android:layout_width="103dp"
android:layout_height="46dp"
android:layout_marginStart="113dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="199dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doMod"
android:text="%(mod)"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.515" />

<Button
android:id="@+id/pow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="113dp"
android:layout_marginTop="292dp"
android:layout_marginEnd="210dp"
android:layout_marginBottom="263dp"
android:backgroundTint="@android:color/holo_red_light"
android:onClick="doPow"
android:text="n1^n2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.507" />

</androidx.constraintlayout.widget.ConstraintLayout>
Code in the MainActivity.java file
package com.example.calculator2;

import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.calculator2.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration appBarConfiguration;


private ActivityMainBinding binding;
public EditText e1, e2;
TextView t1;
int num1, num2;

public boolean getNumbers() {

//checkAndClear();
// defining the edit text 1 to e1
e1 = (EditText) findViewById(R.id.num1);

// defining the edit text 2 to e2


e2 = (EditText) findViewById(R.id.num2);

// defining the text view to t1


t1 = (TextView) findViewById(R.id.result);

// taking input from text box 1


String s1 = e1.getText().toString();

// taking input from text box 2


String s2 = e2.getText().toString();

if(s1.equals("Please enter value 1") && s2.equals(null))


{
String result = "Please enter value 2";
e2.setText(result);
return false;
}
if(s1.equals(null) && s2.equals("Please enter value 2"))
{
String result = "Please enter value 1";
e1.setText(result);
return false;
}
if(s1.equals("Please enter value 1") || s2.equals("Please enter value
2"))
{
return false;
}

if((!s1.equals(null) && s2.equals(null))|| (!s1.equals("") &&


s2.equals("")) ){

String result = "Please enter value 2";

e2.setText(result);
return false;
}
if((s1.equals(null) && !s2.equals(null))|| (s1.equals("") &&
!s2.equals("")) ){
//checkAndClear();
String result = "Please enter value 1";
e1.setText(result);
return false;
}
if((s1.equals(null) && s2.equals(null))|| (s1.equals("") &&
s2.equals("")) ){
//checkAndClear();
String result1 = "Please enter value 1";
e1.setText(result1);
String result2 = "Please enter value 2";
e2.setText(result2);
return false;
}

else {
// converting string to int.
num1 = Integer.parseInt(s1);

// converting string to int.


num2 = Integer.parseInt(s2);

return true;
}

public void doSum(View v) {

// get the input numbers


if (getNumbers()) {
int sum = num1 + num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}

}
public void clearTextNum1(View v) {

// get the input numbers


e1.getText().clear();
}
public void clearTextNum2(View v) {

// get the input numbers


e2.getText().clear();
}
public void doPow(View v) {

//checkAndClear();
// get the input numbers
if (getNumbers()) {
double sum = Math.pow(num1, num2);
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

// a public method to perform subtraction


public void doSub(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
int sum = num1 - num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

// a public method to perform multiplication


public void doMul(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
int sum = num1 * num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

// a public method to perform Division


public void doDiv(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {

// displaying the text in text view assigned as t1


double sum = num1 / (num2 * 1.0);
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

// a public method to perform modulus function


public void doMod(View v) {
//checkAndClear();
// get the input numbers
if (getNumbers()) {
double sum = num1 % num2;
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.num1);
// defining the edit text 2 to e2
e2 = (EditText) findViewById(R.id.num2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}
RESULTS

The screenshots of the results as visible on virtual android stimulator:


CONCLUSION
In conclusion, our journey to build a simple calculator using Android Studio has been an
exhilarating exploration of mobile app development. We started with a vision of creating
a user-friendly tool that could serve a wide range of users, from students to
professionals, making everyday calculations a breeze. Throughout this project, we
delved into the intricacies of Android Studio, learning to design an intuitive user
interface and implement the essential logic that powers a calculator's functionality.

As we reflect on the progress made, we've not only achieved our initial goal of building
a functional calculator app but have also gained valuable insights into the principles of
Android app development. From understanding layouts and views to handling user
input and implementing mathematical operations, each step has contributed to our
growing expertise in the field.

Our project has not only equipped us with the technical skills required for Android
development but has also instilled a sense of creativity and problem-solving. We've
witnessed the transformation of lines of code into a tangible, user-friendly application
that can make a meaningful impact in users' daily lives.

As we celebrate the success of this project, let it be a stepping stone for future
endeavors in mobile app development. The knowledge and experience gained here will
undoubtedly serve as a solid foundation for tackling more complex projects and
exploring the ever-expanding possibilities within the realm of technology.

In the dynamic world of software development, this project represents a small yet
significant contribution. We hope our calculator app becomes a practical tool for users
and inspires further curiosity and exploration in the fascinating world of Android app
development. Thank you for joining us on this coding adventure, and may your future
projects be as rewarding and enlightening as this one!

You might also like