Documente Academic
Documente Profesional
Documente Cultură
shutter
shuttereditz.blogspot.com
f /shuttereditz
shutter
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:
Start Android Studio .Create a new Project .
shuttereditz.blogspot.com
f /shuttereditz
shutter
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.
shuttereditz.blogspot.com
f /shuttereditz
shutter
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.
shuttereditz.blogspot.com
f /shuttereditz
shutter
shuttereditz.blogspot.com
f /shuttereditz
shutter
shuttereditz.blogspot.com
f /shuttereditz
shutter
Button
Button 0
Button 1
Button 2
Button 3
Button 4
Button 5
Button 6
Button 7
Button 8
Button 9
Button +
Button Button *
Button /
Button C
Button .
Button Id
btn0
btn1
btn2
btn3
btn4
btn5
btn6
btn7
btn8
btn9
btnAdd
btnSubtract
btnMultiply
btnDivide
btnClear
btnDot
shuttereditz.blogspot.com
f /shuttereditz
shutter
shuttereditz.blogspot.com
f /shuttereditz
shutter
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
shutter
<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
shutter
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
shutter
package com.blogspot.shuttereditz.calculus;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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 Ids 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
shutter
shuttereditz.blogspot.com
f /shuttereditz
shutter
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";
}
}
public void mResult() {
NumAf = 0;
if(!Scr.getText().toString().trim().isEmpty())
NumAf = Float.parseFloat(Scr.getText().toString());
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
shutter
14
shuttereditz.blogspot.com
f /shuttereditz
shutter
package com.blogspot.shuttereditz.calculus;
import
import
import
import
import
import
android.support.v7.app.ActionBarActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;
shuttereditz.blogspot.com
f /shuttereditz
shutter
}
public void mResult() {
NumAf = 0;
if(!Scr.getText().toString().trim().isEmpty())
NumAf = Float.parseFloat(Scr.getText().toString());
result = NumAf;
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";
}
public void getKeyboard(String str) {
String ScrTxt = Scr.getText().toString();
ScrTxt += str;
if(mod.equals("add"))
Scr.setText(ScrTxt);
else
Scr.setText(str);
mod = "add";
}
public void onButtonClick(View v) {
switch (v.getId()) {
case R.id.btnClear: //Clear
Scr.setText("");
NumberBf = 0;
Operation = "";
break;
16
shuttereditz.blogspot.com
f /shuttereditz
shutter
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
shutter
Thats it.. We have completed creating a simple calculator. Thank you for using this tutorialENJOY!
VISIT US
shuttereditz.blogspot.com
18
shuttereditz.blogspot.com