Simple Calculator example..
I created a simple calculator Android application to understand basic functions in Android.
CalculatorActivity.java
main.xml
Main xml is used to create user interface.
strings.xml
R.java
This is a file auto generated by android.
Project structure in eclipse
outline window
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><u><i><b>
- </b></i></u>
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
good example...I think it is very helpful for new android developer like me.Thanks for the post...
ReplyDeleteFor some useful information about android any body can visit below link
http://marufurrashidbd.blogspot.com/p/android.html
packers and movers noida sector 12 @ http://www.noidapackers.co.in/
ReplyDeletepackers 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/
Nice looking sites and great work. Pretty nice information. it has a better understanding. thanks for spending time on it.
ReplyDeleteBest Industrial Training in Noida
Best Industrial Training in Noida
Hadoop Training Institute in Noida, The Hadoop
ReplyDeletecertification 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