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
<?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:
Thanks a lot it really helped me
Thanks for the simple yet effective tutorial!
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..
Sorry for the above comment..
may be i just gave the wrong input
might be it had some non numbers..
It works fine..
Thanks for this.
i am a new in android development, this
is very helpful for me.
Very useful..thank u
good one! Thank you.
thnx pal..
Good one friend...I was just looking for such a tutorial..
Sankar
am getting same scree for all the programs i run could u please help me...
am getting error at R.id wat may be the reason?
nice one.........really helped me
nice one ..keep posting :).tnx
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
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?
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
Hi Manjunath,
Thanks for sharing such a good and easy tutorial on Android for beginners.
Thanks.
Binod Suman
Bangalore
Dear All,
Can anybody give me the ebook link of Android for beginners.
Thanks in Advance....
Pawan Jha
pankajace12@gmail.com
New Delhi.
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
thnku...
can anybody give me ebook on android for beginner
at mohit.gupta108@gmail.com
Thanks in advance
any one help me to execution steps... because i have some execution error in my program
thanks for tutorials
thnxxx :))
this example is so nice and easy.................
thanks............
Thank you for sharing this wonderful example with us...
Thank you very much for giving such a nice error free example................
Very helpful
I don't know why but I keep getting an exception whenever I initialize the text boxes
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;
}
thnx for this tutorial.it helps me a lot....we r expecting some more such tutorials.please consider....
thanks sir... i will love to see more program
Can anybody give me the ebook link of Android for beginners....:)
swageetha,
j.swag96@gmail.com
Thanks a lot.
It was very useful..Thanks a lot
Hi all,
I want more examples like above.
its realy to good...
its realy to good
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 ..
ne thanks ho
Thanks lot . .Good job..
thanks ....
i really helps
Good One....!
please suggest me a good android ebook.I am a begineer.Please help me.
stefyabraham24@gmail.com
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
it's grt help..thnks.
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?
Thanks for the code....... It is very useful for beginners...
thumba sahaya aaitu...dhanyavadagalu :)
oho
Nice One
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.
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.
good article.....thanks
thank you
simple and useful
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
hi can give example of calculation average of numbers in android by using one editbox
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