Professional Documents
Culture Documents
Calculator Part
Calculator Part
0 shuttereditz.blogspot.com
f /shuttereditz
Intro:
In this Tutorial we are going to start a new project Calculator.
First of all, to start with we are going to create a simple calculator to do simple arithmetic operations (i.e., one
operator and two operands).
Step 1:
Click Finish keeping the default settings for Activity (Name: Main Activity…..).
1 shuttereditz.blogspot.com
f /shuttereditz
First of all insert a Linear Layout (Vertical) inside the Relative Layout. (Delete the Hello World).
Change the following settings for the Text Field in the XML tab.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtScreen"
android:inputType="number|numberDecimal|numberSigned" />
Now go back to Design tab insert a Table Layout below the Text Field ‘txtScreen’.
2 shuttereditz.blogspot.com
f /shuttereditz
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
</TableRow>
Go to Design Tab and insert Buttons for operators in the first row.
Insert a new Table Row below the first one and add Buttons to it to finish the Layout.
3 shuttereditz.blogspot.com
f /shuttereditz
4 shuttereditz.blogspot.com
f /shuttereditz
5 shuttereditz.blogspot.com
f /shuttereditz
Button Button Id
Button 0 btn0
Button 1 btn1
Button 2 btn2
Button 3 btn3
Button 4 btn4
Button 5 btn5
Button 6 btn6
Button 7 btn7
Button 8 btn8
Button 9 btn9
Button + btnAdd
Button - btnSubtract
Button * btnMultiply
Button / btnDivide
Button C btnClear
Button . btnDot
6 shuttereditz.blogspot.com
f /shuttereditz
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtScreen"
android:inputType="number|numberDecimal|numberSigned" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/btnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/btnSubtract" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/btnDivide" />
<Button
android:layout_width="wrap_content"
7 shuttereditz.blogspot.com
f /shuttereditz
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/btnMultiply" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/btn1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/btn2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/btn3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/btn4" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/btn5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/btn6" />
8 shuttereditz.blogspot.com
f /shuttereditz
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/btn7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/btn8" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/btn9" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/btn0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/btnDot" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/btnClear" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="wrap_content"
9 shuttereditz.blogspot.com
f /shuttereditz
android:layout_height="wrap_content"
android:text="="
android:id="@+id/btnEquals"
android:layout_span="4" />
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
10 shuttereditz.blogspot.com
f /shuttereditz
package com.blogspot.shuttereditz.calculus;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Inside the onCreate() function below the setContentView(R.layout.activity_main); Add the following
code:-
Scr = (EditText) findViewById(R.id.txtScreen);
Scr.setText("");
To add functions to the Buttons create an Array of Id’s by inserting this code below:
int idList[] = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn2, R.id.btn3, R.id.btn4,
R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9,
R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide,
R.id.btnClear,R.id.btnEquals, R.id.btnDot};
11 shuttereditz.blogspot.com
f /shuttereditz
case R.id.btnAdd:
mMath("+");
break;
case R.id.btnSubtract:
if(mod.equals("replace")) {
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
}
else mMath("-");
break;
case R.id.btnMultiply:
mMath("*");
break;
case R.id.btnDivide:
mMath("/");
break;
case R.id.btnEquals:
mResult();
Operation = "";
NumberBf = 0;
break;
default:
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
12 shuttereditz.blogspot.com
f /shuttereditz
Now we need to create mMath() and mResult() function so above the onButtonClick() function add
public void mMath(String str) {
mResult();
try {
NumberBf = Float.parseFloat(Scr.getText().toString());
Operation = str;
}catch (Exception e) {
Toast.makeText(getApplicationContext(),(CharSequence) e,
Toast.LENGTH_SHORT).show();
Scr.setText("SYNTAX ERROR");
mod="replace";
}
}
try {
switch (Operation) {
case "+":
result = NumAf + NumberBf;
break;
case "-":
result = NumberBf - NumAf;
break;
case "*":
result = NumAf * NumberBf;
break;
case "/":
result = NumberBf / NumAf;
break;
default:
result = NumAf;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Scr.setText(String.valueOf(result));
mod = "replace";
13 shuttereditz.blogspot.com
f /shuttereditz
if(mod.equals("add"))
Scr.setText(ScrTxt);
else
Scr.setText(str);
mod = "add";
}
14 shuttereditz.blogspot.com
f /shuttereditz
package com.blogspot.shuttereditz.calculus;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int id:idList) {
View v = (View) findViewById(id);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClick(v);
}
});
}
mResult();
try {
NumberBf = Float.parseFloat(Scr.getText().toString());
Operation = str;
}catch (Exception e) {
Toast.makeText(getApplicationContext(),(CharSequence) e, Toast.LENGTH_SHORT).show();
Scr.setText("SYNTAX ERROR");
mod="replace";
}
15 shuttereditz.blogspot.com
f /shuttereditz
try {
switch (Operation) {
case "+":
result = NumAf + NumberBf;
break;
case "-":
result = NumberBf - NumAf;
break;
case "*":
result = NumAf * NumberBf;
break;
case "/":
result = NumberBf / NumAf;
break;
default:
result = NumAf;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Scr.setText(String.valueOf(result));
mod = "replace";
16 shuttereditz.blogspot.com
f /shuttereditz
case R.id.btnAdd:
mMath("+");
break;
case R.id.btnSubtract:
if(mod.equals("replace")) {
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
}
else mMath("-");
break;
case R.id.btnMultiply:
mMath("*");
break;
case R.id.btnDivide:
mMath("/");
break;
case R.id.btnEquals:
mResult();
Operation = "";
NumberBf = 0;
break;
default:
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
17 shuttereditz.blogspot.com
f /shuttereditz
That’s it….. We have completed creating a simple calculator. Thank you for using this tutorial…ENJOY!
VISIT US
shuttereditz.blogspot.com
18 shuttereditz.blogspot.com