Thursday, July 23, 2020

Write a Program Addition of two numbers in Android Application.

Addition of two numbers in Android Application.

MainActivity.java


package com.example.sunny.addition;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class MainActivity extends AppCompatActivity {

 

    EditText number1;

    EditText number2;

    Button Add_button;

    TextView result;

    int ans=0;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        number1=(EditText) findViewById(R.id.fst);

        number2=(EditText) findViewById(R.id.sec);

        Add_button=(Button) findViewById(R.id.add_button);

        result = (TextView) findViewById(R.id.textView_answer);

 

        Add_button.setOnClickListener(new View.OnClickListener() {

 

            public void onClick(View v) {

 

 

                double num1 = Double.parseDouble(number1.getText().toString());

                double num2 = Double.parseDouble(number2.getText().toString());

                double sum = num1 + num2;

               // double sub = num1 - num2;

                result.setText(Double.toString(sum));

            }

 

        });

    }

}



Activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

 

    <TextView

        android:id="@+id/textView_answer"

        android:layout_width="100dp"

        android:layout_height="25dp"

        android:layout_marginLeft="130dp"

        android:layout_marginTop="300dp"

        android:text="0"

        android:textSize="20dp"

        android:textStyle="bold" />

 

    <!--take the input first number-->

    <EditText

        android:id="@+id/fst"

        android:layout_width="150dp"

        android:layout_height="40dp"

        android:layout_marginLeft="200dp"

        android:layout_marginTop="40dp"

        android:hint="Enter Number"

        android:inputType="number" />

 

    <TextView

        android:id="@+id/textView_first_no"

        android:layout_width="150dp"

        android:layout_height="25dp"

        android:layout_marginLeft="10dp"

        android:layout_marginTop="50dp"

        android:text="First number"

        android:textSize="20dp" />

 

 

    <TextView

        android:id="@+id/textView_second_no"

        android:layout_width="150dp"

        android:layout_height="25dp"

        android:layout_marginLeft="10dp"

        android:layout_marginTop="100dp"

        android:text="Second number"

        android:textSize="20dp" />

 

 

 

    <EditText

        android:id="@+id/sec"

        android:layout_width="150dp"

        android:layout_height="40dp"

        android:layout_marginLeft="200dp"

        android:layout_marginTop="90dp"

        android:inputType="number"

        android:hint="Enter Number"

        tools:ignore="MissingConstraints" />

 

 

 

    <Button

        android:id="@+id/add_button"

        android:layout_width="100dp"

        android:layout_height="50dp"

        android:layout_marginLeft="110dp"

        android:layout_marginTop="200dp"

        android:text="Addition">

 

    </Button>

    

 

 

</RelativeLayout>

 

 

 

OUTPUT


Addition of two number in android
Android App Sum of Two Number 

No comments:

Post a Comment

Please do not any spam link in Comment Box