Saturday 30 August 2014

Start Android Development with Simple Calculator Application



SIMPLE CALCULATOR APPLICATION :


In this post I will show you how to make a simple calculator android application which can only do four operations : add, subtract, divide and multiply.



Note : To make an android application you must know the basics of java otherwise it is too difficult to understand the concept of android.

This is a simple application to start with the android development. I am making this type of simple application so that you can easily start android development.

To start with android development first download the Android SDK plus Eclipse ADT and follow the options to setup all from the following link.


After successfully installing Eclipse, open your eclipse and give a workspace. Workspace is that where you want to save your android project in computer.

Now go to the file section of the eclipse and then go to “New” and then to “other”. You must get the following screen :













Now create new Android Application Project.

Than give a name as Simple calculator and then hit Next, next and create an empty activity.




You will get this type of directories :




Now open the src file where the source files of java are stored and than go to the MainActivity.java (this activity shows when you run the application) . 

Copy the code shown below in the MainActivity :

1:  package com.calci.simplecalculator;  
2:    
3:  import android.app.Activity;  
4:  import android.os.Bundle;  
5:  import android.view.View;  
6:  import android.widget.Button;  
7:  import android.widget.EditText;  
8:  import android.widget.TextView;  
9:  import android.widget.Toast;  
10:    
11:  public class MainActivity extends Activity {  
12:    
13:       EditText number1, number2;   
14:       TextView result;  
15:       Button addButton, subButton, mulButton, divButton;  
16:    @Override  
17:    protected void onCreate(Bundle savedInstanceState) {  
18:      super.onCreate(savedInstanceState);  
19:      setContentView(R.layout.activity_main);  
20:        
21:      //initialize variables  
22:      number1 = (EditText)findViewById(R.id.number1);  
23:      number2 = (EditText)findViewById(R.id.number2);  
24:      result = (TextView)findViewById(R.id.result);  
25:      addButton = (Button)findViewById(R.id.addButton);  
26:      subButton = (Button)findViewById(R.id.subButton);  
27:      mulButton = (Button)findViewById(R.id.mulButton);  
28:      divButton = (Button)findViewById(R.id.divButton);  
29:        
30:        
31:      //listener on add button  
32:      addButton.setOnClickListener(new View.OnClickListener() {  
33:                   
34:                 @Override  
35:                 public void onClick(View v) {  
36:                      // TODO Auto-generated method stub  
37:                      try{  
38:                           String n1 = number1.getText().toString();  
39:                           String n2 = number2.getText().toString();  
40:                           String sum = String.valueOf(((Integer.parseInt(n1)) + (Integer.parseInt(n2))));  
41:                           result.setText(sum + " (Sum)");  
42:                 }catch(Exception e){  
43:                      Toast.makeText(MainActivity.this, "Please enter two numbers to calculate the sum", Toast.LENGTH_LONG).show();  
44:                      e.printStackTrace();  
45:                 }  
46:                 }  
47:            });  
48:        
49:      //listener on sub button  
50:      subButton.setOnClickListener(new View.OnClickListener() {  
51:                   
52:           @Override  
53:                 public void onClick(View v) {  
54:                      // TODO Auto-generated method stub  
55:                      try{  
56:                           String n1 = number1.getText().toString();  
57:                           String n2 = number2.getText().toString();  
58:                           String sub = String.valueOf(((Integer.parseInt(n1)) - (Integer.parseInt(n2))));  
59:                           result.setText(sub + " (Subtraction)");  
60:                 }catch(Exception e){  
61:                      Toast.makeText(MainActivity.this, "Please enter two numbers to calculate the subtraction", Toast.LENGTH_LONG).show();  
62:                      e.printStackTrace();  
63:                 }  
64:                 }  
65:            });  
66:        
67:      //listener on mul button  
68:      mulButton.setOnClickListener(new View.OnClickListener() {  
69:                   
70:           @Override  
71:                 public void onClick(View v) {  
72:                      // TODO Auto-generated method stub  
73:                      try{  
74:                           String n1 = number1.getText().toString();  
75:                           String n2 = number2.getText().toString();  
76:                           String mul = String.valueOf(((Integer.parseInt(n1)) * (Integer.parseInt(n2))));  
77:                           result.setText(mul + " (Multiplication)");  
78:                 }catch(Exception e){  
79:                      Toast.makeText(MainActivity.this, "Please enter two numbers to calculate the multiplication", Toast.LENGTH_LONG).show();  
80:                      e.printStackTrace();  
81:                 }  
82:                 }  
83:            });  
84:        
85:      //listener on div button  
86:      divButton.setOnClickListener(new View.OnClickListener() {  
87:                   
88:           @Override  
89:                 public void onClick(View v) {  
90:                      // TODO Auto-generated method stub  
91:                      try{  
92:                           String n1 = number1.getText().toString();  
93:                           String n2 = number2.getText().toString();  
94:                           String div = String.valueOf(((Integer.parseInt(n1)) / (Integer.parseInt(n2))));  
95:                           result.setText(div + " (Division)");  
96:                 }catch(Exception e){  
97:                      Toast.makeText(MainActivity.this, "Please enter two numbers to calculate the division", Toast.LENGTH_LONG).show();  
98:                      e.printStackTrace();  
99:                 }  
100:                 }  
101:            });  
102:    }  
103:    
104:         
105:      
106:      
107:  }  
108:    


You will get the errors with red lines when the button and edit texts are mapped from xml file to java file because we do not have buttons and edit texts in xml file till now.

In this source file we set the onClickListener on the add, sub, mul and div buttons. The whole logic is that we get the number entered by user when the user presses any button in the application and than these numbers will be added, subtracted, divide or multiply according to the button clicked by the user.

Now coming to the designing part i.e., xml file. 
Go to the res folder than go to layout and open the file named as “activity_main.xml”. Copy the following code in the xml in which we give the ids of buttons and edit texts (in “android:id=”@+id/idname” statement of any object like button and edit text)which will mapped in the java source file. When you save xml file you can see all the errors are removed in the java file.

1:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2:    xmlns:tools="http://schemas.android.com/tools"  
3:    android:layout_width="match_parent"  
4:    android:layout_height="match_parent"  
5:    android:orientation="vertical"  
6:     >  
7:    
8:    <TextView   
9:      android:layout_width="wrap_content"  
10:      android:layout_height="wrap_content"  
11:      android:text="SIMPLE CALCULATOR"  
12:      android:textSize="20sp"  
13:      android:layout_gravity="center"  
14:      />  
15:    <LinearLayout   
16:      android:layout_width="fill_parent"  
17:      android:layout_height="wrap_content"  
18:      android:orientation="horizontal"  
19:      android:layout_marginTop="5dp"  
20:      >  
21:    <TextView   
22:      android:layout_width="wrap_content"  
23:      android:layout_height="wrap_content"  
24:      android:text="Number 1"  
25:      android:layout_gravity="center"  
26:      android:layout_marginLeft="10dp"  
27:      android:textSize="18sp"  
28:      />  
29:    <EditText  
30:      android:id="@+id/number1"  
31:      android:layout_width="wrap_content"  
32:      android:layout_height="wrap_content"  
33:      android:layout_marginLeft="4dp"  
34:      android:ems="10"  
35:      android:inputType="number" >  
36:    
37:        
38:    </EditText>  
39:  </LinearLayout>  
40:    <LinearLayout   
41:      android:layout_width="fill_parent"  
42:      android:layout_height="wrap_content"  
43:      android:orientation="horizontal"  
44:      android:layout_marginTop="5dp"  
45:      >  
46:    <TextView   
47:      android:layout_width="wrap_content"  
48:      android:layout_height="wrap_content"  
49:      android:text="Number 2"  
50:      android:layout_gravity="center"  
51:      android:layout_marginLeft="10dp"  
52:      android:textSize="18sp"  
53:      />  
54:    <EditText  
55:      android:id="@+id/number2"  
56:      android:layout_width="wrap_content"  
57:      android:layout_height="wrap_content"  
58:      android:layout_marginLeft="4dp"  
59:      android:ems="10"  
60:      android:inputType="number" >  
61:    
62:        
63:    </EditText>  
64:  </LinearLayout>  
65:    <LinearLayout   
66:      android:layout_width="fill_parent"  
67:      android:layout_height="wrap_content"  
68:      android:orientation="horizontal"  
69:      android:layout_marginTop="5dp"  
70:      >  
71:    <TextView   
72:      android:layout_width="wrap_content"  
73:      android:layout_height="wrap_content"  
74:      android:text="Result : "  
75:      android:layout_gravity="center"  
76:      android:layout_marginLeft="10dp"  
77:      android:textSize="18sp"  
78:      />  
79:    <TextView  
80:      android:id="@+id/result"  
81:      android:layout_width="wrap_content"  
82:      android:layout_height="wrap_content"  
83:      android:layout_marginLeft="10dp"  
84:      android:layout_gravity="center"  
85:      android:textSize="18sp"  
86:      >  
87:    
88:        
89:    </TextView>  
90:  </LinearLayout>  
91:  <LinearLayout   
92:      android:layout_width="fill_parent"  
93:      android:layout_height="wrap_content"  
94:      android:orientation="horizontal"  
95:      android:layout_marginTop="5dp"  
96:      android:weightSum="1"  
97:      >  
98:    <Button  
99:      android:id="@+id/addButton"  
100:      android:layout_weight="0.5"  
101:      style="?android:attr/buttonStyleSmall"  
102:      android:layout_width="fill_parent"  
103:      android:layout_height="wrap_content"  
104:      android:layout_gravity="center"  
105:      android:layout_marginTop="10dp"  
106:      android:text="ADD" />  
107:      
108:    <Button  
109:      android:id="@+id/subButton"  
110:      android:layout_weight="0.5"  
111:      style="?android:attr/buttonStyleSmall"  
112:      android:layout_width="fill_parent"  
113:      android:layout_height="wrap_content"  
114:      android:layout_gravity="center"  
115:      android:layout_marginTop="10dp"  
116:      android:text="SUBTRACT" />  
117:    </LinearLayout>  
118:    
119:  <LinearLayout   
120:      android:layout_width="fill_parent"  
121:      android:layout_height="wrap_content"  
122:      android:orientation="horizontal"  
123:      android:layout_marginTop="5dp"  
124:      android:weightSum="1"  
125:      >  
126:    <Button  
127:      android:id="@+id/mulButton"  
128:      android:layout_weight="0.5"  
129:      style="?android:attr/buttonStyleSmall"  
130:      android:layout_width="fill_parent"  
131:      android:layout_height="wrap_content"  
132:      android:layout_gravity="center"  
133:      android:layout_marginTop="10dp"  
134:      android:text="MULTIPLY" />  
135:      
136:    <Button  
137:      android:id="@+id/divButton"  
138:      android:layout_weight="0.5"  
139:      style="?android:attr/buttonStyleSmall"  
140:      android:layout_width="fill_parent"  
141:      android:layout_height="wrap_content"  
142:      android:layout_gravity="center"  
143:      android:layout_marginTop="10dp"  
144:      android:text="DIVIDE" />  
145:    </LinearLayout>  
146:    
147:  </LinearLayout>  


Xml file is used for the designing purposes in the android application development and java file is used for implementing the business logic.


Now for running the application, right click on the project in the package explorer and go to “Run as” and than “1 android application”. You will be asked to create the emulator if you don’t have before than just create the emulator and run this application. Enjoy your first calculator application in android.

3 comments :

  1. Nice blog as usual and this time I will going to use this project as my android development project.

    ReplyDelete
  2. Thanks a lot for sharing such valuable information. No one would have thought that a simple calculator requires that much complex coding and a lot more.

    ReplyDelete
  3. Thanks for sharing this useful content. hire android developer to build a robust and scalable app suitable for your business requirements at the best price.

    web design and development services

    ReplyDelete