Android Calculator

Screenshot

In this tutorial, it shows on how to create a calculator in android using eclipse.
How it works?
  1. 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" >


    <EditText
        android:id="@+id/etid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:textSize="30dp"
        android:textStyle="bold"
        android:gravity="right"
        android:hint="0" />
    <LinearLayout
        android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
          android:layout_width="225dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:enabled="false"
            android:text="iCodeFree"/>
      <Button
          android:id="@+id/bcls"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textStyle="bold"
            android:background="#100F12"
            android:text="CLR"/>
      </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
          android:id="@+id/b7"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="7"/>
      <Button
          android:id="@+id/b8"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="8"/>
      <Button
          android:id="@+id/b9"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="9"/>
      <Button
          android:id="@+id/bdiv"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textStyle="bold"
            android:background="#100F12"
            android:text="÷"/>
    </LinearLayout>
       
      <LinearLayout
        android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
          android:id="@+id/b4"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="4"/>
      <Button
          android:id="@+id/b5"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="5"/>
      <Button
          android:id="@+id/b6"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="6"/>
      <Button
          android:id="@+id/bmul"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textStyle="bold"
            android:background="#100F12"
            android:text="x"/>
    </LinearLayout>
   
      <LinearLayout
        android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
          android:id="@+id/b1"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="1"/>
      <Button
          android:id="@+id/b2"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="2"/>
      <Button
          android:id="@+id/b3"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="3"/>
      <Button
          android:id="@+id/bsub"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textStyle="bold"
            android:background="#100F12"
            android:text="-"/>
    </LinearLayout>
   
      <LinearLayout
        android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
          android:id="@+id/bdot"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="."/>
      <Button
          android:id="@+id/b0"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="0"/>
     
      <Button
          android:id="@+id/bequal"
          android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="="/>
     
      <Button
          android:id="@+id/badd"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textStyle="bold"
            android:background="#100F12"
            android:text="+"/>
    </LinearLayout>
     
</LinearLayout>
 

      2. Open java source program

package icodefree.android.calculator;

//programmer: Joemel E. Resare, MIT

//Eraser Software Solution

//http://iCodeFree.blogspot.com

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;

public class CalculatorActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
   
    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
    Button btncls, btnequal, btnadd, btnsub, btnmul, btndiv, btndot;
    EditText etdis;
    String sval1, sval2;
    double dval1, dval2, answ;
    boolean booladd, boolsub, boolmul, booldiv;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sval1="";
        sval2="";
        etdis=(EditText)findViewById(R.id.etid);
        btn0=(Button)findViewById(R.id.b0);
        btn1=(Button)findViewById(R.id.b1);
        btn2=(Button)findViewById(R.id.b2);
        btn3=(Button)findViewById(R.id.b3);
        btn4=(Button)findViewById(R.id.b4);
        btn5=(Button)findViewById(R.id.b5);
        btn6=(Button)findViewById(R.id.b6);
        btn7=(Button)findViewById(R.id.b7);
        btn8=(Button)findViewById(R.id.b8);
        btn9=(Button)findViewById(R.id.b9);
        btndot=(Button) findViewById(R.id.bdot);
        btncls=(Button)findViewById(R.id.bcls);
        btnequal=(Button)findViewById(R.id.bequal);
        btnadd=(Button)findViewById(R.id.badd);
        btnsub=(Button)findViewById(R.id.bsub);
        btnmul=(Button)findViewById(R.id.bmul);
        btndiv=(Button)findViewById(R.id.bdiv);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btncls.setOnClickListener(this);
        btnequal.setOnClickListener(this);
        btnadd.setOnClickListener(this);
        btnsub.setOnClickListener(this);
        btnmul.setOnClickListener(this);
        btndiv.setOnClickListener(this);
        btndot.setOnClickListener(this);
    }
      public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId()==R.id.b0){
                  sval2 += "0";
            }else if(v.getId()==R.id.b1){
                  sval2 += "1";
            }else if(v.getId()==R.id.b2){
                  sval2 += "2";
            }else if(v.getId()==R.id.b3){
                  sval2 += "3";
            }else if(v.getId()==R.id.b4){
                  sval2 += "4";
            }else if(v.getId()==R.id.b5){
                  sval2 += "5";
            }else if(v.getId()==R.id.b6){
                  sval2 += "6";
            }else if(v.getId()==R.id.b7){
                  sval2 += "7";
            }else if(v.getId()==R.id.b8){
                  sval2 += "8";
            }else if(v.getId()==R.id.b9){
                  sval2 += "9";
            }else if(v.getId()==R.id.bdot){
                  sval2 += ".";
            }else if(v.getId()==R.id.bcls){
                  sval2="";
                  sval1="";
            }else if(v.getId()==R.id.badd){
                  sval1=sval2;
                  sval2="";
                  booladd=true;
            }else if(v.getId()==R.id.bsub){
                  sval1=sval2;
                  sval2="";
                  boolsub=true;
            }else if(v.getId()==R.id.bmul){
                  sval1=sval2;
                  sval2="";
                  boolmul=true;
            }else if(v.getId()==R.id.bdiv){
                  sval1=sval2;
                  sval2="";
                  booldiv=true;
            }
            else if(v.getId()==R.id.bequal){
                  if(booladd==true){
                        dval1=Double.parseDouble(sval1);
                        dval2=Double.parseDouble(sval2);   
                        answ=dval1 + dval2;
                        sval2=String.valueOf(answ).toString();
                        etdis.setText(sval2);
                        booladd=false;
                        return;
                  }else if(boolsub==true){
                        dval1=Double.parseDouble(sval1);
                        dval2=Double.parseDouble(sval2);   
                        answ=dval1 - dval2;
                        sval2=String.valueOf(answ).toString();
                        etdis.setText(sval2);
                        boolsub=false;
                        return;
                  }if(boolmul==true){
                        dval1=Double.parseDouble(sval1);
                        dval2=Double.parseDouble(sval2);   
                        answ=dval1 * dval2;
                        sval2=String.valueOf(answ).toString();
                        etdis.setText(sval2);
                        boolmul=false;
                        return;
                  }if(booldiv==true){
                        dval1=Double.parseDouble(sval1);
                        dval2=Double.parseDouble(sval2);   
                        answ=dval1 / dval2;
                        sval2=String.valueOf(answ).toString();
                        etdis.setText(sval2);
                        booldiv=false;
                        return;
                  }
            }
            etdis.setText(sval2);
      }
}
Programming Language/IDE: Java and XML / Eclipse Juno
Don't Forget To Give Credits To The One Who Originally Created

1 (mga) komento:

I'm introducing to android code, I allready know Object-oriented programming in java it looks familiar but not the same, thanks for your source code.