Android program to add two numbers.

 

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   
android:background="@color/color1"
   
android:orientation="vertical"
   
tools:context=".MainActivity">

    <
TextView
       
android:layout_width="fill_parent"
       
android:layout_height="100dp"
       
android:layout_marginTop="30dp"
       
android:gravity="center_horizontal"
       
android:text="ADD TWO NUMBERS"
       
android:textColor="@color/color2"
       
android:textAppearance="@style/TextAppearance.AppCompat.Display1"></TextView>
    <
TableLayout
       
android:id="@+id/tableLayout1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_margin="10dp"
       
android:stretchColumns="1">
        <
TableRow
           
android:id="@+id/tableRow1"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content">

            <
TextView
               
android:id="@+id/fno_tv"
               
android:layout_width="match_parent"
               
android:layout_height="40dp"
               
android:text="ENTER FIRST NUMBER   "
               
android:textColor="@color/color2"
               
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

            <
EditText
               
android:id="@+id/fno_et"
               
android:layout_width="match_parent"
               
android:layout_height="40dp"
               
android:ems="10"
               
android:inputType="number"
               
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
               
android:textColor="@color/color3" />
            </
TableRow>
        <
TableRow
           
android:id="@+id/tableRow2"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content">

            <
TextView
               
android:id="@+id/sno_tv"
               
android:layout_width="match_parent"
               
android:layout_height="75dp"
               
android:text="ENTER SECOND NUMBER   "
               
android:textColor="@color/color2"
               
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

            <
EditText
               
android:id="@+id/sno_et"
               
android:layout_width="match_parent"
               
android:layout_height="40dp"
                
android:ems="10"
               
android:inputType="number"
               
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
               
android:textColor="@color/color3" />
        </
TableRow>

        <
Button
           
android:id="@+id/buttonsum"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:background="@color/colorAccent"
           
android:gravity="center"
           
android:text="CALCULATE SUM"
            
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <
TableRow
           
android:id="@+id/tableRow00"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content">

        <
TextView
            
android:id="@+id/a000"
           
android:layout_width="match_parent"
           
android:layout_height="75dp"
           
/>
        </
TableRow>
        <
TableRow
           
android:id="@+id/tableRow3"
           
android:layout_width="match_parent"
            
android:layout_height="40dp">

        <
TextView
           
android:id="@+id/result_tv"
           
android:layout_width="match_parent"
           
android:layout_height="50dp"
           
android:text="RESULT"
           
android:textColor="@color/color2"
           
android:textAppearance="?android:attr/textAppearanceLarge" />

            <
TextView
               
android:id="@+id/result_value"
               
android:layout_width="match_parent"
               
android:layout_height="50dp"
                
android:textAppearance="@style/TextAppearance.AppCompat.Large"
               
android:textColor="@color/color3" />
        </
TableRow>
    </
TableLayout>
</
LinearLayout>

 

 

MainActivity.java

package com.example.addtwonumbers;

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

public class MainActivity extends Activity {
    EditText
number1,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.fno_et);
       
number2=(EditText) findViewById(R.id.sno_et);
       
Add_button=(Button) findViewById(R.id.buttonsum);
       
result = (TextView) findViewById(R.id.result_value);

       
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;

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

 

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <
color name="colorPrimary">#6200EE</color>
    <
color name="colorPrimaryDark">#3700B3</color>
    <
color name="colorAccent">#03DAC5</color>
    <
color name="color1">#FFE4E1</color>
    <
color name="color2">#191970</color>
    <
color name="color3">#FF0000</color>
</
resources>

 

Strings.xml

<resources>
    <
string name="app_name">AddTwoNumbers</string>
</
resources>

Output

output- addition of two numbers


No comments:

Post a Comment