This is Android application for adding two numbers and displaying their output.Its a beginner level application and the understanding of this code will help in the implementation of other features of a basic calculator.
As we create an android project(Addition) in the eclipse environment,its application framework is defined automatically.We just need to add the code needed for the application.
Firstly modifying the .xml file in addition/res/layout/main.xml which is responsible for the layout of the application.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADDITION OF NUMBERS"
android:id="@+id/textView1"
android:layout_x="73dip"
android:layout_y="28dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_x="36dip"
android:layout_height="wrap_content"
android:text="First Amount"
android:id="@+id/textView2"
android:layout_y="80dip"></TextView>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/amount1"
android:layout_height="wrap_content"
android:layout_y="62dip"></EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Amount"
android:id="@+id/textView3"
android:layout_x="36dip"
android:layout_y="169dip"></TextView>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="169dip"
android:id="@+id/amount2"
android:layout_height="wrap_content"
android:layout_y="152dip"></EditText>
<Button android:layout_width="wrap_content"
android:id="@+id/calculate"
android:layout_x="41dip"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_y="232dip"></Button>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/tt"
android:layout_height="wrap_content"
android:layout_y="232dip"></EditText>
</AbsoluteLayout>
Now modify the addition/src/com.example.addition/addition.java as shown below:(com.example.addition is the package name)
package com.example.addition;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class test extends Activity
{
EditText amount1;
EditText amount2;
TextView tt;
Button calculate;
double x=0;
double y=0;
double z=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
amount2=(EditText)findViewById(R.id.amount2);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=x+y;
tt.setText(Double.toString(z));
}
}
No comments:
Post a Comment