Search

Monday, June 8, 2009

Sample Android Program to add two numbers

A program to demonstrate usage of EditText, Textview and Button widgets in android this program is very useful for beginners and give the basic overview on how to use the Api's, using Droiddraw i was able to design the User Interface with two textbox(EditText) and Label(TextView) and button as shown in below Pic

below is the XML for above UI generated from DroidDraw

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<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>
</AbsoluteLayout>

and below is the Java Source code which contains the logic to add two numbers and the refrence to id of the controls can be found in above XML

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 fit 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());
}
}
}




in above code we have OnClickListener which is similar to ActionListener in java swing values are taken from the EditText(textbox) and added and the result is showed in TextView(label).

61 comments:

Mahesh said...

Thanks a lot it really helped me

Anonymous said...

Thanks for the simple yet effective tutorial!

venky said...

Thanks for the tutorial...
This is the first app I did..(ofcourse copy pasted..)

but for some reason if i give 3 digit numbers to add it crashes..
can you help me out in this..

Anonymous said...

Sorry for the above comment..
may be i just gave the wrong input
might be it had some non numbers..
It works fine..

Anonymous said...

Thanks for this.
i am a new in android development, this
is very helpful for me.

Anonymous said...

Very useful..thank u

Anamika Dev said...
This comment has been removed by the author.
Anonymous said...

good one! Thank you.

Anonymous said...

thnx pal..

Anonymous said...

Good one friend...I was just looking for such a tutorial..

Sankar

Anonymous said...

am getting same scree for all the programs i run could u please help me...

Anonymous said...

am getting error at R.id wat may be the reason?

Mahmudul Hasan Autul said...

nice one.........really helped me

Unknown said...

nice one ..keep posting :).tnx

suraj said...

i do this program but it is not properly run when the android virtual screen display it does not show my application please solb my problem

Gurnoorinder Singh said...

hello,
I am using the same type of example to save the values in database, but there seems to be some problem with EDITTEXT. my program is running fine but it is not saving the value of the edittext field in the database. it saves null instead. when i save the value by hard coding it. it successfully saves it.

Can anyone help?

Anonymous said...

I tried to do the same program using Droiddraw.But the program is showing the error "id cannot be resolved for txtbox1,txtbox2,button1 and button2 - four errors..Please help

Anonymous said...

Hi Manjunath,

Thanks for sharing such a good and easy tutorial on Android for beginners.

Thanks.

Binod Suman
Bangalore

Anonymous said...

Dear All,

Can anybody give me the ebook link of Android for beginners.

Thanks in Advance....
Pawan Jha
pankajace12@gmail.com
New Delhi.

Anonymous said...

hi bro, i just start anroid developing ..but i stuggle with java programming .....pls give me some idea...easier way to learn java programming .....so long i try to learn java programming ...i could not make it ....pls help how to learn ....

thank you

Anonymous said...

thnku...

Anonymous said...

can anybody give me ebook on android for beginner
at mohit.gupta108@gmail.com



Thanks in advance

siva said...

any one help me to execution steps... because i have some execution error in my program

Anonymous said...

thanks for tutorials

YUV said...

thnxxx :))

E.H.D Madari said...

this example is so nice and easy.................
thanks............

Raj said...

Thank you for sharing this wonderful example with us...

Gani said...

Thank you very much for giving such a nice error free example................

Anonymous said...

Very helpful

Anonymous said...

I don't know why but I keep getting an exception whenever I initialize the text boxes

Anonymous said...

B4 u run this application add this code in r.java

public static final class id {
public static final int button1=0x7f050003;
public static final int lbl1=0x7f050004;
public static final int txtbox1=0x7f050001;
public static final int txtbox2=0x7f050002;
public static final int widget35=0x7f050000;
}

rupesh said...

thnx for this tutorial.it helps me a lot....we r expecting some more such tutorials.please consider....

Anonymous said...

thanks sir... i will love to see more program

Swagee said...

Can anybody give me the ebook link of Android for beginners....:)

swageetha,
j.swag96@gmail.com

Anonymous said...

Thanks a lot.

shishir said...

It was very useful..Thanks a lot

SatyamReddy said...

Hi all,

I want more examples like above.

Nilesh Bhadane said...

its realy to good...

Nilesh Bhadane said...

its realy to good

Anonymous said...

hii majunath bhai,

aa tame aapelo program to saro halyo, ha ema number pachi space aapye to dakkho thay 6, so, ene kem overkum karvu e kejo time mle to ne mari bhasha samjay to ..

Anonymous said...

ne thanks ho

judes said...

Thanks lot . .Good job..

Rishi said...

thanks ....
i really helps

apourvaaaa said...

Good One....!

Anonymous said...

please suggest me a good android ebook.I am a begineer.Please help me.
stefyabraham24@gmail.com

Anonymous said...

Thank you.
This is the second program for me after hello world http://developer.android.com/resources/tutorials/hello-world.html
Would be good to see a sample on
Camera, GSM, Magnet, Temperature, Ext storage, Sound, Accelerometer

Anonymous said...

it's grt help..thnks.

deepi said...

hi.. i am an beginner in android and this code is useful. but i am not getting the final output after addition. can any one tell me what would have happened?

Harydaz said...

Thanks for the code....... It is very useful for beginners...

shravan said...

thumba sahaya aaitu...dhanyavadagalu :)

Anonymous said...

oho

Anonymous said...

Nice One

Anonymous said...

As per the requirement i need to show when the user enters two numbers i need to get the result in the other edittext how it is possible.with out pressing any button.

Australia Study said...

Thanks,
I've just started to study Android and needing some samples before we can actually develop a real app for our site.

This is a very good starting point for us.

Unknown said...
This comment has been removed by the author.
Anonymous said...

good article.....thanks

Anonymous said...

thank you

Anonymous said...

simple and useful

Unknown said...

Useless I spent hours changing declarations to fit my code and it force closed when I hit the button finally I Decide to copy paste the code and bam it force closes at the push of the button

anusha said...

hi can give example of calculation average of numbers in android by using one editbox

Deepu said...

i have search a lot for such a an instruction
thanks a lot to manjunath
but i want to know one more thing i am a beginner
in this field but i have learned how to create a ui in xml but i don't where i should write the java source code .. i am using eclips if any one can help me. please tell it to me

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google