Calculator example in Android

Below is an example on how to develop Calculator example on Android platform.
android-calculator-example

This Application is used to perform arithmetic operations on two integers, which need to be entered in the first two EditText views.
Select the operation you want to perform.
Result is displayed in the last EditText.

Below is screen shot of the App.

XML Layout file main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/firsNum"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/secondNum"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/add"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="Sub"
android:id="@+id/sub"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="Div"
android:id="@+id/div"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="91dp"
android:layout_height="wrap_content"
android:text="Mul"
android:id="@+id/mul"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"/>
</LinearLayout>

Below is MyActivity.java file

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends Activity implements View.OnClickListener{

    private Button add;
    private Button sub;
    private Button mul;
    private Button div;
    private EditText firstNum;
    private EditText secondNum;
    private EditText results;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        add=(Button)findViewById(R.id.add);
        sub=(Button)findViewById(R.id.sub);
        mul=(Button)findViewById(R.id.mul);
        div=(Button)findViewById(R.id.div);
        firstNum=(EditText)findViewById(R.id.firsNum);
        secondNum=(EditText)findViewById(R.id.secondNum);
        results=(EditText)findViewById(R.id.result);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
    }
    public void onClick(View v){
        int num1=Integer.parseInt(firstNum.getText().toString());
        int num2=Integer.parseInt(secondNum.getText().toString());
        if(v.getId() == R.id.add){
         int result=num1+num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.sub){
            int result=num1-num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.mul){
            int result=num1*num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.div){
            int result=num1/num2;
            results.setText(String.valueOf(result));
        }
    }
}

You may also like to read:
In Android, what is px,dp,dip and sp?
In Android, what is difference between fill_parent and match_parent