Monday, 12 June 2017
Adding two 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.
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.
Read this post to remove the warnings How to display names while button is clicked in android.
Next we need to start the coding part.
The registering the edittext , Textview, Button is same as the previous posts.
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
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:
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));
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment