Friday, 26 July 2013

Android : Simple calculator Application

// siddhu vydyabhushana // 4 comments
Simple Calculator example..
I created a simple calculator  Android application to understand basic functions in Android.
  
CalculatorActivity.java
  1. package codesstore.calculator;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10.   
  11. public class CalculatorActivity extends Activity implements OnClickListener {  
  12.     Button buttonPlus;  
  13.     Button buttonMinus;  
  14.     Button buttonDivide;  
  15.     Button buttonMultiply;  
  16.     Button buttonOk;  
  17.     TextView textAnswer;  
  18.     TextView textOperator;  
  19.     EditText num1;  
  20.     EditText num2;  
  21.     double sum;  
  22.   
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.   
  28.         buttonMinus = (Button) findViewById(R.id.buttonMinus);  
  29.         buttonPlus = (Button) findViewById(R.id.buttonPlus);  
  30.         buttonDivide = (Button) findViewById(R.id.buttonDivide);  
  31.         buttonMultiply = (Button) findViewById(R.id.buttonMultiply);  
  32.         buttonOk = (Button) findViewById(R.id.buttonOk);  
  33.         num1 = (EditText) findViewById(R.id.num1);  
  34.         num2 = (EditText) findViewById(R.id.num2);  
  35.         textAnswer = (TextView) findViewById(R.id.textAnswer);  
  36.         textOperator = (TextView) findViewById(R.id.textOperator);  
  37.           
  38.         buttonOk.setOnClickListener(this);  
  39.         buttonPlus.setOnClickListener(this);  
  40.         buttonMinus.setOnClickListener(this);  
  41.         buttonMultiply.setOnClickListener(this);  
  42.         buttonDivide.setOnClickListener(this);  
  43.   
  44.     }  
  45.   
  46.     @Override  
  47.     public void onClick(View src) {  
  48.   
  49.         switch (src.getId()) {  
  50.         case R.id.buttonOk:  
  51.             textAnswer.setText(String.valueOf(sum));  
  52.             break;  
  53.         case R.id.buttonPlus:  
  54.             sum = Double.parseDouble(String.valueOf(num1.getText()))  
  55.                     + Double.parseDouble(String.valueOf(num2.getText()));  
  56.             textOperator.setText("+");  
  57.             break;  
  58.         case R.id.buttonMinus:  
  59.             sum = Double.parseDouble(String.valueOf(num1.getText()))  
  60.                     - Double.parseDouble(String.valueOf(num2.getText()));  
  61.             textOperator.setText("-");  
  62.             break;  
  63.         case R.id.buttonMultiply:  
  64.             sum = Double.parseDouble(String.valueOf(num1.getText()))  
  65.                     * Double.parseDouble(String.valueOf(num2.getText()));  
  66.             textOperator.setText("*");  
  67.             break;  
  68.         case R.id.buttonDivide:  
  69.             sum = Double.parseDouble(String.valueOf(num1.getText()))  
  70.                     / Double.parseDouble(String.valueOf(num2.getText()));  
  71.             textOperator.setText("/");  
  72.             break;  
  73.         }  
  74.   
  75.     }  
  76. }  

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/title" android:gravity="center" android:textSize="30dp" android:paddingTop="3dp" android:paddingBottom="10dp"/>  
  11.   
  12.   
  13.     <LinearLayout  
  14.         android:id="@+id/inputs"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content" >  
  17.   
  18.   
  19.         <EditText  
  20.             android:id="@+id/num1"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_weight="1" />  
  24.   
  25.   
  26.   
  27.         <TextView  
  28.             android:id="@+id/textOperator"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:layout_weight="1" android:gravity="center" android:textSize="25dp"/>  
  32.   
  33.   
  34.   
  35.         <EditText  
  36.             android:id="@+id/num2"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_weight="1" >  
  40.   
  41.             <requestFocus />  
  42.         </EditText>  
  43.   
  44.     </LinearLayout>  
  45.   
  46.   
  47.     <LinearLayout  
  48.         android:id="@+id/answerbox"  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content" >  
  51.   
  52.   
  53.   
  54.         <TextView  
  55.             android:id="@+id/textAnswer"  
  56.             android:layout_width="wrap_content"  
  57.             android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp"/>  
  58.   
  59.     </LinearLayout>  
  60.   
  61.   
  62.     <LinearLayout  
  63.         android:id="@+id/buttons"  
  64.         android:layout_width="match_parent"  
  65.         android:layout_height="wrap_content" >  
  66.   
  67.         <Button  
  68.             android:id="@+id/buttonPlus"  
  69.             android:layout_width="wrap_content"  
  70.             android:layout_height="wrap_content"  
  71.             android:text="+" android:layout_weight="1"/>  
  72.   
  73.   
  74.         <Button  
  75.             android:id="@+id/buttonMinus"  
  76.             android:layout_width="wrap_content"  
  77.             android:layout_height="wrap_content"  
  78.             android:text="-" android:layout_weight="1"/>  
  79.   
  80.   
  81.         <Button  
  82.             android:id="@+id/buttonDivide"  
  83.             android:layout_width="wrap_content"  
  84.             android:layout_height="wrap_content"  
  85.             android:text="/" android:layout_weight="1"/>  
  86.   
  87.   
  88.         <Button  
  89.             android:id="@+id/buttonMultiply"  
  90.             android:layout_width="wrap_content"  
  91.             android:layout_height="wrap_content"  
  92.             android:text="*" android:layout_weight="1"/>  
  93.   
  94.   
  95.         <Button  
  96.             android:id="@+id/buttonOk"  
  97.             android:layout_width="wrap_content"  
  98.             android:layout_height="wrap_content"  
  99.             android:text="=" android:layout_weight="1"/>  
  100.   
  101.     </LinearLayout>  
  102.   
  103. </LinearLayout>v   

strings.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="title">Simple Calculator</string>  
  5.     <string name="app_name">Calculator</string>  
  6.   
  7. </resources><u><i><b>  
  8. </b></i></u>  


R.java
This is a file auto generated by android. 
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 
  2.  * 
  3.  * This class was automatically generated by the 
  4.  * aapt tool from the resource data it found.  It 
  5.  * should not be modified by hand. 
  6.  */  
  7.   
  8. package codesstore.calculator;  
  9.   
  10. public final class R {  
  11.     public static final class attr {  
  12.     }  
  13.     public static final class drawable {  
  14.         public static final int calculator_icon=0x7f020000;  
  15.         public static final int ic_launcher=0x7f020001;  
  16.     }  
  17.     public static final class id {  
  18.         public static final int answerbox=0x7f050004;  
  19.         public static final int buttonDivide=0x7f050009;  
  20.         public static final int buttonMinus=0x7f050008;  
  21.         public static final int buttonMultiply=0x7f05000a;  
  22.         public static final int buttonOk=0x7f05000b;  
  23.         public static final int buttonPlus=0x7f050007;  
  24.         public static final int buttons=0x7f050006;  
  25.         public static final int inputs=0x7f050000;  
  26.         public static final int num1=0x7f050001;  
  27.         public static final int num2=0x7f050003;  
  28.         public static final int textAnswer=0x7f050005;  
  29.         public static final int textOperator=0x7f050002;  
  30.     }  
  31.     public static final class layout {  
  32.         public static final int main=0x7f030000;  
  33.     }  
  34.     public static final class string {  
  35.         public static final int app_name=0x7f040001;  
  36.         public static final int title=0x7f040000;  
  37.     }  
  38. }  

Project structure in eclipse

 outline window

4 comments:

  1. good example...I think it is very helpful for new android developer like me.Thanks for the post...

    For some useful information about android any body can visit below link
    http://marufurrashidbd.blogspot.com/p/android.html

    ReplyDelete
  2. packers and movers noida sector 12 @ http://www.noidapackers.co.in/
    packers and movers noida sector 53 @ http://www.noidapackers.co.in/
    packers and movers noida sector 51 @ http://www.noidapackers.co.in/
    packers and movers noida sector 50 @ http://www.noidapackers.co.in/
    packers and movers noida sector 49 @ http://www.noidapackers.co.in/
    packers and movers noida sector 41 @ http://www.noidapackers.co.in/
    packers and movers noida sector 39 @ http://www.noidapackers.co.in/
    packers and movers noida sector 62 @ http://www.noidapackers.co.in/
    packers and movers noida sector 61 @ http://www.noidapackers.co.in/

    ReplyDelete
  3. Nice looking sites and great work. Pretty nice information. it has a better understanding. thanks for spending time on it.

    Best Industrial Training in Noida
    Best Industrial Training in Noida

    ReplyDelete
  4. Hadoop Training Institute in Noida, The Hadoop

    certification has considerable advantages in addition to the great demand for trained professionals. A certified Hadoop professional gets the

    advantage in terms of payment package compared to other professionals working in the field without certification. When it comes to IJP, Hadoop

    certification is compatible to advance the ladder and accelerate your career. big data hadoop training in Noida, big data hadoop Training institutes

    in Noida, sector 2, oracle big data training,Oracle Training

    in Noida
    , big data boot camp, nosql database training, big data appliance training, exalytics training, exadata course, big data introduction,

    oracle course, oracle training, big data training in Noida, ibm big data hadoop training institutes noida
    Hadoop Training Institute in Noida

    CONTAC CIITN:

    B-12, Sector - 2, Noida, U.P
    State - Uttar Pradesh U.P
    Country - India
    Pin Code - 201301

    Phone - +917290926565
    Mon - Sun: 10am - 6pm

    ReplyDelete