Android Simple Calculator

Simple Calculator example..
I created a simple calculator  Android application to understand basic functions in Android.
 
CalculatorActivity.java
package codesstore.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 CalculatorActivity extends Activity implements OnClickListener {
    Button buttonPlus;
    Button buttonMinus;
    Button buttonDivide;
    Button buttonMultiply;
    Button buttonOk;
    TextView textAnswer;
    TextView textOperator;
    EditText num1;
    EditText num2;
    double sum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonMinus = (Button) findViewById(R.id.buttonMinus);
        buttonPlus = (Button) findViewById(R.id.buttonPlus);
        buttonDivide = (Button) findViewById(R.id.buttonDivide);
        buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
        buttonOk = (Button) findViewById(R.id.buttonOk);
        num1 = (EditText) findViewById(R.id.num1);
        num2 = (EditText) findViewById(R.id.num2);
        textAnswer = (TextView) findViewById(R.id.textAnswer);
        textOperator = (TextView) findViewById(R.id.textOperator);
        
        buttonOk.setOnClickListener(this);
        buttonPlus.setOnClickListener(this);
        buttonMinus.setOnClickListener(this);
        buttonMultiply.setOnClickListener(this);
        buttonDivide.setOnClickListener(this);

    }

    @Override
    public void onClick(View src) {

        switch (src.getId()) {
        case R.id.buttonOk:
            textAnswer.setText(String.valueOf(sum));
            break;
        case R.id.buttonPlus:
            sum = Double.parseDouble(String.valueOf(num1.getText()))
                    + Double.parseDouble(String.valueOf(num2.getText()));
            textOperator.setText("+");
            break;
        case R.id.buttonMinus:
            sum = Double.parseDouble(String.valueOf(num1.getText()))
                    - Double.parseDouble(String.valueOf(num2.getText()));
            textOperator.setText("-");
            break;
        case R.id.buttonMultiply:
            sum = Double.parseDouble(String.valueOf(num1.getText()))
                    * Double.parseDouble(String.valueOf(num2.getText()));
            textOperator.setText("*");
            break;
        case R.id.buttonDivide:
            sum = Double.parseDouble(String.valueOf(num1.getText()))
                    / Double.parseDouble(String.valueOf(num2.getText()));
            textOperator.setText("/");
            break;
        }

    }
}

main.xml
Main xml is used to create user interface.

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title" android:gravity="center" android:textSize="30dp" android:paddingTop="3dp" android:paddingBottom="10dp"/>


    <LinearLayout
        android:id="@+id/inputs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <EditText
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />



        <TextView
            android:id="@+id/textOperator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" android:gravity="center" android:textSize="25dp"/>



        <EditText
            android:id="@+id/num2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <requestFocus />
        </EditText>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/answerbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >



        <TextView
            android:id="@+id/textAnswer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp"/>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/buttonPlus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" android:layout_weight="1"/>


        <Button
            android:id="@+id/buttonMinus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" android:layout_weight="1"/>


        <Button
            android:id="@+id/buttonDivide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/" android:layout_weight="1"/>


        <Button
            android:id="@+id/buttonMultiply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*" android:layout_weight="1"/>


        <Button
            android:id="@+id/buttonOk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="=" android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>v 

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

    <string name="title">Simple Calculator</string>
    <string name="app_name">Calculator</string>

</resources>


R.java
This is a file auto generated by android.
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package codesstore.calculator;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int calculator_icon=0x7f020000;
        public static final int ic_launcher=0x7f020001;
    }
    public static final class id {
        public static final int answerbox=0x7f050004;
        public static final int buttonDivide=0x7f050009;
        public static final int buttonMinus=0x7f050008;
        public static final int buttonMultiply=0x7f05000a;
        public static final int buttonOk=0x7f05000b;
        public static final int buttonPlus=0x7f050007;
        public static final int buttons=0x7f050006;
        public static final int inputs=0x7f050000;
        public static final int num1=0x7f050001;
        public static final int num2=0x7f050003;
        public static final int textAnswer=0x7f050005;
        public static final int textOperator=0x7f050002;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int title=0x7f040000;
    }
}

Project structure in eclipse

 outline window

2 comments:

  1. I am new in android. plz let me know what s/w are be needed to execute this program. where should I write the code?

    ReplyDelete
  2. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

    ReplyDelete