I have made a calculator app which the code is of the following:
package com.example.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edit1, edit2;
Button btnAdd, btnSub, btnMul, btnDiv, btnSin, btnCos, btnTan, btnSqrt, btnSqr, btnFact, btnPi, btnE, btnLog, btnLn, btnMod, btnInt, btnAsin, btnAcos, btnAtan, btnSinh, btnCosh, btnTanh, btn3root, btnNroot, btnSin1, btnCos1, btnTan1, btnSinh1, btnCosh1, btnTanh1;
Double result;
String num1, num2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Easy Scientific Calculator");
edit1=(EditText)findViewById(R.id.Edit1);
edit2=(EditText)findViewById(R.id.Edit2);
btnAdd=(Button)findViewById(R.id.BtnAdd);
btnSub=(Button)findViewById(R.id.BtnSub);
btnMul=(Button)findViewById(R.id.BtnMul);
btnDiv=(Button)findViewById(R.id.BtnDiv);
btnSin=(Button)findViewById(R.id.BtnSin);
btnCos=(Button)findViewById(R.id.BtnCos);
btnTan=(Button)findViewById(R.id.BtnTan);
btnSqrt=(Button)findViewById(R.id.BtnSqrt);
btnSqr=(Button)findViewById(R.id.BtnSqr);
btnFact=(Button)findViewById(R.id.BtnFact);
btnPi=(Button)findViewById(R.id.BtnPi);
btnE=(Button)findViewById(R.id.BtnE);
btnLog=(Button)findViewById(R.id.BtnLog);
btnLn=(Button)findViewById(R.id.BtnLn);
btnMod=(Button)findViewById(R.id.BtnMod);
btnInt=(Button)findViewById(R.id.BtnInt);
btnAsin=(Button)findViewById(R.id.BtnAsin);
btnAcos=(Button)findViewById(R.id.BtnAcos);
btnAtan=(Button)findViewById(R.id.BtnAtan);
btnSinh=(Button)findViewById(R.id.BtnSinh);
btnCosh=(Button)findViewById(R.id.BtnCosh);
btnTanh=(Button)findViewById(R.id.BtnTanh);
btn3root=(Button)findViewById(R.id.Btn3root);
btnNroot=(Button)findViewById(R.id.BtnNroot);
btnSin1=(Button)findViewById(R.id.BtnSin1);
btnCos1=(Button)findViewById(R.id.BtnCos1);
btnTan1=(Button)findViewById(R.id.BtnTan1);
btnSinh1=(Button)findViewById(R.id.BtnSinh1);
btnCosh1=(Button)findViewById(R.id.BtnCosh1);
btnTanh1=(Button)findViewById(R.id.BtnTanh1);
btnAdd.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Double.parseDouble(num1)+Double.parseDouble(num2);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSub.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Double.parseDouble(num1)-Double.parseDouble(num2);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnMul.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Double.parseDouble(num1)*Double.parseDouble(num2);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnDiv.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Double.parseDouble(num1)/Double.parseDouble(num2);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSin.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.sin((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnCos.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.cos((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnTan.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.tan((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSqrt.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.sqrt(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSqr.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Math.pow(Double.parseDouble(num1), Double.parseDouble(num2));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnFact.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
Double r=1.0;
Double v=Double.parseDouble(num1);
for (int i = 1; i <= v; i++)
r *= i;
result=r;
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnPi.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
result=Math.PI;
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnE.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
result=Math.E;
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnLog.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Math.log10(Double.parseDouble(num1))/Math.log10(Double.parseDouble(num2));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnLn.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.log(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnMod.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=(Double.parseDouble(num1))%(Double.parseDouble(num2));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnInt.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.floor(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnAsin.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=57.29577951308232087679815481410517033240547246657*Math.asin(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnAcos.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=57.29577951308232087679815481410517033240547246657*Math.acos(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnAtan.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=57.29577951308232087679815481410517033240547246657*Math.atan(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSinh.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.sinh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnCosh.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.cosh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnTanh.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.tanh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btn3root.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.pow((Double.parseDouble(num1)), 1.0/3);
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnNroot.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
num2=edit2.getText().toString();
result=Math.pow((Double.parseDouble(num1)), 1.0/(Double.parseDouble(num2)));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSin1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.sin((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnCos1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.cos((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnTan1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.tan((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnSinh1.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.sinh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnTanh.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.cosh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
btnTanh.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=1/(Math.tanh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
}
}
I got a problem with this that if one of the EditTexts do not have text, it gets a stackoverflow. I am a beginner, and a middle-schooler, and I have hunted for information, but I couldn't find it. Please don't deduct points from me.
How do I stop the application in the middle and send a message in EditText edit1 that it says "More Variables Needed"? Please example it in the sqrt below. the number comes from edit1 and should be outputted to edit1 too. Thank You.
btnSqrt.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
int action = arg1.getActionMasked();
if(action==MotionEvent.ACTION_UP)
{
num1=edit1.getText().toString();
result=Math.sqrt(Double.parseDouble(num1));
edit1.setText(Double.toString(result));
return false;
}
else
return false;
}
});
Instead of using setOnTouchListener
you can try setOnClickListener
so you could avoid multiple calls of the calculation process.
Also you can perform a check if one editText
is empty.
Try TextUtils.isEmpty(string)