Calculator
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btnClear,btnAdd,btnSub,btnMul,btnDiv,btnDot,btnEqual;
float result1,result2;
boolean add,sub,mul,div;
EditText bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
btn3=(Button) findViewById(R.id.btn3);
btn4=(Button) findViewById(R.id.btn4);
btn5=(Button) findViewById(R.id.btn5);
btn6=(Button) findViewById(R.id.btn6);
btn7=(Button) findViewById(R.id.btn7);
btn8=(Button) findViewById(R.id.btn8);
btn9=(Button) findViewById(R.id.btn9);
btn0=(Button) findViewById(R.id.btn0);
btnClear=(Button) findViewById(R.id.btnClear);
btnAdd=(Button) findViewById(R.id.btnadd);
btnSub=(Button) findViewById(R.id.btnSub);
btnMul=(Button) findViewById(R.id.btnMul);
btnDiv=(Button) findViewById(R.id.btnDiv);
btnEqual=(Button) findViewById(R.id.btnEqual);
btnDot=(Button) findViewById(R.id.btnDot);
bar=(EditText)findViewById(R.id.bar);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+"9");
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
bar.setText(bar.getText()+"0");
}
});
btnDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText(bar.getText()+".");
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bar==null){
bar.setText("");
}
else{
result1=Float.parseFloat(bar.getText()+"");
add=true;
bar.setText(null);
}
}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bar==null){
bar.setText("");
}
else{
result1=Float.parseFloat(bar.getText()+"");
sub=true;
bar.setText(null);
}
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bar==null){
bar.setText("");
}
else{
result1=Float.parseFloat(bar.getText()+"");
mul=true;
bar.setText(null);
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bar==null){
bar.setText("");
}
else{
result1=Float.parseFloat(bar.getText()+"");
div=true;
bar.setText(null);
}
}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result2=Float.parseFloat(bar.getText()+"");
if (add==true)
{
bar.setText(result1+result2+"");
add=false;
}
if (sub==true)
{
bar.setText(result1-result2+"");
sub=false;
}
if (mul==true)
{
bar.setText(result1*result2+"");
mul=false;
}
if (div==true)
{
bar.setText(result1/result2+"");
div=false;
}
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bar.setText("");
}
});
}
}
Image: