In this article we are going to create a calculator application with basic functionalities such as addition, subtraction, multiplication and division.
This Android application is developed in Eclipse (4.2.1 ) with ADT plugin ( 20.0.3 ) and Android SDK (R20.0.3)
1. Create a new Android application project namely “SimpleCalculator”
2. Design application launcher icon
3. Create a blank activity
4. Enter MainActivity Details
5. Delete Android’s support library from this project, if exists
By default Eclipse ( 4.2.1) adds Android Support Library to Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”
6. Update the file res/values/strings.xml with the given below code :
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="title_activity_main">Android Simple Calculator</string> <string name="menu_settings">Settings</string> <string name="app_name">SimpleCalculator</string> <string name="hntNumber1">Enter Number1</string> <string name="hntNumber2">Enter Number2</string> <string name="lblBtnResult">Show Result</string> <string-array name="sprItems"> <item >+</item> <item >-</item> <item >x</item> <item >/</item> </string-array> </resources>
7. Update the file res/values/styles.xml
<resources> <style name="AppTheme" parent="android:Theme" /> </resources>
8. Update the file res/values-v11/styles.xml
<resources> <style name="AppTheme" parent="android:Theme" /> </resources>
9. Update the file res/values-v14/styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Holo" /> </resources>
10. Update the layout of the MainActivity in the file res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/edtNumber1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="@string/hntNumber1" /> <Spinner android:id="@+id/sprOperator" android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/sprItems" /> <EditText android:id="@+id/edtNumber2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="@string/hntNumber2" /> <Button android:id="@+id/btnResult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/lblBtnResult" /> <EditText android:id="@+id/edtResult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" /> </LinearLayout>
11. Update the MainActivity in the file src/in/wptrafficanalyzer/simplecalculator/MainActivity.java
package in.wptrafficanalyzer.simplecalculator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Defining onclick listener for finding the result OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { Spinner spr = (Spinner) findViewById(R.id.sprOperator); EditText edtNumber1 = ( EditText) findViewById(R.id.edtNumber1); EditText edtNumber2 = ( EditText) findViewById(R.id.edtNumber2); EditText edtResult = ( EditText) findViewById(R.id.edtResult); String selectedItem = (String) spr.getSelectedItem(); if(selectedItem.trim().equals("+")){ float result = Float.parseFloat(edtNumber1.getText().toString()) + Float.parseFloat(edtNumber2.getText().toString()); edtResult.setText(Float.toString(result)); }else if(selectedItem.trim().equals("-")){ float result = Float.parseFloat(edtNumber1.getText().toString()) - Float.parseFloat(edtNumber2.getText().toString()); edtResult.setText(Float.toString(result)); }else if(selectedItem.trim().equals("x")){ float result = Float.parseFloat(edtNumber1.getText().toString()) * Float.parseFloat(edtNumber2.getText().toString()); edtResult.setText(Float.toString(result)); }else if(selectedItem.trim().equals("/")){ float result = Float.parseFloat(edtNumber1.getText().toString()) / Float.parseFloat(edtNumber2.getText().toString()); edtResult.setText(Float.toString(result)); } } }; // Getting reference of the button btnResult Button btn = (Button) findViewById(R.id.btnResult); // Setting onclick listener btn.setOnClickListener(listener); } }
7. Execute the application in Android Virtual Device from eclipse ide by clicking the menu Run->Run
8. Download the application

9. Testing with Android Testing Framework
Only the 50% of the application development is completed now. The remaining 50% is with the testing of the application and it is discussed in the article titled “Android Testing Framework – Testing Calculator Application” .
10. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
Hi , i m getting error in this code…
res-menu-main.xml giving me error …please tell what to do…