Android Basic Calculation

In this tutorial, it shows on how to create a basic calculation in android. Using two edittext for user input, textview for the output and button for addition, subtraction, multiplication and division operations.
How it works?
1. Open “res/values/strings.xml” file, to add values.

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="num1">Enter First Number</string>

    <string name="num2">Enter Second Number</string>

    <string name="result">Result</string>

    <string name="app_name">BasicCalculator</string>
</resources>
 

2. Open “res/layout/main.xml” file, to add textview, edittext and button widget.

<?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" >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
   
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/num1" />
    <EditText
        android:id="@+id/txt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
   
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/num2" />
    <EditText
        android:id="@+id/txt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
   
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="@string/result" />
    <TextView
        android:id="@+id/tvresult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text=""/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/b1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="+"/>
    <Button
        android:id="@+id/b2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="-"/>
    <Button
        android:id="@+id/b3"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="*"/>
    <Button
        android:id="@+id/b4"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="/"/>
</LinearLayout>
</LinearLayout>

3. Open java source program


package icodefree.android.calculator;



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.TextView;

public class BasicCalculatorActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
  
    TextView tresult;
    Button btn1, btn2, btn3, btn4;
    EditText et1, et2;
    double num1, num2, result;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tresult=(TextView) findViewById(R.id.tvresult);
        et1=(EditText)findViewById(R.id.txt1);
        et2=(EditText)findViewById(R.id.txt2);
        btn1=(Button)findViewById(R.id.b1);
        btn2=(Button)findViewById(R.id.b2);
        btn3=(Button)findViewById(R.id.b3);
        btn4=(Button)findViewById(R.id.b4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }
    public void getInput(){
      num1=Double.parseDouble(et1.getText().toString());
      num2=Double.parseDouble(et2.getText().toString());
    }
      public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId()==R.id.b1){
                  getInput();
                  result = num1 + num2;
            }else if(v.getId()==R.id.b2){
                  getInput();
                  result = num1 - num2;
            }if(v.getId()==R.id.b3){
                  getInput();
                  result = num1 * num2;
            }if(v.getId()==R.id.b4){
                  getInput();
                  result = num1 / num2;
            }
            tresult.setText(String.valueOf(result).toString());
      }
}
 
4. Run the app

Programming Language/IDE: Java and XML / Eclipse Juno

Click Here To Download Source Code