Thursday, March 1, 2012

Android program for add tow number


This tutorial for simply add two number in android.

I have used two Edittext box to take input. And the onclick event listener will occur when you will click over the button.



Step1)Main java file of your android activity.


package add.number;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class AddActivity extends Activity {
               
                Button button1;
    EditText txtbox1,txtbox2;
    TextView tv;
               
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        txtbox1=  (EditText)findViewById(R.id.txtbox1);
        button1 = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.lbl1);
        txtbox2= (EditText) findViewById(R.id.txtbox2);
        button1.setOnClickListener(new clicker());
      
       
    }
   
    class clicker implements Button.OnClickListener
    {
    public void onClick(View v)
    {
                String a,b;
                Integer vis;
                a = txtbox1.getText().toString();
                b = txtbox2.getText().toString();
                vis = Integer.parseInt(a)+Integer.parseInt(b);
                tv.setText(vis.toString());
    }
    }
   
   
}

Step 2)Main.XML file of your program

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   
   
   
<EditText
android:id="@+id/txtbox1"
android:layout_width="145px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="54px"
> 

</EditText>

<EditText
android:id="@+id/txtbox2"
android:layout_width="144px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="109px"
> 
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="106px"
android:layout_height="36px"
android:text="Button"
android:layout_x="88px"
android:layout_y="224px"
> 
</Button>

<TextView
android:id="@+id/lbl1"
android:layout_width="141px"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="76px"
android:layout_y="166px"
> 
</TextView>

   
   
</LinearLayout>


1 comment:

  1. How to run this program please tell step by step
    Send aniruthan123@gmail.com

    ReplyDelete