Add Two Numbers
Screen
Layout
<?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"
>
<Button
android:id="@+id/btnAdd"
android:layout_width="88px"
android:layout_height="wrap_content"
android:text="Add"
android:layout_x="109px"
android:layout_y="204px"
>
</Button>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="17px"
android:text="First Number:"
android:layout_x="43px"
android:layout_y="34px"
>
</TextView>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="22px"
android:text="Second Number:"
android:layout_x="27px"
android:layout_y="76px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sum:"
android:layout_x="90px"
android:layout_y="120px"
>
</TextView>
<EditText
android:id="@+id/number2"
android:layout_width="101px"
android:layout_height="35px"
android:textSize="18sp"
android:layout_x="145px"
android:layout_y="74px"
>
</EditText>
<EditText
android:id="@+id/number1"
android:layout_width="105px"
android:layout_height="36px"
android:textSize="18sp"
android:layout_x="143px"
android:layout_y="27px"
>
</EditText>
<EditText
android:id="@+id/sum"
android:layout_width="96px"
android:layout_height="36px"
android:textSize="18sp"
android:layout_x="144px"
android:layout_y="116px"
>
</EditText>
</AbsoluteLayout>
Java Code
package com.sum.two;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class Add2Nos extends Activity {
Button button1;
EditText firstNumber, secondNumber, sumoftwo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
firstNumber = (EditText) findViewById(R.id.number1);
secondNumber = (EditText) findViewById(R.id.number2);
sumoftwo = (EditText) findViewById(R.id.sum);
button1 = (Button) findViewById(R.id.btnAdd);
button1.setOnClickListener((OnClickListener) new clicker());
}
class clicker implements Button.OnClickListener{
public void onClick(View v) {
String a,b;
Integer res;
a = firstNumber.getText().toString().trim();
b = secondNumber.getText().toString().trim();
res = Integer.parseInt(a) + Integer.parseInt(b);
sumoftwo.setText(res.toString());
}
}
}
0 comments:
Post a Comment