[Android Software Development] DC07:
HOW-To Make A Simple Android Calculator

​

How To Make A Simple Calculator

Step 1

. Select Create New Android Application

Creating XML For Layout

To create a calculator first we need to create the layout of the calculator.

Layout is created using XML file given below

Copy This To Your “activity_main” (Note- If you named the layout activity main or something else please change it to activity_main or it will give a couple of errors)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<EditText

android:id="@+id/result_id"

android:layout_width="fill_parent"

android:layout_height="120dp"

/>

<Button

android:id="@+id/Btn7_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/result_id"

android:text="7"

android:onClick="btn7Clicked"

/>

<Button

android:id="@+id/Btn8_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/result_id"

android:layout_toRightOf="@id/Btn7_id"

android:text="8"

android:onClick="btn8Clicked"

/>

<Button

android:id="@+id/Btn9_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/result_id"

android:layout_toRightOf="@id/Btn8_id"

android:text="9"

android:onClick="btn9Clicked"

/>

<Button

android:id="@+id/Btnclear_id"

android:layout_width="90dp"

android:layout_height="60dp"

android:layout_below="@id/result_id"

android:layout_toRightOf="@id/Btn9_id"

android:text="clear"

android:onClick="btnclearClicked"

/>

<Button

android:id="@+id/Btn4_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/Btn7_id"

android:text="4"

android:onClick="btn4Clicked"

/> <Button

android:id="@+id/Btn5_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/Btn8_id"

android:layout_toRightOf="@id/Btn4_id"

android:text="5"

android:onClick="btn5Clicked"

/> <Button

android:id="@+id/Btn6_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/Btn9_id"

android:layout_toRightOf="@id/Btn5_id"

android:text="6"

android:onClick="btn6Clicked"

/>

<Button

android:id="@+id/Btnplus_id"

android:layout_width="90dp"

android:layout_height="60dp"

android:layout_below="@id/Btnclear_id"

android:layout_toRightOf="@id/Btn6_id"

android:text="+"

android:onClick="btnplusClicked"

/>

<Button

android:id="@+id/Btn1_id"

android:layout_width="70dp"

android:layout_height="60dp"

android:layout_below="@id/Btn4_id"
android:text="1"
android:onClick="btn1Clicked"
/>

<Button
android:id="@+id/Btn2_id"
android:layout_width="70dp"
android:layout_height="60dp"
android:layout_below="@id/Btn5_id"
android:layout_toRightOf="@id/Btn1_id"
android:text="2"
android:onClick="btn2Clicked"
/>

<Button
android:id="@+id/Btn3_id"
android:layout_width="70dp"
android:layout_height="60dp"
android:layout_below="@id/Btn6_id"
android:layout_toRightOf="@id/Btn2_id"
android:text="3"
android:onClick="btn3Clicked"
/>

<Button
android:id="@+id/Btnminus_id"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_below="@id/Btnplus_id"
android:layout_toRightOf="@id/Btn3_id"
android:text="-"
android:onClick="btnminusClicked"
/>

<Button
android:id="@+id/Btnequal_id"
android:layout_width="110dp"
android:layout_height="60dp"
android:layout_below="@id/Btn1_id"
android:text="="
android:onClick="btnequalClicked"
/>

<Button
android:id="@+id/Btndivide_id"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_below="@id/Btn1_id"
android:layout_toRightOf="@id/Btnequal_id"
android:text="/"
android:onClick="btndivideClicked"
/>

<Button
android:id="@+id/Btnmulti_id"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_below="@id/Btnminus_id"
android:layout_toRightOf="@id/Btndivide_id"
android:text="*"
android:onClick="btnmultiClicked"

/>

</RelativeLayout>

The Graphical Layout Will look Like this

Then You have to Make The Buttons work and calculate your numbers

Simply find your MainActivity.java (It Is place under package name)

Copy This to your java file :

package com.dc.simplecalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

public String str ="";
Character op = 'q';
int i,num,numtemp;
EditText showResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

showResult = (EditText)findViewById(R.id.result_id);

}
public void btn1Clicked(View v){
insert(1);

}

public void btn2Clicked(View v){
insert(2);

}
public void btn3Clicked(View v){
insert(3);

}
public void btn4Clicked(View v){
insert(4);

}
public void btn5Clicked(View v){
insert(5);

}
public void btn6Clicked(View v){
insert(6);
}
public void btn7Clicked(View v){
insert(7);

}
public void btn8Clicked(View v){
insert(8);

}
public void btn9Clicked(View v){
insert(9);

}
public void btnplusClicked(View v){
perform();
op = '+';

}

public void btnminusClicked(View v){
perform();
op = '-';

}
public void btndivideClicked(View v){
perform();
op = '/';

}
public void btnmultiClicked(View v){
perform();
op = '*';

}
public void btnequalClicked(View v){
calculate();

}

public void btnclearClicked(View v){
reset();
}
private void reset() {
// TODO Auto-generated method stub
str ="";
op ='q';
num = 0;
numtemp = 0;
showResult.setText("");
}
private void insert(int j) {
// TODO Auto-generated method stub
str = str+Integer.toString(j);
num = Integer.valueOf(str).intValue();
showResult.setText(str);

}
private void perform() {
// TODO Auto-generated method stub
str = "";
numtemp = num;
}
private void calculate() {
// TODO Auto-generated method stub
if(op == '+')
num = numtemp+num;
else if(op == '-')
num = numtemp-num;
else if(op == '/')
num = numtemp/num;
else if(op == '*')
num = numtemp*num;
showResult.setText(""+num);
}

}

Next Step!

Testing Your App, Fingers Crossed

Simply Click On Run>Run As Android Application

Congo! You Just Made A Simple Calc App……

Exporting Your App

Open Android Manifest.xml

Select use Export Wizard

That’s All….

If you have any queries post in this thread I will try my best to solve it…

Sucess!

Have A Good Day!

​