How to enter two number in one activity and perform Add and Subtract operation on another activity in an android studio?

Asked 20-Nov-2018
Updated 20-Nov-2018
Viewed 2932 times

1

How to enter two number in one activity and perform Add and Subtract operation on another activity in an android studio?


1 Answer


0

"Perform Add-Subtract Operation on another Activity in Android Studio"

If you want to perform the add-subtract operation in two different activity, then, first of all, you need to follow these steps- 

Step 1:  Open your android studio project and create activity file for input two number and also add one button for redirect input number into another activity. In my code, I’m create activity_main.xml for input two numbers. Let see, what code will be needed for creating your layout file.


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_gravity="center"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context="com.example.msclient009.addsubstractexample.MainActivity">
// using linear layout for add control in vertical format
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="wrap_content">
    <EditText
        android:id="@+id/first_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:hint="Enter First No......." />

    <EditText
        android:id="@+id/second_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:hint="Enter Second No......."
        />
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        tools:layout_editor_absoluteX="157dp"
        tools:layout_editor_absoluteY="289dp"
        tools:ignore="MissingConstraints" />
    </LinearLayout>
</RelativeLayout> 


Step 2:  Now we will create MainActivity.java file for fetching input numbers. And for the redirect to another activity, we use Intent class.

Step 3:  For sending parameter,  one activity to another activity we use putExtra(key, value) method.


MainActivity.java

package com.example.msclient009.addsubstractexample;

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    EditText firstNumber,secondNumber;
    Button ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstNumber=(EditText)findViewById(R.id.first_number);
        secondNumber=(EditText)findViewById(R.id.second_number);

        ok=(Button)findViewById(R.id.ok);
// apply click listeren
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), AddSubtractOperation.class);
                intent.putExtra("FirstNumber",firstNumber.getText().toString()); // Code for sending data
                intent.putExtra("SecondNumber",secondNumber.getText().toString());
                startActivity(intent); // Code for startActivity (AddSubtractOperation)
            }
        });
    }
}


Step 4: For performing the add-subtract operation you will need to create another layout file. In my code, we will create activity_add_subtract_operation.xml

Step 5: In this layout file we will add one textView for display result and two buttons(+ & -) for performing operations. Let see, what code will write-


activity_add_subtract_operation.xml

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

<android.support.design.widget.CoordinatorLayout 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="com.example.msclient009.addsubstractexample.AddSubtractOperation">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <ImageView
                android:id="@+id/back_arrow"
                android:layout_width="wrap_content"
                android:layout_marginTop="-40dp"
                android:paddingRight="15dp"
                android:paddingLeft="15dp"
                android:src="@drawable/back_arrow"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:text=" Click for Back"
                android:textColor="#fff"
                android:textSize="22dp"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>
<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"
    android:layout_gravity="center"
    android:gravity="center"
    tools:context="com.example.msclient009.addsubstractexample.AddSubtractOperation">
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Plus & Minus for display Result"
        android:textSize="16dp"
        tools:ignore="MissingConstraints"
        android:layout_marginBottom="120dp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    <Button
        android:id="@+id/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="24dp"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="24dp" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>


Step 6:  Create another java file for performing an operation and we will also use the back button for go to previous activity.

Step 7: For getting parameter to previous activity we use getIntent().getStringExtra("key") method.


AddSubtractOperation.java

package com.example.msclient009.addsubstractexample;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AddSubtractOperation extends AppCompatActivity {
    ImageView backArrow;
    Button btnPlus,btnMinus;
    TextView resultTextView;
    int firstNumber,secondNumber,result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_subtract_operation);

        backArrow=(ImageView)findViewById(R.id.back_arrow);
        btnPlus=(Button)findViewById(R.id.plus);
        btnMinus=(Button)findViewById(R.id.minus);
        resultTextView=(TextView)findViewById(R.id.result);
// Code for getting parameter from previous activity
        firstNumber = Integer.valueOf(getIntent().getStringExtra("FirstNumber"));
        secondNumber=Integer.valueOf(getIntent().getStringExtra("SecondNumber"));
        // Code for go back previous activity
        backArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                 startActivity(intent);
            }
        });
        btnPlus.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View view) {
                result=firstNumber+secondNumber;
                resultTextView.setText(String.valueOf(result));
            }
        });        
        btnMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result=firstNumber-secondNumber;
                resultTextView.setText(String.valueOf(result));
            }
        });
    }
}


Finally what will be the output of above codes :


How to enter two number in one activity and perform Add and Subtract operation on another activity in an android studio?How to enter two number in one activity and perform Add and Subtract operation on another activity in an android studio?




"Thanks!!! for Reading"