Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Calculator App Devlopment

Splash Screen in Android


• A splash screen in Android is a graphical control element that typically
appears when an application is first launched.
• It serves as an initial visual indication to the user that the application
is loading or initializing.
• Splash screens are usually displayed for a short period, often just a
few seconds, before transitioning to the main activity or interface of
the application.
• Splash screen is used to display some basic introductory information
such as the company logo, content, etc just before the app loads
completely
Key characteristics of a splash screen
• Visual Branding: Splash screens often display the logo, name, or other
branding elements of the application or organization behind the app.
This helps in reinforcing brand recognition and creating a consistent
user experience.
• Loading Indication: While the splash screen is displayed, the
application may perform initial setup tasks such as loading resources,
initializing components, or fetching data from a remote server. The
splash screen provides a visual cue to the user that these processes
are underway.
Key characteristics of a splash screen
• Transient Nature: Splash screens are typically transient and
temporary. They are meant to be displayed briefly and are not
interactive. Once the initialization tasks are completed, the splash
screen transitions to the main activity or interface of the application.
• Customization: Developers have the flexibility to customize the
appearance and behavior of the splash screen to align with the overall
design and theme of the application. This includes choosing colors,
animations, and other visual effects.
Key characteristics of a splash screen
• User Experience Considerations: While splash screens can enhance
the perceived responsiveness of an application and provide branding
opportunities, they should be used carefully. Prolonged display of
splash screens or excessive animations can frustrate users, especially
if they perceive it as unnecessary delay in accessing the main
functionality of the app.
Steps to craete
• Open Android Studio and select "Start a new Android Studio project"
from the welcome screen.
• Choose a project name, domain, and location on your computer.
• Select "Phone and Tablet" as the form factor and choose the
minimum SDK version you want to support.
• Choose an empty activity template and click "Finish".
Designing Layout
• Open the activity_main.xml file in the "res/layout" folder of your
project.
• Replace the existing code with the following XML code:
Java code
• // Package declaration for the class MainActivity within the
com.example.calculator package
package com.example.calculator;
// Importing necessary classes from the Android framework import
android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
// Overriding the onCreate method to initialize the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Setting the layout for the activity
e1 = findViewById(R.id.num1); // Initializing EditText e1 with the view referenced by
num1 ID
e2 = findViewById(R.id.num2); // Initializing EditText e2 with the view referenced by
num2 ID
t1 = findViewById(R.id.result); // Initializing TextView t1 with the view referenced by
result ID
}
• View v is included in the method signatures, it is not directly used in
the implementation of these methods. It is required for compatibility
with the onClick attribute in the XML layout file to handle user
interactions.
// Method to validate input values
public boolean getNumbers() {
String s1 = e1.getText().toString(); // Getting the text from EditText e1 and converting it to string
String s2 = e2.getText().toString(); // Getting the text from EditText e2 and converting it to string

// Checking if any of the EditText fields are empty


if (TextUtils.isEmpty(s1) || TextUtils.isEmpty(s2)) {
t1.setText("Please enter both values"); // Setting error message in TextView t1
return false; // Returning false to indicate validation failure
}

return true; // Returning true to indicate validation success


}
// Method to perform addition operation
public void doSum(View v) {
if (getNumbers()) { // Checking if input validation succeeds
int num1 = Integer.parseInt(e1.getText().toString()); // Parsing text from e1 to
integer
int num2 = Integer.parseInt(e2.getText().toString()); // Parsing text from e2 to
integer
int sum = num1 + num2; // Calculating sum
t1.setText(String.valueOf(sum)); // Displaying sum in TextView t1
}
}
// Method to perform subtraction operation
public void doSub(View v) {
if (getNumbers()) { // Checking if input validation succeeds
int num1 = Integer.parseInt(e1.getText().toString()); // Parsing text from e1 to
integer
int num2 = Integer.parseInt(e2.getText().toString()); // Parsing text from e2 to
integer
int sub = num1 - num2; // Calculating difference
t1.setText(String.valueOf(sub)); // Displaying difference in TextView t1
}
}
// Method to perform multiplication operation
public void doMul(View v) {
if (getNumbers()) { // Checking if input validation succeeds
int num1 = Integer.parseInt(e1.getText().toString()); // Parsing text from e1 to
integer
int num2 = Integer.parseInt(e2.getText().toString()); // Parsing text from e2 to
integer
int mul = num1 * num2; // Calculating product
t1.setText(String.valueOf(mul)); // Displaying product in TextView t1
}
}
// Method to perform division operation
public void doDiv(View v) {
if (getNumbers()) { // Checking if input validation succeeds
int num1 = Integer.parseInt(e1.getText().toString()); // Parsing text from e1 to integer
int num2 = Integer.parseInt(e2.getText().toString()); // Parsing text from e2 to integer
if (num2 == 0) { // Checking if divisor is zero
t1.setText("Error: Division by zero"); // Displaying error message in TextView t1
} else {
double div = (double) num1 / num2; // Calculating division result
t1.setText(String.valueOf(div)); // Displaying division result in TextView t1
}
}
}
// Method to perform power operation
public void doPow(View v) {
if (getNumbers()) { // Checking if input validation succeeds
int num1 = Integer.parseInt(e1.getText().toString()); // Parsing text from e1 to
integer
int num2 = Integer.parseInt(e2.getText().toString()); // Parsing text from e2 to
integer
double pow = Math.pow(num1, num2); // Calculating power
t1.setText(String.valueOf(pow)); // Displaying power result in TextView t1
}
}
// Method to clear text in EditText e1
public void clearTextNum1(View v) {
e1.getText().clear(); // Clearing text in EditText e1
}

// Method to clear text in EditText e2


public void clearTextNum2(View v) {
e2.getText().clear(); // Clearing text in EditText e2
}
}
Description of methods used
• onCreate(Bundle savedInstanceState): This method is called when the activity is first
created. It initializes the activity layout by setting the content view from the layout
resource file (activity_main.xml). It also initializes the EditText and TextView variables
by finding the views in the layout using their respective IDs.
• getNumbers(): This method is used to validate the input values entered by the user. It
retrieves the text from EditText fields e1 and e2, checks if they are empty, and displays
an error message in TextView t1 if any of them are empty.
• doSum(View v): This method is invoked when the user clicks the sum button (+). It first
validates the input values using getNumbers(), then parses the text from e1 and e2 to
integers, calculates the sum, and displays the result in t1.
• doSub(View v): This method is invoked when the user clicks the subtract button (-). It
follows a similar process to doSum() but calculates the difference of the two numbers
entered by the user.
• doMul(View v): This method is invoked when the user clicks the multiply button (*). It
follows a similar process to doSum() but calculates the product of the two numbers
entered by the user.
• doDiv(View v): This method is invoked when the user clicks the divide button (/). It
follows a similar process to doSum() but calculates the division of the two numbers
entered by the user. It also handles the case where the divisor is zero to avoid division
by zero error.
• doPow(View v): This method is invoked when the user clicks the power button (^). It
follows a similar process to doSum() but calculates the power of the first number
raised to the second number.
• clearTextNum1(View v) and clearTextNum2(View v): These methods are invoked when
the user clicks the clear button associated with each input EditText field (e1 and e2).
They clear the text in the respective EditText fields.

You might also like