How to Make a Calculator App for Android

Hello everyone, in this tutorial we are going to learn how to make a calculator app for android (using Eclipse). Step by step process is given in this article that will help you to make a simple calculator in very easy way.

You can also download the apk file and use it as a normal app in your android phone.

 

Steps to Make a Calculator App for Android

1. First of all open eclipse (If your are new to android programming then go to http://developer.android.com/sdk/installing/installing-adt.html to download and configure android development tools).
 

2. Create a new project > Select Android Application Project > Now name your project “Calc”. 

 How to Make a Calculator App for Android
 
3. Choose a Package Name. A Package Name is a unique URL of your Application. In most cases it is the inverse of your website. For example: com.thecrazyprogrammer.appname
 

4. If you want you can leave it to the default package name which is “com.example.calc”. Now hit next two times. 

How to Make a Calculator App for Android

 

5. If you have made an Icon then add it otherwise go with the default one. Select Blank Activity > Now name your activity “GUI”. 

How to Make a Calculator App for Android

 

6. From Palette option add total 16 buttons & name them from 0 to 9 and +, -, X, /, ., =. 

How to Make a Calculator App for Android

 

7. After completing this go to Package Explorer & under your Package Explorer Select src > Your package Name > yourActivityName.java 

How to Make a Calculator App for Android

8. Open the java file and paste the given code:

packagecom.example.calc;

import android.support.v7.app.ActionBarActivity;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;

public class GUI extends ActionBarActivity {
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdot,badd,bsub,bmul,bdiv,beq;
EditText et;
int val1,val2;
boolean add,sub,div,mul;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_gui);
  b1=(Button) findViewById(R.id.button1);
  b2=(Button) findViewById(R.id.Button01);
  b3=(Button) findViewById(R.id.Button02);
  b4=(Button) findViewById(R.id.Button03);
  b5=(Button) findViewById(R.id.Button04);
  b6=(Button) findViewById(R.id.Button05);
  b7=(Button) findViewById(R.id.Button06);
  b8=(Button) findViewById(R.id.Button07);
  b9=(Button) findViewById(R.id.Button08);
  b0=(Button) findViewById(R.id.Button09);
  bdot=(Button) findViewById(R.id.Button10);
  badd=(Button) findViewById(R.id.Button11);
  bsub=(Button) findViewById(R.id.Button12);
  bmul=(Button) findViewById(R.id.Button13);
  bdiv=(Button) findViewById(R.id.Button14);
  beq=(Button) findViewById(R.id.Button15);
  et=(EditText) findViewById(R.id.editText1);
  
  b1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"1");
   }
  });
  b2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"2");
   }
  });
  b3.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"3");
   }
  });
  b4.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"4");
   }
  });
  b5.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"5");
   }
  });
  b6.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"6");
   }
  });
  b7.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"7");
   }
  });
  b8.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"8");
   }
  });
  b9.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"9");
   }
  });
  b0.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+"0");
   }
  });
  bdot.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    et.setText(et.getText()+".");
   }
  });
  
 badd.setOnClickListener(new View.OnClickListener() {
  
  @Override
  public void onClick(View v) {
   val1=Integer.parseInt(et.getText()+"");
   add=true;
   et.setText(null);
  }
 });
bsub.setOnClickListener(new View.OnClickListener() {
  
  @Override
  public void onClick(View v) {
   val1=Integer.parseInt(et.getText()+"");
   sub=true;
   et.setText(null);
  }
 });
bdiv.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
  val1=Integer.parseInt(et.getText()+"");
  div=true;
  et.setText(null);
 }
});
bmul.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
  val1=Integer.parseInt(et.getText()+"");
  mul=true;
  et.setText(null);
 }
});

beq.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
  val2=Integer.parseInt(et.getText()+"");
  if (add==true) {
   et.setText(val1+val2+"");
   add=false; 
  }
  if (sub==true) {
   et.setText(val1-val2+"");
   sub=false; 
  }
  if (mul==true) {
   et.setText(val1*val2+"");
   mul=false; 
  }
  if (div==true) {
   et.setText(val1/val2+"");
   div=false; 
  }
 }
 
});


 }

 @Override
 publicbooleanonCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.gui, menu);
  return true;
 }

 @Override
 publicbooleanonOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  returnsuper.onOptionsItemSelected(item);
 }
}

9. Right click on your project and select run as Android Application . Now the emulator will pop up & you can test your project.
How to Make a Calculator App for Android
10. Download the calculator app for android from below link.

If you liked the tutorial then don’t forget to comment and share!

25 thoughts on “How to Make a Calculator App for Android”

  1. Int will store only non decimal values and if you are using divide and . in your calculator you should use double :). Just a view, not saying you have done it wrong.

    1. Hello md. imran please help me for android app develope,,
      how to start developing,,,
      i am presuing BCA
      in this time i am learning c.
      but i want to know android developing..

      1. Not only that it will display blank as the string is set null (i.e. ” “), if we insert “+”,”2″ or any digits it will display that.

  2. your program is very clear.. but I didnt get the output bcos it gives the result as 0 for all the operator… seems lyk something is missing… clear it plz….

    1. may be the id for each content is not good…or its matchi ng…u should check id of each content either its a button or textfield

  3. app got chrashed during entring value -20 or dividing any value by 0 eg 5/0 . there is no exception handling for this hence a bad code

  4. i followed these steps but answer is nothing…so plz add more step for every step..and check the code…if i have some problems in coding then how to solve this my self?some red signal makes under the code self…how we can removed ….plz help me and all of..

  5. I took 16 buttons and then in my XML file,I assigned 0 to 9 and then +,-,*,/,.,=
    but they are not working properly i.e,if I am clicking on 2 button but it is performing different operations

  6. This tutorial was very helpful to build my basics. I was able to build my own app for Play store. Thank you Sir.

  7. This is great way to start your android programming.But with this code,You can’t use your dot operator and even if you change your operands to Double or Floats then you cannot do more than 1 calculation in the calculator.Example you want to add 12+1 that would give you 13.0 but now if you want to add 1 more to this 13.0 without pressing Clear,the application would crash.
    But this crash doesn’t happen when you are using integer types.

  8. hi bro since your code dont follow operator precedence the result is incorrect in calculation of more than two numbers

Leave a Comment

Your email address will not be published. Required fields are marked *