Monday, 12 June 2017

Adding two numbers in Android

adding-numbers-in-android




Download Source Code

In this post you can learn how to add two numbers in android.

Before that i recommend to read the below button example post to get an idea about working with buttons.

How to work with buttons in android

How to display names while button is clicked in android.


As in my last i have said that before starting the design we need to draw what we are going to design.

ui-design


Create a New Project and drag and drop two EditText for getting inputs from the user and a Button to calculate addition operation and a TextView to display the Result.


primary-ui-design


Read this post to remove the warnings How to display names while button is clicked in android. 

warning-cleared-ui

Next we need to start the coding part.

The registering the edittext , Textview, Button is same as the previous posts.

button-code

After that we need to declare variables to get the numbers and store the result value.

we define it in double.

double number1,number2,sum;

Then type the following code

addition-of-two-numbers-android-code
 

the last lines in the code is just converting the string in to double for calculation.

Then after typing the code Run the program.

Output:


output

output
output
output



Read How to run android program in your mobile

 Code of onClick() method:

public void onClick(View arg0)
    {
       
        number1=Double.parseDouble( num1.getText().toString());
        number2=Double.parseDouble(num2.getText().toString());
        sum=number1+number2;
       
        result.setText(Double.toString(sum));
       
    }

No comments :

Post a Comment