Wednesday, 12 September 2012

Addition of Two number in android


Step1:
           Create a new project. 

          Go to File => Project => Android Project =>Next=>Target SDK =>Next => Give class name & package=> Finish.


Step2:
        Here I am taking Java file name as “Main.java”. Now you open main.xml file. As per the program we will take two integer number and after clicking add button,the result will show on the text view in same page.


Step3:
   Go to your main.xml file. Take two edit text. See just drag & drop into xml file. Add a button also. Add a TextView in Last.

See Here main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Addition of 2 number"
        android:gravity="center" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD" />
<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>



Step4:
   Now go to your Main.java File. We need to write the code for adding two numbers.Let’s see how to do it.


Observe setContentView(R.layout.main);. Here it is calling main.xml file from java file. So we need to call Button , EditText, TextView in Java file that we designed in main.xml


So we need to create instance for Button , EditText, TextView in Java.
Just follow this piece of code:

final Button btn = (Button)findViewById(R.id.button1);
final EditText et1 = (EditText)findViewById(R.id.editText1);
final EditText et2 = (EditText)findViewById(R.id.editText2);
final TextView result = (TextView)findViewById(R.id.textView1)
When ever you are calling Button or EditText or TextView in java file from xml file,we can call it by using id.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD" />

Here id is button1. How it’s using in java file. Write Button  <somename> = (Button)findViewById(R.id.button1).
By using findViewById it will search how many id is there in xml file.You need to choose that ID(for Button id is button1).This is the same case for EditText, TextView.

Now what we want  to do?? We want to add two number when we click the button. So when we clicking, some listener is needed for click. So for clicking on button listener is setOnClickListener. How we use it??
btn.setOnClickListener(new OnClickListener()
        {
                 
                  @Override
                  public void onClick(View v)
                  {

                  }
            });

After creating button instance, we have to use setOnClickListener. Just type it & put new & then <ctrl+space>,then select OnClickListener(). It will show some error. Just click that point,it will ask for import.Just click on that & put ; in the last.
Now see this piece of code:
int x = new Integer(et1.getText().toString());
int y = new Integer(et2.getText().toString());
int sum = x + y;
result.setText("The Sum of "+x+" and "+y+" is "+sum);

Here we are taking two integer number x & y. In that x & y we are callling that EditText object and after getting the value(getText()) converting into the String.We are adding x & Y and storing in sum. After that showing the result.


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

public class Main extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn = (Button)findViewById(R.id.button1);
        final EditText et1 = (EditText)findViewById(R.id.editText1);
        final EditText et2 = (EditText)findViewById(R.id.editText2);
        final TextView result = (TextView)findViewById(R.id.textView1);
        btn.setOnClickListener(new OnClickListener()
        {
                 
                  @Override
                  public void onClick(View v)
                  {
                        int x = new Integer(et1.getText().toString());
                        int y = new Integer(et2.getText().toString());
                        int sum = x + y;
                        result.setText("The Sum of "+x+" and "+y+" is "+sum);
                  }
            });
    }
}


Step5: <<OUTPUT>>

21 comments:

  1. Nice job... Cont. with more post..:)

    ReplyDelete
    Replies
    1. Follow post everyday. I will post everyday new concept and Practice it. If you got any confusion, must comment, I will give some solution.

      Delete
  2. Thanks :) god bless you :)

    ReplyDelete
  3. Nice explanations thank you...... and give more examples.....

    ReplyDelete
  4. Surely Arul, I will try to give my best. Comment or contact click2tara@gmail.com for any queries regarding android development.

    ReplyDelete
  5. really nice one

    ReplyDelete
  6. I am a beginner in android development and I am Trying to run AVD but my AVD is not running please can any one help me out..

    ReplyDelete
    Replies
    1. change your vt-x settings in BIOS. then it would work fine..

      Delete
  7. How to increase AVT running speed?

    ReplyDelete
  8. How to increase AVT running speed?

    ReplyDelete
  9. I have an error on this code Error return statement missing , so how can i resolve it

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I read your article its very interesting information about Android Application service

    .
    Thanks

    ReplyDelete
  12. Seksipoker adalah Poker Online CC Terbaik

    IDN Poker Terpercaya hanya di Seksi poker

    agen poker 303
    poker online cc
    agen poker terbaik
    agen poker online terpercaya 2019
    agen poker online terpeercaya 2020
    agen poker online deposit pulsa
    agen poker online terbaik
    promosi
    event

    Promosi menarik dari seksipoker
    Bonus new member
    Bonus next deposit
    Turnover Harian & Mingguan
    Bonus referral
    Grand Prize Setiap Bulannya

    Contact us :
    Link : https://seksiidnpoker.com
    Whatsapp : +62 821-1786-1080

    ReplyDelete