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”.

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.

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

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

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

8. Open the java file and paste the given code: <span style="font-family: verdana, geneva, sans-serif;">packagecom.example.calc; </span> 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); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 < span style = "font-family: verdana, geneva, sans-serif;" > packagecom . example . calc ; < / span > 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. 10. Download the calculator app for android from below link. Calculator.apk

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