Android program to perform four basic Arithmetic Operations(addition, subtraction, multiplication and division)

 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="ARITHMETIC OPERATIONS"
           
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:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
android:textColor="@color/color2" />

                <
EditText
                   
android:id="@+id/fno_et"
                   
android:layout_width="match_parent"
                   
android:layout_height="45dp"
                   
android:ems="10"
                   
android:inputType="number"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
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:textAppearance="@style/TextAppearance.AppCompat.Large"
                   
android:textColor="@color/color2" />

                <
EditText
                   
android:id="@+id/sno_et"
                   
android:layout_width="match_parent"
                   
android:layout_height="45dp"
                   
android:ems="10"
                   
android:inputType="number"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    
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="ADD"
               
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
android:layout_width="match_parent"
                   
android:layout_height="10dp" />
            </
TableRow>

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

            <
TableRow

               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView

                   
android:layout_width="match_parent"
                   
android:layout_height="10dp"
                   
/>
            </
TableRow>

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

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
android:layout_width="match_parent"
                    
android:layout_height="10dp" />
            </
TableRow>

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

            <
TableRow
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content">
                <
TextView
                   
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="wrap_content"
                   
android:layout_height="50dp"
                   
android:text="RESULT"
                   
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                   
android:textColor="@color/color2" />

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

MainAcitivity.java

package com.example.arithmeticoperations;

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

public class MainActivity extends Activity {

    @Override
   
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button btnsum = (Button) findViewById(R.id.buttonsum);
        final Button btnsub = (Button) findViewById(R.id.buttonsub);
        final Button btndiv = (Button) findViewById(R.id.buttondiv);
        final Button btnmul = (Button) findViewById(R.id.buttonmul);
        final EditText etv = (EditText) findViewById(R.id.fno_et);
        final EditText etv2 = (EditText) findViewById(R.id.sno_et);
        final TextView result = (TextView) findViewById(R.id.result_value);

        btnsum.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnsum.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double sum = x + y;
                result.setText(Double.toString(sum));
            }
        });

        btnsub.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnsub.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double sub = x - y;
                result.setText(Double.toString(sub));
            }
        });

        btndiv.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btndiv.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double div = x / y;
                result.setText(Double.toString(div));
            }
        });

        btnmul.setOnClickListener(new OnClickListener() {

            @Override
           
public void onClick(View v) {
                 btnmul.setBackgroundColor(Color.GRAY);
                double x = new Double(etv.getText().toString());
                double y = new Double(etv2.getText().toString());
                double mul = x * y;
                result.setText(Double.toString(mul));
            }
        });
    }

}

 

 

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>

 

String.xml

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


  

3 comments:

  1. You had me engaged by reading and you know what? I want to read more about that! this too is a great post. how to buy data on airtel 200 for 1gb

    ReplyDelete
  2. After reading this post, I thought it wise to also share it with some friends. You have wonderful ideas here. apon software

    ReplyDelete
  3. This is one of the vital info I have read. Am glad I came across it. Npower Business Login

    ReplyDelete