0% found this document useful (0 votes)
628 views9 pages

Native Calculator App Development Guide

The document describes developing a native calculator application for Android. It involves creating an Android project and AVD, adding buttons and functionality for numbers and arithmetic operations, and displaying the output. The algorithm includes setting onclick listeners for buttons, getting input, performing calculations, and displaying the result. It also includes the Java and XML code to build the calculator app interface and logic.

Uploaded by

Manoj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
628 views9 pages

Native Calculator App Development Guide

The document describes developing a native calculator application for Android. It involves creating an Android project and AVD, adding buttons and functionality for numbers and arithmetic operations, and displaying the output. The algorithm includes setting onclick listeners for buttons, getting input, performing calculations, and displaying the result. It also includes the Java and XML code to build the calculator app interface and logic.

Uploaded by

Manoj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Develop a Native Calculator Application
  • Result

3.

Develop a Native Calculator Application


AIM:
To develop a calculator android application.

ALGORITHM:

1. Create a New Android Project:


 Click New in the toolbar.
 In the window that appears, open the Android folder, select Android Application
Project, and click next.
 Provide the application name and the project name and then finally give the
desired package name.
 Choose a launcher icon for your application and then select Blank Activity and
then click Next
 Provide the desired Activity name for your project and then click Finish.
2. Create a New AVD (Android Virtual Device):
 In Eclipse, click Android Virtual Device Manager from the toolbar.
 In the Android Virtual Device Manager panel, click New.
 Fill in the details for the AVD. Give it a name, a platform target, an SD card size,
and a skin (HVGA is default).
 Click Create AVD and Select the new AVD from the Android Virtual Device
Manager and click Start.
3. Run the application.
4. Provide any two input numbers.
5. Choose any arithmetic operations of your choice and the output gets displayed on the
display screen of the calculator application.
6. Close the Android project.

PROGRAM CODE:

[Link]

package [Link].calculator_two;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity implements OnClickListener {


private Button nine, eig, sev, six, fiv, four, thr, two, one, zero, dot,
plus, mins, div, mul, eq, cl;
private EditText et;
private String s = "0";
private int result = 0;
private char lO = ' ';

protected void onCreate(Bundle savedInstanceState) {


// TODO Auto-generated method stub
[Link](savedInstanceState);
setContentView([Link].activity_main);
nine = (Button) findViewById([Link].b9);
eig = (Button) findViewById([Link].b8);
sev = (Button) findViewById([Link].b7);
six = (Button) findViewById([Link].b6);
fiv = (Button) findViewById([Link].b5);
four = (Button) findViewById([Link].b4);
thr = (Button) findViewById([Link].b3);
two = (Button) findViewById([Link].b2);
one = (Button) findViewById([Link].b1);
zero = (Button) findViewById([Link].b0);
dot = (Button) findViewById([Link]);
plus = (Button) findViewById([Link]);
mins = (Button) findViewById([Link]);
div = (Button) findViewById([Link]);
mul = (Button) findViewById([Link]);
eq = (Button) findViewById([Link]);
cl = (Button) findViewById([Link]);
et = (EditText) findViewById([Link]);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}

public void onClick(View v) {


switch ([Link]()) {
case [Link].b0:
case [Link].b1:
case [Link].b2:
case [Link].b3:
case [Link].b4:
case [Link].b5:
case [Link].b6:
case [Link].b7:
case [Link].b8:
case [Link].b9:

String inDigit = ((Button) v).getText().toString();


if ([Link]("0")) {
s = inDigit;
} else {
s += inDigit;
}
[Link](s);
if (lO == '=') {
result = 0;
lO = ' ';
}
break;
case [Link]:
compute();
lO = '+';
break;
case [Link]:
compute();
lO = '-';
break;
case [Link]:
compute();
lO = '/';
break;
case [Link]:
compute();
lO = '*';
break;
case [Link]:
compute();
lO = '=';
break;
case [Link]:
result = 0;
s = "0";
lO = ' ';
[Link]("0");
break;
}
}

private void compute() {


int inNum = [Link](s);
s = "0";
if (lO == ' ') {
result = inNum;
} else if (lO == '+') {
result += inNum;
} else if (lO == '-') {
result -= inNum;
} else if (lO == '*') {
result *= inNum;
} else if (lO == '/') {
result /= inNum;
} else if (lO == '=') {
// Keep the result for the next operation
}
[Link]([Link](result));
}
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter no. here"
android:textSize="30dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="horizontal"
android:weightSum="4" >

<Button
android:id="@+id/b9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textColor="#ff0000" />

<Button
android:id="@+id/b8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textColor="#ff0000" />

<Button
android:id="@+id/b7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textColor="#ff0000" />

<Button
android:id="@+id/bpl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textColor="#ff0000" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >

<Button
android:id="@+id/b6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textColor="#ff0000" />

<Button
android:id="@+id/b5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textColor="#ff0000" />

<Button
android:id="@+id/b4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textColor="#ff0000" />

<Button
android:id="@+id/bmin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textColor="#ff0000" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >

<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textColor="#ff0000" />
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textColor="#ff0000" />

<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textColor="#ff0000" />

<Button
android:id="@+id/bmul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textColor="#ff0000" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5" >

<Button
android:id="@+id/bd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textColor="#ff0000" />

<Button
android:id="@+id/b0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textColor="#ff0000" />
<Button
android:id="@+id/bcl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clr"
android:textColor="#ff0000" />

<Button
android:id="@+id/beq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textColor="#ff0000" />

<Button
android:id="@+id/bdiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textColor="#ff0000" />
</LinearLayout>

</LinearLayout>

OUTPUT:
RESULT:

Thus, the program for android based calculator application was executed successfully.

Common questions

Powered by AI

Separating UI layout from programming logic, as seen with XML files for UI and Java for logic, ensures a clear separation of concerns. Benefits include enhanced modularity, allowing the UI to evolve independently of the program logic, improved readability and maintainability of code, and easier collaboration between designers and developers. It also aids in internationalization and customization, as UI components can easily be modified or localized without altering the underlying logic .

The compute function in MainActivity.java converts the current string input into an integer, determines the arithmetic operation based on the last operator used, and updates the result accordingly. It uses a switch-case structure to perform operations like addition, subtraction, multiplication, and division, updating the result based on the input and operator. If the operator is '=', the function retains the result for subsequent operations .

MainActivity.java employs practices such as implementing the OnClickListener interface and using a centralized onClick method to handle all button click events. By assigning click listeners to each button at initialization, and using a switch statement to process the button ID during clicks, the application decouples button-specific logic from UI setup, enhancing readability and maintainability, and supporting effective state management for input and operations .

Using LinearLayout allows for a straightforward layout where UI components can be arranged in horizontal or vertical sequences, which is beneficial for creating a uniform grid-like structure of buttons. This layout simplifies weight and dimension management across various device screens and ensures consistent spacing and alignment. However, it may lead to performance issues in complex UIs with deep nesting, and it lacks the flexibility and wrapper capabilities of other layout types like ConstraintLayout, which can manage more dynamic and complex UI designs .

The essential steps to create a native calculator application in Android include creating a new Android project, defining an Android Virtual Device (AVD), developing a user interface with buttons for each digit and operation, implementing the logic for arithmetic operations in the MainActivity.java, and running the application on the AVD. The application performs basic arithmetic operations by capturing input, updating a display, and computing results based on user interactions .

The application handles user input by setting click listeners for each button. When a button is pressed, the onClick function identifies which button was clicked and updates the input string or calls the compute function depending on whether the button is a digit or an operation (e.g., +, -, *, /, =). It appends digits to the current input string and resets the input upon computation or clear button press .

The Android Virtual Device (AVD) serves as an emulator that simulates an Android device on which developers can run and test their applications. The AVD is configured with specific characteristics like screen resolution, operating system version, and hardware profile, allowing developers to see how their application behaves on a virtual device without needing physical hardware .

The activity_main.xml file is structured using a LinearLayout containing nested LinearLayouts, each oriented horizontally and assigned a weightSum for distributing space among child elements. These child elements are buttons representing digits and arithmetic operations. Each button has attributes such as layout_width, layout_weight, text, and textColor, which define their appearance and behavior in the UI. The main layout uses attributes like layout_width, layout_height, and orientation to define its properties .

User actions are reset by initializing relevant variables, clearing the input string, and updating the display to show '0' when the 'Clr' button is pressed. In the onClick function, the case for 'Clr' resets the result to 0, sets the current input string to '0', and clears any operation or result indications by setting the last operator variable to a blank state .

Integrating user feedback loops is essential as it allows the application to respond dynamically to user inputs, providing clear and immediate responses that enhance user satisfaction. Feedback mechanisms, such as updating the display as numbers and operations are entered, confirming actions with visual or auditory signals, and handling invalid inputs gracefully, ensure users understand each action's outcome, reducing error rates and improving overall engagement, especially in an application like a calculator where precision and clarity are critical .

3. Develop a Native Calculator Application 
 
AIM:  
 
To develop a calculator android application. 
 
ALGORITHM: 
 
1. Creat
private Button nine, eig, sev, six, fiv, four, thr, two, one, zero, dot, 
 
 
 
plus, mins, div, mul, eq, cl; 
 
private Ed
et.setOnClickListener(this); 
 
} 
 
 
public void onClick(View v) { 
 
 
switch (v.getId()) { 
 
 
case R.id.b0: 
 
 
ca
lO = '='; 
 
 
 
break; 
 
 
case R.id.bcl: 
 
 
 
result = 0; 
 
 
 
s = "0"; 
 
 
 
lO = ' '; 
 
 
 
et.setText("0");
android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="
android:id="@+id/b6" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_co
<Button 
            android:id="@+id/b2" 
            android:layout_width="match_parent" 
            android:layou
<Button 
            android:id="@+id/bcl" 
            android:layout_width="match_parent" 
            android:la
 
 
 
 
 
 
RESULT:  
 
Thus, the program for android based calculator application was executed successfully.

You might also like