Friday, 18 April 2014

Addition of two numbers using Android application

1. go to file and choose new and click on Android Application:

2. Now you will get Android Application wizard:

3: fill the following: (you can give any name)
Application name:    Add
Project Name:         Add
Package Name:       com.example.add

See the Example below sample screen:

4: click on Next button
5. click on Next button
6. click on Next button
7. click on Next button
8. click on finish button

9. it will look like below:

10. Now go to +res folder of left hand side

11. click on layout of res folder


12. click on "activty_main.xml": then we will see like the screen

13. then click on Graphical layout(by default it is )
14. click on TextFields:


15: Drag and drop in the layout

16. once you drag that textfield into the layout: we will see the following screen

17: double click on that text filed in the layout: it goes to activity_main.xml file

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>




18. type whatever you want to display the text, now i want to ask the user to enter any number1 so type after <EditText  android:hint"write here "

<EditText android:hint="Enter any number1"

Example:
<EditText android:hint="Enter any number1"
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>


</RelativeLayout>

Screen shot is:

19. Click on Graphical Layout to Go back to write another textfield to take second number:

20. once we click the Graphical Layout then we will see the following screen shot:

21. Now we need another text field (to take second number) : do the same process of steps (15,16)

22. Now double click the second textfield to activity_main.xml file:

23. Now control goes to "activity_main.xml" file


24. Now write android:hint"any text" after <EditText ..
i.e:
<EditText android:hint="Enter number2"

Screen shot is:

25. Now go to Graphical Layout to create "TextView" to display the result of addition of two numbers:
26. click on FormWidget:

27. once we click on form Widget, we will get the TextView buttons

28 select any text view (either small , midium, large) here Im taking Large TextView, once you select the Textview, drag that textView (here large) and paste where you want to display (now i want to display after 2nd text field.
29.  Now  our job is , we have to access the text field1 and textfield2 and display the output in Textview and create the button (submit)
for to create Button, select the Button icon and drag into the layout, wherever you want, then double click on Butoon ,

Now once you double click the icon, control goes to "activity_main.xml" file:(we will see the code like below)
we will see the follwing code:
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="30dp"
        android:text="Button" />

now the change the Button name, and rename it whatever you want,(i want "submit")
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="30dp"
        android:text="submit" />
and save


30. To do this task first we need to create the references, so click on "MainActivity.java" of res folder

31. created the references:
TextView tv;
    Button result;
    EditText avalue,bvalue;


31. Now we need to access the values from the textfield, write the code after "setContentview(R.layout.activity_main);
result=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.textView2);
        avalue=(EditText)findViewById(R.id.editText1);
        bvalue=(EditText)findViewById(R.id.editText2);
        result.setOnClickListener(new OnClickListener() 
        {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int a,b,c;
a=Integer.parseInt(avalue.getText().toString());
b=Integer.parseInt(bvalue.getText().toString());
c=a+b;
tv.setText("the result is"+c);
}
});
        
        
    }
screen shot is:

32.


No comments:

Post a Comment