Skip to content Skip to sidebar Skip to footer

How Do A Simple Calculator In Android? How To Do It Using Buttons And Single Edit Text?

I am learning Android so I want to keep things simple. I have to add two numbers and display the result in a single editbox. To add with the second number you have store the first

Solution 1:

If your calculator has no ( / ) and you don't allow entering negative numbers then a simple algorithm to calculate a constant input of numbers and operations could work like this:

You have one variable which represents the result (that you probably want to display each step in the EditText) and one to buffer unfinished results

if the operation is * or /: apply it to the buffer

if the operation is + or -: add buffer to result, overwrite buffer with new input

2 * 4 + 3 + 5 * 2 * 3 - 1 + 2

  • initially final result = 0, buffer = 1

  • 2 -> result = 0, buffer = 2 (careful here, I interpreted the initial step as *2)

  • * 4 -> result = 0, buffer = (2*4) = 8
  • + 3 -> result = (0+8) = 8, buffer = 3
  • + 5 -> result = (8+3) = 11, buffer = 5
  • * 2 -> result = 11, buffer = (5*2) = 10
  • * 3 -> result = 11, buffer = (10*3) = 30
  • - 1 -> result = (11+30) = 41, buffer = -1
  • + 2 -> result = (41-1) = 40, buffer = 2

-> finally (e.g. when pressing =) add both together: Answer = 42

Solution 2:

//Below having correct programming .//I did some changes in your code.This code was working nicely try this.//`import android.os.Bundle;import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

publicclassMainActivityextendsActivity {

 publicEditText display;  
TextView edt;
Integer c,d,r,b;
String a="0",aa;


Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b4=(Button)findViewById(R.id.button4);
    b5=(Button)findViewById(R.id.button5);
    b6=(Button)findViewById(R.id.button6);
    b7=(Button)findViewById(R.id.button7);
    b8=(Button)findViewById(R.id.button8);
    b9=(Button)findViewById(R.id.button9);
    b10=(Button)findViewById(R.id.button0);
    b11=(Button)findViewById(R.id.button11);
    b12=(Button)findViewById(R.id.button12);
    b13=(Button)findViewById(R.id.button13);
    b14=(Button)findViewById(R.id.button14);
    b15=(Button)findViewById(R.id.button15);
    b16=(Button)findViewById(R.id.button16);



    b1.setOnClickListener(newView.OnClickListener() {
         publicvoidonClick(View v) {
             // TODO Auto-generated method stub

             a=edt.getText().toString();
             a=a+"1";
             edt.setText(a);

         }
     });

     b2.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"2";
                     edt.setText(a);
                                         }
             });

     b3.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     a=edt.getText().toString();
                     a=a+"3";
                     edt.setText(a);
                                         }
             });


     b4.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     a=edt.getText().toString();
                     a=a+"4";
                     edt.setText(a);

                                         }
             });


     b5.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {

                     a=edt.getText().toString();
                     a=a+"5";
                     edt.setText(a);

                 }
             });


     b6.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"6";
                     edt.setText(a);
                                         }
             });


     b7.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {

                     a=edt.getText().toString();
                     a=a+"7";
                     edt.setText(a);
                                         }
             });


     b8.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"8";
                     edt.setText(a);

                 }
             });


     b9.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"9";
                     edt.setText(a);
                                         }
             });


     b10.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                     a=edt.getText().toString();
                     a=a+"0";
                     edt.setText(a);
                 }
             });


 b11.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {

                       aa=a; 
                       b=1;    
                       a="";
                       edt.setText("+");

                     edt.setText("");
          }
             });





     b12.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {

                     aa=a;
                     b=2;
                     a="";
                     edt.setText("-");
                     edt.setText("");

                 }
             });


     b13.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     aa=a;
                     b=3;
                     a="";
                     edt.setText("*");
                     edt.setText("");

                 }
             });



     b14.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     aa=a;
                     b=4;
                     a="";
                     edt.setText("/");
                     edt.setText("");

                 }
             });


     b15.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {

                     if(b==1){
                       c=Integer.parseInt(aa);

                       d=Integer.parseInt(a);             
                       r=c+d;
                     }
                     elseif(b==4){
                         c=Integer.parseInt(aa);
                       d=Integer.parseInt(a);
                       r=c/d; 
                     }elseif(b==2){
                             c=Integer.parseInt(aa);
                           d=Integer.parseInt(a);
                           r=c-d; 
                         }
                         elseif(b==3){
                                 c=Integer.parseInt(aa);
                               d=Integer.parseInt(a);
                               r=c*d; 
                             }
                     Toast.makeText(MainActivity.this, "Result is::"+r, 10000).show();
                     c=0;
                     b=0;
                     d=0;
                     a="";
                     aa="";
                     edt.setText("");
                 }
             });


     b16.setOnClickListener(newView.OnClickListener()
     {

                 publicvoidonClick(View v) {
                     // TODO Auto-generated method stub

                 edt.setText("");
                 }
             });




 }`

Solution 3:

Your above code was almost correct but small logic was missed in that programming . After clicking button it will store string values into "a" its ok . After that we click on operator symbol "a" will store into "aa" variable.

example:

a="4";
aa=a;//aa="4"//After you do like this below way .

c=Integer.parseInt(aa)+Integer.parseInt(a);
//In the above line c (storing integer values) = 4+4;//a having "4" and aa="4".

Post a Comment for "How Do A Simple Calculator In Android? How To Do It Using Buttons And Single Edit Text?"