How To Create Calculator App For Android
Introduction
- Android Studio.
- Little bit XML and JAVA knowledge.
Steps to be followed are given below
Open an Android Studio. Start the new project.

Put the Application name and company domain. If you wish to use C++ for coding the project, mark the Include C++ support, followed by clicking Next.

Select the Android minimum SDK. Afterward, you chose the minimum SDK and it will show the approximate percentage of the people, who use SDK, followed by clicking Next.

Choose the basic activity, followed by clicking Next.

Put the activity name and layout name. Android Studio basically takes a Java class name, which is provided by you in the activity name

- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/linearLayout1"
- android:layout_marginLeft="10pt"
- android:layout_marginRight="10pt"
- android:layout_marginTop="3pt">
- <EditText
- android:layout_weight="1"
- android:layout_height="wrap_content"
- android:layout_marginRight="5pt"
- android:id="@+id/etNum1"
- android:layout_width="match_parent"
- android:inputType="numberDecimal">
- </EditText>
- <EditText
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_marginLeft="5pt"
- android:id="@+id/etNum2"
- android:layout_width="match_parent"
- android:inputType="numberDecimal">
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/linearLayout2"
- android:layout_marginTop="3pt"
- android:layout_marginLeft="5pt"
- android:layout_marginRight="5pt">
- <Button
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:text="+"
- android:textSize="15pt"
- android:id="@+id/btnAdd">
- </Button>
- <Button
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:text="-"
- android:textSize="15pt"
- android:id="@+id/btnSub">
- </Button>
- <Button
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:text="*"
- android:textSize="15pt"
- android:id="@+id/btnMult">
- </Button>
- <Button
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:text="/"
- android:textSize="15pt"
- android:id="@+id/btnDiv">
- </Button>
- </LinearLayout>
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_marginLeft="5pt"
- android:layout_marginRight="5pt"
- android:textSize="12pt"
- android:layout_marginTop="3pt"
- android:id="@+id/tvResult"
- android:gravity="center_horizontal">
- </TextView>
- </LinearLayout>

Step 8
- package ganeshannt.calc;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class CalcActivity extends Activity implements
- OnClickListener
- {
- EditText input1;
- EditText input2;
- Button addition;
- Button subtraction;
- Button multiplication;
- Button division;
- TextView tvResult;
- String oper = "";
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_calc);
- input1 = (EditText) findViewById(R.id.etNum1);
- input2 = (EditText) findViewById(R.id.etNum2);
- addition = (Button) findViewById(R.id.btnAdd);
- subtraction = (Button) findViewById(R.id.btnSub);
- multiplication = (Button) findViewById(R.id.btnMult);
- division = (Button) findViewById(R.id.btnDiv);
- tvResult = (TextView) findViewById(R.id.tvResult);
- // set a listener
- addition.setOnClickListener(this);
- subtraction.setOnClickListener(this);
- multiplication.setOnClickListener(this);
- division.setOnClickListener(this);
- }
- @Override
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- float num1 = 0;
- float num2 = 0;
- float result = 0;
- // check if the fields are empty
- if (TextUtils.isEmpty(input1.getText().toString())
- || TextUtils.isEmpty(input2.getText().toString()))
- {
- return;
- }
- // read EditText and fill variables with numbers
- num1 = Float.parseFloat(input1.getText().toString());
- num2 = Float.parseFloat(input2.getText().toString());
- // defines the button that has been clicked and performs the corresponding operation
- // write operation into oper, we will use it later for output
- switch (v.getId())
- {
- case R.id.btnAdd:
- oper = "+";
- result = num1 + num2;
- break;
- case R.id.btnSub:
- oper = "-";
- result = num1 - num2;
- break;
- case R.id.btnMult:
- oper = "*";
- result = num1 * num2;
- break;
- case R.id.btnDiv:
- oper = "/";
- result = num1 / num2;
- break;
- default:
- break;
- }
- // form the output line
- tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
- }
- }
This is our user interface of the Application. Click Make project option.

Run the Application, followed by choosing the virtual machine. Click OK.

Here, we successfully created a Calculator app for an Android, using an Android Studio Application, which was created and executed.

Addition operation

Subtraction operation

Divide operation

Multiplication operation

Post a Comment