The Basic Calculator

A calculator is the basic need of every individual for calculations ranging from paying grocery bills to high-end official computations. The powers of smartphones and tablets have made the substantial calculator pretty much outdated.

Inspired by the Google Calculator App, I made my mind to design and develop a basic calculator of my own that would help to compute simple arithmetic calculations such as addition, subtraction, multiplication and division.

So, we have a combination of linear layouts to arrange all the elements of the calculator such as the numbers and symbolsrepresenting different operators.

All the computations that we carry out are visible in the textbox just at the top of our layout. So, what actually this calculator does is that accepts any integer or float value, performs the required operation and displays the result below it.

It is evident that only a single operation can be performed at a time, with 2 input values, but the advantage is that again a series of operations can be performed with the obtained result.

Let’s start creating the project. The step-wise procedure is as follows –

1.Create a new project in Android Studio:Specify an appropriate title for the project, e.g. MyCalculator and select empty activity.

2.Design xml file of the Activity:Remember that the design of the layout should be an attractive one and compatible on all screen sizes.

The pertinent screen-shot of the UI of project is given as below, but you can also develop a different and a better design –The code-snippet for the activity_main.xml file is as follows –

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.igeniusdev.calculatorclone.MainActivity"
android:background="#FFC125">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="9dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@drawable/editsearch">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginLeft="15dp"
android:layout_marginTop="9dp"
android:textSize="20dp"
android:typeface="serif"
android:textColor="#691F01" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
android:textSize="20dp"
android:typeface="serif"
android:textColor="#691F01" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
android:textSize="20dp"
android:typeface="serif"
android:textColor="#691F01" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:typeface="serif"
android:textStyle="bold"
android:textColor="#691F01" />

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
android:id="@+id/textView5"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
android:id="@+id/textView6"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
android:id="@+id/textView7"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="+"
android:id="@+id/textView8"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="4"
android:id="@+id/textView9"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="5"
android:id="@+id/textView10"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="6"
android:id="@+id/textView11"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="-"
android:id="@+id/textView12"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="7"
android:id="@+id/textView13"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="8"
android:id="@+id/textView14"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="9"
android:id="@+id/textView15"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="*"
android:id="@+id/textView16"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:id="@+id/textView17"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="."
android:id="@+id/textView18"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="="
android:id="@+id/textView19"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="/"
android:id="@+id/textView20"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="35dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="CC"
android:id="@+id/textView21"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="30dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="DEL"
android:id="@+id/textView22"
android:layout_weight="1"
android:layout_margin="7dp"
android:textSize="30dp"
android:background="#8B6508"
android:textColor="#ffffff"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
  1. Hide the toolbar for a better view:

The toolbar is unnecessary component in this project, so removing it is a better option. To do so, go to the styles.xml file and inside <style> tag, add the following properties –

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

2.Finally, code in the MainActivity.java class:

This is the most important part of the project as it contains all the methods for each operation to be carried out. Also, the code facilitates to catch any exception and restrict the force stop of our app in any possible situation.

public class MainActivityextends AppCompatActivity
{
TextViewb1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bdot, badd, bsub, bmul, bdiv, beq, bclear, bdel;
TextViewe1, e2, e3, eresult;

float val1 = 0;
float val2 = 0;
float result = 0;
booleanadd, sub, div, mul;

String oper= "";

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

b1 = (TextView) findViewById(R.id.textView5);
b2 = (TextView) findViewById(R.id.textView6);
b3 = (TextView) findViewById(R.id.textView7);
b4 = (TextView) findViewById(R.id.textView9);
b5 = (TextView) findViewById(R.id.textView10);
b6 = (TextView) findViewById(R.id.textView11);
b7 = (TextView) findViewById(R.id.textView13);
b8 = (TextView) findViewById(R.id.textView14);
b9 = (TextView) findViewById(R.id.textView15);
b0 = (TextView) findViewById(R.id.textView17);
bdot= (TextView) findViewById(R.id.textView18);
badd= (TextView) findViewById(R.id.textView8);
bsub= (TextView) findViewById(R.id.textView12);
bmul= (TextView) findViewById(R.id.textView16);
bdiv= (TextView) findViewById(R.id.textView20);
beq= (TextView) findViewById(R.id.textView19);
bclear= (TextView) findViewById(R.id.textView21);
bdel= (TextView) findViewById(R.id.textView22);

e1 = (TextView) findViewById(R.id.textView);
e2 = (TextView) findViewById(R.id.textView3);
e3 = (TextView) findViewById(R.id.textView2);

eresult= (TextView) findViewById(R.id.textView4);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("1");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"1");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >0 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"1");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 
&&eresult.getText().toString().trim().length() >0)
                {
e2.setText("1");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"1");
                }

            }
        });
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
            {

if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("2");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"2");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"2");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("2");
                }
else if(e1.getText().toString().trim().length() >0 
&&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"2");
                }
            }
        });
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
            {


if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("3");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"3");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"3");
                }

else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("3");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"3");
                }

            }
        });
b4.setOnClickListener(new View.OnClickListener()
        {
@Override
public void onClick(View v)
            {


if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("4");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"4");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"4");
                }

else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("4");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"4");
                }

            }
        });
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("5");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"5");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"5");
                }

else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("5");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"5");
                }
            }
        });
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("6");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"6");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"6");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("6");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"6");
                }

            }
        });
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("7");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"7");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"7");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("7");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"7");
                }
            }
        });
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("8");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"8");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"8");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("8");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"8");
                }

            }
        });
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("9");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"9");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"9");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("9");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"9");
                }
            }
        });
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) 
{

if (e1.getText().toString().trim().length() == 0)
                {
e1.setText("0");
                }
else if(e1.getText().toString().trim().length() >= 1 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() +"0");
                }
else if(e3.getText().toString().trim().length() == 1 &&e1.getText().toString().trim().length() >= 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"0");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e2.setText("0");
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() +"0");
                }
            }
        });

bdot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (e1.getText().toString().trim().length() == 0)
                {
Toast.makeText(getApplicationContext(),"Please append an integer first",Toast.LENGTH_LONG).show();
                }

if (e1.getText().toString().trim().length() >0 && !(e1.getText().toString().trim().contains(".")) &&eresult.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 0)
                {
e1.setText(e1.getText() + ".");
                }

if(e2.getText().toString().trim().length() >0 && !(e2.getText().toString().trim().contains(".")) &&eresult.getText().toString().trim().length() == 0)
                {
e2.setText(e2.getText() + ".");
                }

            }
        });

badd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
            {
if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0)
                {
e3.setText("+");
add = true;
                }

else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() >0 &&eresult.getText().toString().trim().length() >0)
                {
e1.setText(result+"");
e2.setText(null);
e3.setText("+");
eresult.setText(null);
add = true;
                }
            }
        });
bsub.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
            {
if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0)
                {
e3.setText("-");
sub = true;
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() >0 &&eresult.getText().toString().trim().length() >0)
                {
e1.setText(result+"");
e2.setText(null);
e3.setText("-");
eresult.setText(null);
sub = true;
                }
            }
        });
bdiv.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
            {
if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0)
                {
e3.setText("/");
div = true;
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() >0 &&eresult.getText().toString().trim().length() >0)
                {
e1.setText(result+"");
e2.setText(null);
e3.setText("/");
eresult.setText(null);
div = true;
                }
            }
        });
bmul.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
            {
if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0)
                {
e3.setText("*");
mul= true;
                }
else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() >0 
&&eresult.getText().toString().trim().length() >0)
                {
e1.setText(result+"");
e2.setText(null);
e3.setText("*");
eresult.setText(null);
mul= true;
                }
            }
        });

beq.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
            {

if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1)
                {

val1 = Float.parseFloat(e1.getText().toString());
val2 = Float.parseFloat(e2.getText().toString());

if (add == true)
                    {
oper= "+";
result = val1 + val2;
add = false;

}
if (sub == true)
                    {
oper= "-";
result = val1 - val2;
sub = false;
                    }

if (mul== true)
                    {

oper= "*";
result = val1 * val2;
mul= false;
                    }

if (div == true)
                    {

oper= "/";
result = val1 / val2;
div = false;
           }

eresult.setText(result+"");
                }
else
{

}
            }
        });

bclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
            {
e1.setText(null);
e2.setText(null);
e3.setText(null);
eresult.setText(null);
            }
        });

bdel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
            {

if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() == 0 &&e3.getText().toString().trim().length() == 0)
                {
String str = e1.getText().toString();
str = str.substring(0, str.length() - 1);
e1.setText(str);
                }

else if(e2.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() == 0)
                {
                    String str = e2.getText().toString();
str = str.substring(0, str.length() - 1);
e2.setText(str);
                }

else if(e1.getText().toString().trim().length() >0 &&e2.getText().toString().trim().length() >0 &&e3.getText().toString().trim().length() == 1 &&eresult.getText().toString().trim().length() >0)
                {
e1.setText(null);
e2.setText(null);
e3.setText(null);
eresult.setText(null);
                }
            }
        });
    }
}

Now, debug and run the project. Enter as many as values you like and witness the swift and smooth calculation it performs.

Like and share my blog if it gained you something. Good day…! 

Let's Think together, Say Something !