Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Wednesday, 13 August 2014

A Simple Adding Two Numbers

MAIN ACTIVITY

Now modify the addition/src/com.example.addition/addition.java as shown below:(com.example.addnumber is the package name)

package com.example.addnumber;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity 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.activity_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));
     }
     }


ACTIVITY_MAIN.XML

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"
android:background="@drawable/lokk"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView android:layout_width="wrap_content"
android:layout_x="36dip"
android:layout_height="wrap_content"
android:text="First Amount"
android:id="@+id/textView2"
android:textColor="#FFFFFF"
android:layout_y="80dip"></TextView>


<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:textColor="#FFFFFF"
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:textColor="#FFFFFF"
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:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_y="152dip"></EditText>


<Button android:layout_width="100dp"
android:id="@+id/calculate"
android:layout_x="41dip"
android:textColor="#FFFFFF"
android:layout_height="50dp"
android:background="@drawable/sdfr"
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:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:layout_y="232dip"></EditText>

</AbsoluteLayout>


screenshot

0 comments:

Post a Comment

Followers