0% found this document useful (0 votes)
1K views9 pages

Develop A Native Calculator Application: Mainactivity - Java

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)
1K views9 pages

Develop A Native Calculator Application: Mainactivity - Java

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
You are on page 1/ 9

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:

MainActivity.java

package com.example.calculator_two;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

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
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nine = (Button) findViewById(R.id.b9);
eig = (Button) findViewById(R.id.b8);
sev = (Button) findViewById(R.id.b7);
six = (Button) findViewById(R.id.b6);
fiv = (Button) findViewById(R.id.b5);
four = (Button) findViewById(R.id.b4);
thr = (Button) findViewById(R.id.b3);
two = (Button) findViewById(R.id.b2);
one = (Button) findViewById(R.id.b1);
zero = (Button) findViewById(R.id.b0);
dot = (Button) findViewById(R.id.bd);
plus = (Button) findViewById(R.id.bpl);
mins = (Button) findViewById(R.id.bmin);
div = (Button) findViewById(R.id.bdiv);
mul = (Button) findViewById(R.id.bmul);
eq = (Button) findViewById(R.id.beq);
cl = (Button) findViewById(R.id.bcl);
et = (EditText) findViewById(R.id.tv);
zero.setOnClickListener(this);
nine.setOnClickListener(this);
eig.setOnClickListener(this);
sev.setOnClickListener(this);
six.setOnClickListener(this);
fiv.setOnClickListener(this);
four.setOnClickListener(this);
thr.setOnClickListener(this);
two.setOnClickListener(this);
one.setOnClickListener(this);
dot.setOnClickListener(this);
plus.setOnClickListener(this);
mins.setOnClickListener(this);
div.setOnClickListener(this);
mul.setOnClickListener(this);
eq.setOnClickListener(this);
cl.setOnClickListener(this);
et.setOnClickListener(this);
}

public void onClick(View v) {


switch (v.getId()) {
case R.id.b0:
case R.id.b1:
case R.id.b2:
case R.id.b3:
case R.id.b4:
case R.id.b5:
case R.id.b6:
case R.id.b7:
case R.id.b8:
case R.id.b9:

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


if (s.equals("0")) {
s = inDigit;
} else {
s += inDigit;
}
et.setText(s);
if (lO == '=') {
result = 0;
lO = ' ';
}
break;
case R.id.bpl:
compute();
lO = '+';
break;
case R.id.bmin:
compute();
lO = '-';
break;
case R.id.bdiv:
compute();
lO = '/';
break;
case R.id.bmul:
compute();
lO = '*';
break;
case R.id.beq:
compute();
lO = '=';
break;
case R.id.bcl:
result = 0;
s = "0";
lO = ' ';
et.setText("0");
break;
}
}

private void compute() {


int inNum = Integer.parseInt(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
}
et.setText(String.valueOf(result));
}
}
activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.

You might also like