AIM: Creating a Simple Calculator with basic operations.
Solution:
1) Create a class Calculator.java
-----------------------
public class Calculator extends Activity {
private final String SDK_VERSION = "1";
private final int MENUITEM_CLOSE = 300;
/*
* Edit Text and Button object initialization for simple
* calculator design.
*/
private EditText txtCalc=null;
private Button btnZero=null;
private Button btnOne=null;
private Button btnTwo=null;
private Button btnThree=null;
private Button btnFour=null;
private Button btnFive=null;
private Button btnSix=null;
private Button btnSeven=null;
private Button btnEight=null;
private Button btnNine=null;
private Button btnPlus=null;
private Button btnMinus=null;
private Button btnMultiply=null;
private Button btnDivide=null;
private Button btnEquals=null;
private Button btnC=null;
private Button btnDecimal=null;
private Button btnMC=null;
private Button btnMR=null;
private Button btnMM=null;
private Button btnMP=null;
private Button btnBS=null;
private Button btnPerc=null;
private Button btnSqrRoot=null;
private Button btnPM=null;
private double num = 0;
private double memNum = 0;
private int operator = 1;
// 0 = nothing, 1 = plus, 2 = minus, 3 =
// multiply, 4 = divide
private boolean readyToClear = false;
private boolean hasChanged = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black);
setContentView(R.layout.calculator);
this.setTitle("SimpleCalculator " + SDK_VERSION);
initControls();
initScreenLayout();
reset();
}
private void initScreenLayout() {
/*
* The following three command lines you can use depending
* upon the emulator device you are using.
*/
// 320 x 480 (Tall Display - HVGA-P) [default]
// 320 x 240 (Short Display - QVGA-L)
// 240 x 320 (Short Display - QVGA-P)
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// this.showAlert(dm.widthPixels +" "+ dm.heightPixels, dm.widthPixels
// +" "+ dm.heightPixels, dm.widthPixels +" "+ dm.heightPixels, false);
int height = dm.heightPixels;
int width = dm.widthPixels;
if (height < 400 || width < 300) {
txtCalc.setTextSize(20);
}
if (width < 300) {
btnMC.setTextSize(18);
btnMR.setTextSize(18);
btnMP.setTextSize(18);
btnMM.setTextSize(18);
btnBS.setTextSize(18);
btnDivide.setTextSize(18);
btnPlus.setTextSize(18);
btnMinus.setTextSize(18);
btnMultiply.setTextSize(18);
btnEquals.setTextSize(18);
btnPM.setTextSize(18);
btnPerc.setTextSize(18);
btnC.setTextSize(18);
btnSqrRoot.setTextSize(18);
btnNine.setTextSize(18);
btnEight.setTextSize(18);
btnSeven.setTextSize(18);
btnSix.setTextSize(18);
btnFive.setTextSize(18);
btnFour.setTextSize(18);
btnThree.setTextSize(18);
btnTwo.setTextSize(18);
btnOne.setTextSize(18);
btnZero.setTextSize(18);
btnDecimal.setTextSize(18);
}
btnZero.setTextColor(Color.MAGENTA);
btnOne.setTextColor(Color.MAGENTA);
btnTwo.setTextColor(Color.MAGENTA);
btnThree.setTextColor(Color.MAGENTA);
btnFour.setTextColor(Color.MAGENTA);
btnFive.setTextColor(Color.MAGENTA);
btnSix.setTextColor(Color.MAGENTA);
btnSeven.setTextColor(Color.MAGENTA);
btnEight.setTextColor(Color.MAGENTA);
btnNine.setTextColor(Color.MAGENTA);
btnPM.setTextColor(Color.MAGENTA);
btnDecimal.setTextColor(Color.MAGENTA);
btnMP.setTextColor(Color.BLUE);
btnMM.setTextColor(Color.BLUE);
btnMR.setTextColor(Color.BLUE);
btnMC.setTextColor(Color.BLUE);
btnBS.setTextColor(Color.BLUE);
btnC.setTextColor(Color.RED);
btnPerc.setTextColor(Color.BLACK);
btnSqrRoot.setTextColor(Color.BLACK);
}
private void initControls() {
txtCalc = (EditText) findViewById(R.id.txtCalc);
btnZero = (Button) findViewById(R.id.btnZero);
btnOne = (Button) findViewById(R.id.btnOne);
btnTwo = (Button) findViewById(R.id.btnTwo);
btnThree = (Button) findViewById(R.id.btnThree);
btnFour = (Button) findViewById(R.id.btnFour);
btnFive = (Button) findViewById(R.id.btnFive);
btnSix = (Button) findViewById(R.id.btnSix);
btnSeven = (Button) findViewById(R.id.btnSeven);
btnEight = (Button) findViewById(R.id.btnEight);
btnNine = (Button) findViewById(R.id.btnNine);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
btnEquals = (Button) findViewById(R.id.btnEquals);
btnC = (Button) findViewById(R.id.btnC);
btnDecimal = (Button) findViewById(R.id.btnDecimal);
btnMC = (Button) findViewById(R.id.btnMC);
btnMR = (Button) findViewById(R.id.btnMR);
btnMM = (Button) findViewById(R.id.btnMM);
btnMP = (Button) findViewById(R.id.btnMP);
btnBS = (Button) findViewById(R.id.btnBS);
btnPerc = (Button) findViewById(R.id.btnPerc);
btnSqrRoot = (Button) findViewById(R.id.btnSqrRoot);
btnPM = (Button) findViewById(R.id.btnPM);
btnZero.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(0);
}
});
btnOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(1);
}
});
btnTwo.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(2);
}
});
btnThree.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(3);
}
});
btnFour.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(4);
}
});
btnFive.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(5);
}
});
btnSix.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(6);
}
});
btnSeven.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(7);
}
});
btnEight.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(8);
}
});
btnNine.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(9);
}
});
btnPlus.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(1);
}
});
btnMinus.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(2);
}
});
btnMultiply.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(3);
}
});
btnDivide.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(4);
}
});
btnEquals.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(0);
}
});
btnC.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
reset();
}
});
btnDecimal.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleDecimal();
}
});
btnPM.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handlePlusMinus();
}
});
btnMC.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = 0;
}
});
btnMR.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(memNum));
}
});
btnMM.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = memNum
- Double.parseDouble(txtCalc.getText().toString());
operator = 0;
}
});
btnMP.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = memNum
+ Double.parseDouble(txtCalc.getText().toString());
operator = 0;
}
});
btnBS.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleBackspace();
}
});
btnSqrRoot.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(Math.sqrt(Double.parseDouble(txtCalc
.getText().toString()))));
}
});
btnPerc.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(num
* (0.01 * Double.parseDouble(txtCalc.getText()
.toString()))));
}
});
txtCalc.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int i, android.view.KeyEvent e) {
if (e.getAction() == KeyEvent.ACTION_DOWN) {
int keyCode = e.getKeyCode();
// txtCalc.append("["+Integer.toString(keyCode)+"]");
switch (keyCode) {
case KeyEvent.KEYCODE_0:
handleNumber(0);
break;
case KeyEvent.KEYCODE_1:
handleNumber(1);
break;
case KeyEvent.KEYCODE_2:
handleNumber(2);
break;
case KeyEvent.KEYCODE_3:
handleNumber(3);
break;
case KeyEvent.KEYCODE_4:
handleNumber(4);
break;
case KeyEvent.KEYCODE_5:
handleNumber(5);
break;
case KeyEvent.KEYCODE_6:
handleNumber(6);
break;
case KeyEvent.KEYCODE_7:
handleNumber(7);
break;
case KeyEvent.KEYCODE_8:
handleNumber(8);
break;
case KeyEvent.KEYCODE_9:
handleNumber(9);
break;
case 43:
handleEquals(1);
break;
case KeyEvent.KEYCODE_EQUALS:
handleEquals(0);
break;
case KeyEvent.KEYCODE_MINUS:
handleEquals(2);
break;
case KeyEvent.KEYCODE_PERIOD:
handleDecimal();
break;
case KeyEvent.KEYCODE_C:
reset();
break;
case KeyEvent.KEYCODE_SLASH:
handleEquals(4);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
return false;
}
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1, MENUITEM_CLOSE, "Close");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENUITEM_CLOSE:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
String txt = Double.toString(num);
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
readyToClear = true;
hasChanged = false;
}
operator = newOperator;
}
private void handleNumber(int num) {
if (operator == 0)
reset();
String txt = txtCalc.getText().toString();
if (readyToClear) {
txt = "";
readyToClear = false;
} else if (txt.equals("0"))
txt = "";
txt = txt + Integer.toString(num);
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
hasChanged = true;
}
private void setValue(String value) {
if (operator == 0)
reset();
if (readyToClear) {
readyToClear = false;
}
txtCalc.setText(value);
txtCalc.setSelection(value.length());
hasChanged = true;
}
private void handleDecimal() {
if (operator == 0)
reset();
if (readyToClear) {
txtCalc.setText("0.");
txtCalc.setSelection(2);
readyToClear = false;
hasChanged = true;
} else {
String txt = txtCalc.getText().toString();
if (!txt.contains(".")) {
txtCalc.append(".");
hasChanged = true;
}
}
}
private void handleBackspace() {
if (!readyToClear) {
String txt = txtCalc.getText().toString();
if (txt.length() > 0) {
txt = txt.substring(0, txt.length() - 1);
if (txt.equals(""))
txt = "0";
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
}
}
}
private void handlePlusMinus() {
if (!readyToClear) {
String txt = txtCalc.getText().toString();
if (!txt.equals("0")) {
if (txt.charAt(0) == '-')
txt = txt.substring(1, txt.length());
else
txt = "-" + txt;
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
}
}
}
private void reset() {
num = 0;
txtCalc.setText("0");
txtCalc.setSelection(1);
operator = 1;
}
}
-----------------------
Create a Layout file (calculator.xml)
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
< EditText android:id="@+id/txtCalc"
android:layout_height="wrap_content" android:textSize="56sp"
android:typeface="normal" android:textStyle="normal" android:text="0"
android:gravity="bottom" android:layout_width="fill_parent" android:singleLine="true"/>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1">
< Button android:id="@+id/btnMP" android:padding="0px"
android:text="M+" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMM" android:padding="0px"
android:text="M-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMR" android:padding="0px"
android:text="MR" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMC" android:padding="0px"
android:text="MC" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnBS" android:padding="0px"
android:text="BS" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1">
< Button android:id="@+id/btnSeven" android:padding="0px"
android:text="7" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnEight" android:padding="0px"
android:text="8" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnNine" android:padding="0px"
android:text="9" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnDivide" android:padding="0px"
android:text="/" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnC" android:padding="0px"
android:text="C" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout >
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnFour" android:padding="0px"
android:text="4" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnFive" android:padding="0px"
android:text="5" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnSix" android:padding="0px"
android:text="6" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMultiply" android:padding="0px"
android:text="*" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPerc" android:padding="0px"
android:text="%" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnOne" android:padding="0px"
android:text="1" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnTwo" android:padding="0px"
android:text="2" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnThree" android:padding="0px"
android:text="3" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMinus" android:padding="0px"
android:text="-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnSqrRoot" android:padding="0px"
android:text="√" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" / >
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnZero" android:padding="0px"
android:text="0" android:gravity="center" android:layout_span="2"
android:typeface="serif" android:textStyle="bold"
android:textSize="23sp" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
// about .805
< Button android:id="@+id/btnDecimal" android:padding="0px"
android:text="." android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPM" android:padding="0px"
android:text="+/-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPlus" android:padding="0px"
android:text="+" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnEquals" android:padding="0px"
android:text="=" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" / >
< / Linear Layout >
< / Linear Layout>
Please download the working code files by clicking the below links.
Calculator.java
Calculator.xml
Output would be as follows:
Solution:
1) Create a class Calculator.java
-----------------------
public class Calculator extends Activity {
private final String SDK_VERSION = "1";
private final int MENUITEM_CLOSE = 300;
/*
* Edit Text and Button object initialization for simple
* calculator design.
*/
private EditText txtCalc=null;
private Button btnZero=null;
private Button btnOne=null;
private Button btnTwo=null;
private Button btnThree=null;
private Button btnFour=null;
private Button btnFive=null;
private Button btnSix=null;
private Button btnSeven=null;
private Button btnEight=null;
private Button btnNine=null;
private Button btnPlus=null;
private Button btnMinus=null;
private Button btnMultiply=null;
private Button btnDivide=null;
private Button btnEquals=null;
private Button btnC=null;
private Button btnDecimal=null;
private Button btnMC=null;
private Button btnMR=null;
private Button btnMM=null;
private Button btnMP=null;
private Button btnBS=null;
private Button btnPerc=null;
private Button btnSqrRoot=null;
private Button btnPM=null;
private double num = 0;
private double memNum = 0;
private int operator = 1;
// 0 = nothing, 1 = plus, 2 = minus, 3 =
// multiply, 4 = divide
private boolean readyToClear = false;
private boolean hasChanged = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black);
setContentView(R.layout.calculator);
this.setTitle("SimpleCalculator " + SDK_VERSION);
initControls();
initScreenLayout();
reset();
}
private void initScreenLayout() {
/*
* The following three command lines you can use depending
* upon the emulator device you are using.
*/
// 320 x 480 (Tall Display - HVGA-P) [default]
// 320 x 240 (Short Display - QVGA-L)
// 240 x 320 (Short Display - QVGA-P)
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// this.showAlert(dm.widthPixels +" "+ dm.heightPixels, dm.widthPixels
// +" "+ dm.heightPixels, dm.widthPixels +" "+ dm.heightPixels, false);
int height = dm.heightPixels;
int width = dm.widthPixels;
if (height < 400 || width < 300) {
txtCalc.setTextSize(20);
}
if (width < 300) {
btnMC.setTextSize(18);
btnMR.setTextSize(18);
btnMP.setTextSize(18);
btnMM.setTextSize(18);
btnBS.setTextSize(18);
btnDivide.setTextSize(18);
btnPlus.setTextSize(18);
btnMinus.setTextSize(18);
btnMultiply.setTextSize(18);
btnEquals.setTextSize(18);
btnPM.setTextSize(18);
btnPerc.setTextSize(18);
btnC.setTextSize(18);
btnSqrRoot.setTextSize(18);
btnNine.setTextSize(18);
btnEight.setTextSize(18);
btnSeven.setTextSize(18);
btnSix.setTextSize(18);
btnFive.setTextSize(18);
btnFour.setTextSize(18);
btnThree.setTextSize(18);
btnTwo.setTextSize(18);
btnOne.setTextSize(18);
btnZero.setTextSize(18);
btnDecimal.setTextSize(18);
}
btnZero.setTextColor(Color.MAGENTA);
btnOne.setTextColor(Color.MAGENTA);
btnTwo.setTextColor(Color.MAGENTA);
btnThree.setTextColor(Color.MAGENTA);
btnFour.setTextColor(Color.MAGENTA);
btnFive.setTextColor(Color.MAGENTA);
btnSix.setTextColor(Color.MAGENTA);
btnSeven.setTextColor(Color.MAGENTA);
btnEight.setTextColor(Color.MAGENTA);
btnNine.setTextColor(Color.MAGENTA);
btnPM.setTextColor(Color.MAGENTA);
btnDecimal.setTextColor(Color.MAGENTA);
btnMP.setTextColor(Color.BLUE);
btnMM.setTextColor(Color.BLUE);
btnMR.setTextColor(Color.BLUE);
btnMC.setTextColor(Color.BLUE);
btnBS.setTextColor(Color.BLUE);
btnC.setTextColor(Color.RED);
btnPerc.setTextColor(Color.BLACK);
btnSqrRoot.setTextColor(Color.BLACK);
}
private void initControls() {
txtCalc = (EditText) findViewById(R.id.txtCalc);
btnZero = (Button) findViewById(R.id.btnZero);
btnOne = (Button) findViewById(R.id.btnOne);
btnTwo = (Button) findViewById(R.id.btnTwo);
btnThree = (Button) findViewById(R.id.btnThree);
btnFour = (Button) findViewById(R.id.btnFour);
btnFive = (Button) findViewById(R.id.btnFive);
btnSix = (Button) findViewById(R.id.btnSix);
btnSeven = (Button) findViewById(R.id.btnSeven);
btnEight = (Button) findViewById(R.id.btnEight);
btnNine = (Button) findViewById(R.id.btnNine);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
btnEquals = (Button) findViewById(R.id.btnEquals);
btnC = (Button) findViewById(R.id.btnC);
btnDecimal = (Button) findViewById(R.id.btnDecimal);
btnMC = (Button) findViewById(R.id.btnMC);
btnMR = (Button) findViewById(R.id.btnMR);
btnMM = (Button) findViewById(R.id.btnMM);
btnMP = (Button) findViewById(R.id.btnMP);
btnBS = (Button) findViewById(R.id.btnBS);
btnPerc = (Button) findViewById(R.id.btnPerc);
btnSqrRoot = (Button) findViewById(R.id.btnSqrRoot);
btnPM = (Button) findViewById(R.id.btnPM);
btnZero.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(0);
}
});
btnOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(1);
}
});
btnTwo.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(2);
}
});
btnThree.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(3);
}
});
btnFour.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(4);
}
});
btnFive.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(5);
}
});
btnSix.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(6);
}
});
btnSeven.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(7);
}
});
btnEight.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(8);
}
});
btnNine.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleNumber(9);
}
});
btnPlus.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(1);
}
});
btnMinus.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(2);
}
});
btnMultiply.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(3);
}
});
btnDivide.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(4);
}
});
btnEquals.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleEquals(0);
}
});
btnC.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
reset();
}
});
btnDecimal.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleDecimal();
}
});
btnPM.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handlePlusMinus();
}
});
btnMC.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = 0;
}
});
btnMR.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(memNum));
}
});
btnMM.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = memNum
- Double.parseDouble(txtCalc.getText().toString());
operator = 0;
}
});
btnMP.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
memNum = memNum
+ Double.parseDouble(txtCalc.getText().toString());
operator = 0;
}
});
btnBS.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
handleBackspace();
}
});
btnSqrRoot.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(Math.sqrt(Double.parseDouble(txtCalc
.getText().toString()))));
}
});
btnPerc.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(num
* (0.01 * Double.parseDouble(txtCalc.getText()
.toString()))));
}
});
txtCalc.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int i, android.view.KeyEvent e) {
if (e.getAction() == KeyEvent.ACTION_DOWN) {
int keyCode = e.getKeyCode();
// txtCalc.append("["+Integer.toString(keyCode)+"]");
switch (keyCode) {
case KeyEvent.KEYCODE_0:
handleNumber(0);
break;
case KeyEvent.KEYCODE_1:
handleNumber(1);
break;
case KeyEvent.KEYCODE_2:
handleNumber(2);
break;
case KeyEvent.KEYCODE_3:
handleNumber(3);
break;
case KeyEvent.KEYCODE_4:
handleNumber(4);
break;
case KeyEvent.KEYCODE_5:
handleNumber(5);
break;
case KeyEvent.KEYCODE_6:
handleNumber(6);
break;
case KeyEvent.KEYCODE_7:
handleNumber(7);
break;
case KeyEvent.KEYCODE_8:
handleNumber(8);
break;
case KeyEvent.KEYCODE_9:
handleNumber(9);
break;
case 43:
handleEquals(1);
break;
case KeyEvent.KEYCODE_EQUALS:
handleEquals(0);
break;
case KeyEvent.KEYCODE_MINUS:
handleEquals(2);
break;
case KeyEvent.KEYCODE_PERIOD:
handleDecimal();
break;
case KeyEvent.KEYCODE_C:
reset();
break;
case KeyEvent.KEYCODE_SLASH:
handleEquals(4);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
return false;
}
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1, MENUITEM_CLOSE, "Close");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENUITEM_CLOSE:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
String txt = Double.toString(num);
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
readyToClear = true;
hasChanged = false;
}
operator = newOperator;
}
private void handleNumber(int num) {
if (operator == 0)
reset();
String txt = txtCalc.getText().toString();
if (readyToClear) {
txt = "";
readyToClear = false;
} else if (txt.equals("0"))
txt = "";
txt = txt + Integer.toString(num);
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
hasChanged = true;
}
private void setValue(String value) {
if (operator == 0)
reset();
if (readyToClear) {
readyToClear = false;
}
txtCalc.setText(value);
txtCalc.setSelection(value.length());
hasChanged = true;
}
private void handleDecimal() {
if (operator == 0)
reset();
if (readyToClear) {
txtCalc.setText("0.");
txtCalc.setSelection(2);
readyToClear = false;
hasChanged = true;
} else {
String txt = txtCalc.getText().toString();
if (!txt.contains(".")) {
txtCalc.append(".");
hasChanged = true;
}
}
}
private void handleBackspace() {
if (!readyToClear) {
String txt = txtCalc.getText().toString();
if (txt.length() > 0) {
txt = txt.substring(0, txt.length() - 1);
if (txt.equals(""))
txt = "0";
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
}
}
}
private void handlePlusMinus() {
if (!readyToClear) {
String txt = txtCalc.getText().toString();
if (!txt.equals("0")) {
if (txt.charAt(0) == '-')
txt = txt.substring(1, txt.length());
else
txt = "-" + txt;
txtCalc.setText(txt);
txtCalc.setSelection(txt.length());
}
}
}
private void reset() {
num = 0;
txtCalc.setText("0");
txtCalc.setSelection(1);
operator = 1;
}
}
-----------------------
Create a Layout file (calculator.xml)
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
< EditText android:id="@+id/txtCalc"
android:layout_height="wrap_content" android:textSize="56sp"
android:typeface="normal" android:textStyle="normal" android:text="0"
android:gravity="bottom" android:layout_width="fill_parent" android:singleLine="true"/>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1">
< Button android:id="@+id/btnMP" android:padding="0px"
android:text="M+" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMM" android:padding="0px"
android:text="M-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMR" android:padding="0px"
android:text="MR" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMC" android:padding="0px"
android:text="MC" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnBS" android:padding="0px"
android:text="BS" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1">
< Button android:id="@+id/btnSeven" android:padding="0px"
android:text="7" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnEight" android:padding="0px"
android:text="8" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnNine" android:padding="0px"
android:text="9" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnDivide" android:padding="0px"
android:text="/" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnC" android:padding="0px"
android:text="C" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout >
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnFour" android:padding="0px"
android:text="4" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnFive" android:padding="0px"
android:text="5" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnSix" android:padding="0px"
android:text="6" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMultiply" android:padding="0px"
android:text="*" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPerc" android:padding="0px"
android:text="%" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnOne" android:padding="0px"
android:text="1" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnTwo" android:padding="0px"
android:text="2" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnThree" android:padding="0px"
android:text="3" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnMinus" android:padding="0px"
android:text="-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnSqrRoot" android:padding="0px"
android:text="√" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="24sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" / >
< / LinearLayout>
< LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="1" >
< Button android:id="@+id/btnZero" android:padding="0px"
android:text="0" android:gravity="center" android:layout_span="2"
android:typeface="serif" android:textStyle="bold"
android:textSize="23sp" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
// about .805
< Button android:id="@+id/btnDecimal" android:padding="0px"
android:text="." android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPM" android:padding="0px"
android:text="+/-" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnPlus" android:padding="0px"
android:text="+" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" />
< Button android:id="@+id/btnEquals" android:padding="0px"
android:text="=" android:gravity="center" android:typeface="serif"
android:textStyle="bold" android:textSize="23sp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="1" / >
< / Linear Layout >
< / Linear Layout>
Please download the working code files by clicking the below links.
Calculator.java
Calculator.xml
Output would be as follows:
Parabens pelo post, me ajudou muito, muito e muito
ReplyDeletegood post....
ReplyDeleteVery nice article. But I've little modified it according to my need. Can u suggest me if this can be done using two variables instead of taking a single variable to store the operand i.e num
ReplyDeleteWaiting for your reply asap.
man y dont u start a youtube channel teaching us tutorials
ReplyDeletenice!
ReplyDeleteAmazing!!!!
ReplyDeletenice job. thanks
ReplyDeletethanks,Great Sharing...
ReplyDeletenothing found like this.
nice..but i got an error in this line txtCalc.setOnKeyListener(new OnKeyListener().can anyone suggest any solution?
ReplyDeleteyes, just before the next @override you will see a };
Deleteit should be });
ie. the start bracket at txtCalc.setOnKeyListener( doesn't have a matching end bracket
i think
Actually, its not that.. there's just one too many }
DeleteTry deleting the last one
You don't have to delete anything, just import android.view.View.OnKeyListener;
DeleteI have no idea...in the end I deleted all }}}
ReplyDeletecan somebody to tell the solution to this problem?! thanks
Please find the Code files at the below.
ReplyDeleteCalculatator.java(http://www.mediafire.com/?thdd892j0dt234v)
Calculator.xml (http://www.mediafire.com/view/?57emq9ci0tdnvho)
Really Nice code, Please add android:descendantFocusability="blocksDescendants" in your xml(Inside First LinearLayout). Bcoz if u r using mobile with keypad will create lots of problem.
ReplyDeleteif we want to see history on calculator ,what could be the code ?
ReplyDeletelove it modified it a little on ICS and made it look better
ReplyDeleteI loved as much as you will receive carried out right here.
ReplyDeleteThe sketch is tasteful, your authored material stylish.
nonetheless, you command get got an shakiness
over that you wish be delivering the following. unwell unquestionably come further formerly again since
exactly the same nearly a lot often inside case you shield this hike.
Here is my web-site - bmi calculation
i'd taken a distributor ship in herbalife and also i'm new to herbalife family my query is when i went sell this Herbalife Shapeworks Advance
ReplyDeleteWeight reduction System to 1 customer he is asking me a written statement that by getting this course will not
cause facet effect like kidney failure and ect.
my page - weight loss programs that really work fast
okay so with the lemon and backing soda how much of itdo i use?
ReplyDeleteMy web page; Dental Pro 7 Ebay
It's been said that men want intercourse, females want really like. Effectively, I am a woman and i want both. Do men seek adore? What is intimate to males?
ReplyDeleteStop by my webpage ways to ask girls out
I love your writing fashion along with the way you
ReplyDeleteimpart the information
my web blog ... six pack shortcuts workout plan
"if kids have interests, then education happened"?
ReplyDelete. . this the crucial of teaching kids
Look at my page :: 100 things that attract women to men
Hi Dallas. I would recommend which you verify in together with your primary physician to report these signs.
ReplyDeleteThere are many neurological and systemic circumstances that may well cause the signs and symptoms
that you describe...so I hesitate to peg them
to alcohol withdrawal. Nevertheless, your doctor
can run necessary tests, refer you to a specialist and give you
with overall peace of mind consequently.
Remember to let us know how it goes!
Here is my weblog creams to help last longer in bed
Keep this going please, great job!
ReplyDeleteTake a look at my blog post - medical office furniture catalogs
I will right away take
ReplyDeletehold of your rss feed as I can’t in finding your email subscription
hyperlink or e-newsletter service. Do you have
any?
Kindly let me recognise so that I may just subscribe.
Thanks.
Stop by my web site ... gardening tips
Heуa i’m for the firѕt time hеre.
ReplyDeleteӏ camе acгosѕ thiѕ boаrd
and I іn findіng It truly helрful & it
helρed mе out a lοt. I am hoping to proѵide somеthing аgain and aіd οthers like уοu helped mе.
Fеel frеe to visit mу web site :: core exercises for golf
I was trying this on eclipse and when i install this in device it is showing error "unfortunately the app has stopped" . I tried in emulator but same,,I am posting my logcat info..please do help..Thanks..
ReplyDeleteLogcat
10-10 19:58:32.886: D/ActivityThread(29610): setTargetHeapUtilization:0.25
10-10 19:58:32.886: D/ActivityThread(29610): setTargetHeapIdealFree:8388608
10-10 19:58:32.896: D/ActivityThread(29610): setTargetHeapConcurrentStart:2097152
10-10 19:58:32.916: W/dalvikvm(29610): threadid=1: thread exiting with uncaught exception (group=0x40aa7390)
10-10 19:58:32.926: E/AndroidRuntime(29610): FATAL EXCEPTION: main
10-10 19:58:32.926: E/AndroidRuntime(29610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bestcalculator/com.example.bestcalculator.MainActivity}: java.lang.ClassNotFoundException: com.example.bestcalculator.MainActivity
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1883)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread.access$600(ActivityThread.java:126)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.os.Looper.loop(Looper.java:137)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread.main(ActivityThread.java:4456)
10-10 19:58:32.926: E/AndroidRuntime(29610): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 19:58:32.926: E/AndroidRuntime(29610): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 19:58:32.926: E/AndroidRuntime(29610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-10 19:58:32.926: E/AndroidRuntime(29610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-10 19:58:32.926: E/AndroidRuntime(29610): at dalvik.system.NativeStart.main(Native Method)
10-10 19:58:32.926: E/AndroidRuntime(29610): Caused by: java.lang.ClassNotFoundException: com.example.bestcalculator.MainActivity
10-10 19:58:32.926: E/AndroidRuntime(29610): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
10-10 19:58:32.926: E/AndroidRuntime(29610): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-10 19:58:32.926: E/AndroidRuntime(29610): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-10 19:58:32.926: E/AndroidRuntime(29610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1874)
10-10 19:58:32.926: E/AndroidRuntime(29610): ... 11 more
Good code really help me... :)
ReplyDelete