It’s not working properly,
package com.example.devilshell.myfirstcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btnc,btnac,btn00,btnhash,btnadd,btnsub,btnmul,btndiv,btneq,btncent;
TextView tvresult;
final char Addition = '+';
final char Subtraction='-';
final char Division='/';
final char Multiplication = '*';
final char Equal = 0;
double val1 = Double.NaN;
double val2;
char Action;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0=findViewById(R.id.btn0);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn4=findViewById(R.id.btn4);
btn5=findViewById(R.id.btn5);
btn6=findViewById(R.id.btn6);
btn7=findViewById(R.id.btn7);
btn8=findViewById(R.id.btn8);
btn9=findViewById(R.id.btn9);
btn00=findViewById(R.id.btn00);
btnhash=findViewById(R.id.btnhash);
btnc=findViewById(R.id.btnc);
btnac=findViewById(R.id.btnac);
btnadd=findViewById(R.id.btna);
btnsub=findViewById(R.id.btns);
btnmul=findViewById(R.id.btnm);
btndiv=findViewById(R.id.btnd);
btneq=findViewById(R.id.buttoneq);
btncent=findViewById(R.id.btncent);
tvresult=findViewById(R.id.textView);
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText().toString()+"0");
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"9");
}
});
btn00.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tvresult.setText(tvresult.getText()+"00");
}
});
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Action=Addition;
tvresult.setText(String.valueOf(val1)+"+");
tvresult.setText(null);
}
});
btnsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Action=Subtraction;
tvresult.setText(String.valueOf(val1)+"-");
tvresult.setText(null);
}
});
btnmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Action=Multiplication;
tvresult.setText(String.valueOf(val1)+"*");
tvresult.setText(null);
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Action=Division;
tvresult.setText(String.valueOf(val1)+"/");
tvresult.setText(null);
}
});
btneq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Compute();
Action=Equal;
tvresult.setText(tvresult.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
}
});
}
private void Compute(){
if(!Double.isNaN(val1)){
val2=Double.parseDouble(tvresult.getText().toString());
switch (Action){
case Addition:
val1 = val2 + val1;
break;
case Subtraction:
val1 = val2 - val1;
break;
case Multiplication:
val1 = val2 * val1;
break;
case Division:
val1 = val1 / val2;
break;
case Equal:
break;
}
}
else {
val1=Double.parseDouble(tvresult.getText().toString());
}
}
}