CALCULATOR APP
/********************* CalcActivity.java *********************/
package
com.android.calc;
import
android.app.Activity;
import
android.content.SharedPreferences;
import
android.content.SharedPreferences.Editor;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
public
class CalcActivity extends Activity implements OnClickListener{
public
static final String PREFS_NAME = "MyPrefsFile";
EditText
etNumber;
String
numberString;
String
lastOperator;
int
value2;
String
opr;
int
lastNum;
SharedPreferences
settings;
SharedPreferences.Editor
editor;
boolean
isAnswerIN = false;
@Override
protected
void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
settings
= getSharedPreferences(PREFS_NAME, 0);
editor
= settings.edit();
getViews();
numberString
= etNumber.getText().toString();
}
private
void getViews() {
etNumber
= (EditText) findViewById(R.id.number);
((Button)findViewById(R.id.btn_zero)).setOnClickListener(this);
((Button)findViewById(R.id.btn_one)).setOnClickListener(this);
((Button)findViewById(R.id.btn_two)).setOnClickListener(this);
((Button)findViewById(R.id.btn_three)).setOnClickListener(this);
((Button)findViewById(R.id.btn_four)).setOnClickListener(this);
((Button)findViewById(R.id.btn_five)).setOnClickListener(this);
((Button)findViewById(R.id.btn_six)).setOnClickListener(this);
((Button)findViewById(R.id.btn_seven)).setOnClickListener(this);
((Button)findViewById(R.id.btn_eight)).setOnClickListener(this);
((Button)findViewById(R.id.btn_nine)).setOnClickListener(this);
((Button)findViewById(R.id.btn_add)).setOnClickListener(this);
((Button)findViewById(R.id.btn_sub)).setOnClickListener(this);
((Button)findViewById(R.id.btn_mul)).setOnClickListener(this);
((Button)findViewById(R.id.btn_slash)).setOnClickListener(this);
((Button)findViewById(R.id.btn_sqrt)).setOnClickListener(this);
((Button)findViewById(R.id.btn_answer)).setOnClickListener(this);
((Button)findViewById(R.id.btn_ac)).setOnClickListener(this);
((Button)findViewById(R.id.backSpace)).setOnClickListener(this);
}
@Override
public
void onClick(View v) {
switch
(v.getId()) {
case
R.id.btn_zero:
getDisplay(0);
break;
case
R.id.btn_one:
getDisplay(1);
break;
case
R.id.btn_two:
getDisplay(2);
break;
case
R.id.btn_three:
getDisplay(3);
break;
case
R.id.btn_four:
getDisplay(4);
break;
case
R.id.btn_five:
getDisplay(5);
break;
case
R.id.btn_six:
getDisplay(6);
break;
case
R.id.btn_seven:
getDisplay(7);
break;
case
R.id.btn_eight:
getDisplay(8);
break;
case
R.id.btn_nine:
getDisplay(9);
break;
case
R.id.btn_add:
lastOperator
= "+";
save();
break;
case
R.id.btn_sub:
lastOperator
= "-";
save();
break;
case
R.id.btn_mul:
lastOperator
= "*";
save();
break;
case
R.id.btn_slash:
lastOperator
= "/";
save();
break;
case
R.id.btn_answer:
compute();
break;
case
R.id.btn_ac:
start();
break;
default:
break;
}
}
public
void getDisplay(int id) {
numberString
= etNumber.getText().toString();
if(numberString.trim().equals("0")
&& isAnswerIN == true){
numberString
= ""+id ;
}else{
numberString
= numberString + id ;
}
etNumber.setText(numberString);
Log.d("\n
********** numberString = "+ numberString, "");
}
private
void save() {
isAnswerIN
= false;
numberString
= etNumber.getText().toString();
lastNum
= Integer.parseInt(numberString);
editor.putInt("lastnumber",
lastNum);
editor.putString("lastOpr",
lastOperator);
etNumber.setText(String.valueOf(""));
Log.d("\n
IN SAVE ********* LASTNUM = "+ lastNum, "LASTOPR = " +
lastOperator);
}
private
void start() {
lastNum
= 0;
lastOperator
= "";
editor.putInt("lastnumber",
lastNum);
editor.putString("lastOpr",
lastOperator);
etNumber.setText(String.valueOf(0));
Log.d("\n
IN START ********* " , "LASTNUM = " + lastNum);
}
private
void compute() {
int
answer;
if(isAnswerIN
== false){
numberString
= etNumber.getText().toString();
if(!numberString.trim().equals(""))
value2
= Integer.parseInt(numberString);
else
value2
= 0;
}
lastNum
= settings.getInt("lastnumber", lastNum);
opr
= settings.getString("lastOpr", lastOperator);
if
(opr == "+") {
answer
= lastNum + value2;
}else
if(opr == "-"){
answer
= lastNum - value2;
}else
if(opr == "*"){
answer
= lastNum * value2;
}else
if(opr == "/"){
answer
= lastNum /value2;
}else{
answer
= value2;
}
etNumber.setText(String.valueOf(answer));
editor.putInt("value2",
value2);
lastNum
= answer;
editor.putInt("lastnumber",
lastNum);
isAnswerIN
= true;
Log.d("\n
IN COMPUTE ******** result = "+ value2 ,"lastnum = "+
lastNum+ "answer = " +answer);
}
}
/*********************
main.xml *********************/
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/backSpace"
android:text="Backspace"
android:layout_width="180dp"
android:layout_height="60dp"
android:layout_below="@+id/number"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_ac"
android:text="AC"
android:layout_width="140dp"
android:layout_height="60dp"
android:layout_below="@+id/number"
android:layout_toRightOf="@+id/backSpace"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_seven"
android:text="7"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/backSpace"
/>
<Button
android:id="@+id/btn_eight"
android:text="8"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/backSpace"
android:layout_toRightOf="@+id/btn_seven"
/>
<Button
android:id="@+id/btn_nine"
android:text="9"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_ac"
android:layout_toRightOf="@+id/btn_eight"
/>
<Button
android:id="@+id/btn_slash"
android:text="/"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_ac"
android:layout_toRightOf="@+id/btn_nine"
/>
<Button
android:id="@+id/btn_sqrt"
android:text="sqrt"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_ac"
android:layout_toRightOf="@+id/btn_slash"
/>
<Button
android:id="@+id/btn_four"
android:text="4"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_seven"
android:layout_toLeftOf="@+id/btn_eight"
/>
<Button
android:id="@+id/btn_five"
android:text="5"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_eight"
android:layout_toRightOf="@+id/btn_four"
/>
<Button
android:id="@+id/btn_six"
android:text="6"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_nine"
android:layout_toRightOf="@+id/btn_five"
/>
<Button
android:id="@+id/btn_mul"
android:text="*"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_slash"
android:layout_toRightOf="@+id/btn_six"
/>
<Button
android:id="@+id/btn_oneByX"
android:text="1/x"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_sqrt"
android:layout_toRightOf="@+id/btn_mul"
/>
<Button
android:id="@+id/btn_one"
android:text="1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_four"
/>
<Button
android:id="@+id/btn_two"
android:text="2"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_five"
android:layout_toRightOf="@+id/btn_one"
/>
<Button
android:id="@+id/btn_three"
android:text="3"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_six"
android:layout_toRightOf="@+id/btn_two"
/>
<Button
android:id="@+id/btn_sub"
android:text="-"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_mul"
android:layout_toRightOf="@+id/btn_three"
/>
<Button
android:id="@+id/btn_add"
android:text="+"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_oneByX"
android:layout_toRightOf="@+id/btn_sub"
/>
<Button
android:id="@+id/btn_zero"
android:text="0"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/btn_one"
/>
<Button
android:id="@+id/btn_answer"
android:text="Answer"
android:layout_width="350px"
android:layout_height="64dp"
android:layout_below="@+id/btn_three"
android:layout_toRightOf="@+id/btn_zero"/>
</RelativeLayout>
No comments:
Post a Comment