title: How do a simple calculator in Android? How to do it using Buttons and single edit text?
body: <p>I am learning Android so I want to keep things simple. </p>
  
  <p>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 number in a variable. Then later add using this variable with the second number in the edit text. </p>
  
  <p>If you look at the xml and Java Code below you will have a better idea. Thanks in advance. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt;
    &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_gravity="bottom"&gt;
      &lt;Button android:text="1" android:padding="20px" android:layout_marginTop="38dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_alignParentLeft="true"&gt;&lt;/Button&gt;
      &lt;Button android:text="2" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button1" android:id="@+id/button2" android:layout_toRightOf="@+id/button1"&gt;&lt;/Button&gt;
      &lt;Button android:text="3" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button2" android:id="@+id/button3" android:layout_toRightOf="@+id/button2"&gt;&lt;/Button&gt;
      &lt;Button android:text="4" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button3" android:id="@+id/button4" android:layout_toRightOf="@+id/button3"&gt;&lt;/Button&gt;
      &lt;Button android:text="5" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button5" android:layout_below="@+id/button1" android:layout_alignParentLeft="true"&gt;&lt;/Button&gt;
      &lt;Button android:text="6" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button5" android:layout_alignLeft="@+id/button2" android:id="@+id/button6"&gt;&lt;/Button&gt;
      &lt;Button android:text="7" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button6" android:layout_alignLeft="@+id/button3" android:id="@+id/button7"&gt;&lt;/Button&gt;
      &lt;Button android:text="8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button4" android:id="@+id/button8" android:layout_below="@+id/button4"&gt;&lt;/Button&gt;
      &lt;Button android:text="=" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button11" android:id="@+id/button15" android:layout_below="@+id/button12"&gt;&lt;/Button&gt;
  &lt;Button android:text="clear" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignBottom="@+id/button15" android:layout_alignLeft="@+id/button12" android:id="@+id/button16" android:layout_below="@+id/button12"&gt;&lt;/Button&gt;
      &lt;Button android:text="-" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button12" android:layout_below="@+id/button8"&gt;&lt;/Button&gt;
      &lt;Button android:text="+" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button15" android:layout_toLeftOf="@+id/button16" android:id="@+id/button11"&gt;&lt;/Button&gt;
      &lt;Button android:text="/" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button7" android:layout_alignTop="@+id/button15" android:id="@+id/button14"&gt;&lt;/Button&gt;
      &lt;Button android:text="0" android:layout_alignRight="@+id/button14" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button14" android:id="@+id/button0"&gt;&lt;/Button&gt;
      &lt;Button android:text="*" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button10" android:layout_alignTop="@+id/button14" android:id="@+id/button13"&gt;&lt;/Button&gt;
      &lt;Button android:text="9" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button13" android:id="@+id/button9" android:layout_alignParentLeft="true"&gt;&lt;/Button&gt;
  &lt;EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/button14"&gt;&lt;/EditText&gt;
    &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Java code</p>
  
  <pre><code>package calci.tor;
  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;
  
  public class CalculatorActivity extends Activity {
      /** Called when the activity is first created. */
      public EditText display;  
      TextView edt;
      Integer c;
      String a="",b;
      //int d=0;
  
      Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.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(new View.OnClickListener() 
      {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              a=edt.getText().toString();
              edt.setText(a+ "1");
  
  
  
          }
      });
  
      b2.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      a=edt.getText().toString();
                      edt.setText(a+ "2");
                                          }
              });
  
      b3.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      a=edt.getText().toString();
                      edt.setText(a+ "3");
                                          }
              });
  
  
      b4.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      a=edt.getText().toString();
                      edt.setText(a+ "4");
  
                                          }
              });
  
  
      b5.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
  
                      a=edt.getText().toString();
                      edt.setText(a+ "5");
  
                  }
              });
  
  
      b6.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      a=edt.getText().toString();
                      edt.setText(a+ "6");
                                          }
              });
  
  
      b7.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
  
                      a=edt.getText().toString();
                      edt.setText(a+ "7");
                                          }
              });
  
  
      b8.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      a=edt.getText().toString();
                      edt.setText(a+ "8");
  
                  }
              });
  
  
      b9.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      a=edt.getText().toString();
                      edt.setText(a+ "9");
                                          }
              });
  
  
      b10.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      a=edt.getText().toString();
                      edt.setText(a+ "0");
  
                  }
              });
  
  
      b11.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                         String aa;
                        aa=a;      //To Store the first number displayed in edit text  to aa
                  edt.setText("+");
                      edt.setText("");
                      //d=1;
                      c=Integer.parseInt(aa)+Integer.parseInt(a);
  
  
  
  
                  }
              });
  
  
  
  
  
      b12.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      edt.setText("-");
  
                  }
              });
  
  
      b13.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      edt.setText("*");
                  }
              });
  
  
  
      b14.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      edt.setText("/");
                  }
              });
  
  
      b15.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                      //int c=Integer.parseInt(a)+Integer.parseInt(b);
                      //edt.setText(c);
                      display.setText(c);
                  }
              });
  
  
      b16.setOnClickListener(new View.OnClickListener()
      {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
  
                  edt.setText("");
                  }
      });
      }
  }
  </code></pre>