How to build a simple Calculator app using Android Studio?
Pre-requisites:
- Android App Development Fundamentals for Beginners
- Guide to Install and Set up Android Studio
- Android | Starting with first app/android project
- Android | Running your first Android app
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.
- Step1: Opening a new project
- Open a new project just click of File option at topmost corner in left.
- Then click on new and open a new project with whatever name you want.
- Now we gonna work on Empty Activity with language as Java. Leave all other options as untouched.
- You can change the name of project as per your choice.

- By default, there will be two files activity_main.xml and MainActivity.java.
- Step2: Designing the UI
- Below is the code for the xml file.
actibity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#8BC34A"android:backgroundTint="@android:color/darker_gray"tools:context=".MainActivity"><!-- Text View to display "gfg_myFirstApp "--><TextViewandroid:id="@+id/textView"android:layout_width="133dp"android:layout_height="28dp"android:layout_marginStart="139dp"android:layout_marginLeft="139dp"android:layout_marginTop="16dp"android:layout_marginEnd="139dp"android:layout_marginRight="139dp"android:layout_marginBottom="559dp"<!-- providing the green colour to the background -->android:background="#0F9D58"android:text="gfg_myFirstApp"android:textAppearance="@style/TextAppearance.AppCompat.Medium"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><!-- Text View to display our basic heading of "calculator"--><TextViewandroid:layout_width="194dp"android:layout_height="43dp"android:layout_marginStart="114dp"android:layout_marginLeft="114dp"android:layout_marginTop="58dp"android:layout_marginEnd="103dp"android:layout_marginRight="103dp"android:layout_marginBottom="502dp"android:scrollbarSize="30dp"android:text=" Calculator"android:textAppearance="@style/TextAppearance.AppCompat.Body1"android:textSize="30dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- Edit Text View to input the values --><EditTextandroid:id="@+id/num1"android:layout_width="364dp"android:layout_height="28dp"android:layout_marginStart="72dp"android:layout_marginTop="70dp"android:layout_marginEnd="71dp"android:layout_marginBottom="416dp"android:background="@android:color/white"android:ems="10"android:hint="Number1(0)"android:inputType="number"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- Edit Text View to input 2nd value--><EditTextandroid:id="@+id/num2"android:layout_width="363dp"android:layout_height="30dp"android:layout_marginStart="72dp"android:layout_marginTop="112dp"android:layout_marginEnd="71dp"android:layout_marginBottom="374dp"android:background="@android:color/white"android:ems="10"android:hint="number2(0)"android:inputType="number"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- Text View to display result --><TextViewandroid:id="@+id/result"android:layout_width="356dp"android:layout_height="71dp"android:layout_marginStart="41dp"android:layout_marginTop="151dp"android:layout_marginEnd="48dp"android:layout_marginBottom="287dp"android:background="@android:color/white"android:text="result"android:textColorLink="#673AB7"android:textSize="25sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform 'sum' operation --><Buttonandroid:id="@+id/sum"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginTop="292dp"android:layout_marginEnd="307dp"android:layout_marginBottom="263dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doSum"android:text="+"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform subtraction operation. --><Buttonandroid:id="@+id/sub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="210dp"android:layout_marginTop="292dp"android:layout_marginEnd="113dp"android:layout_marginBottom="263dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doSub"android:text="-"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform division. --><Buttonandroid:id="@+id/div"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="307dp"android:layout_marginTop="292dp"android:layout_marginEnd="16dp"android:layout_marginBottom="263dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doDiv"android:text="/"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform multiplication. --><Buttonandroid:id="@+id/mul"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginTop="356dp"android:layout_marginEnd="307dp"android:layout_marginBottom="199dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doMul"android:text="x"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform a modulus function. --><Buttonandroid:id="@+id/button"android:layout_width="92dp"android:layout_height="48dp"android:layout_marginStart="113dp"android:layout_marginTop="356dp"android:layout_marginEnd="206dp"android:layout_marginBottom="199dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doMod"android:text="%(mod)"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- A button to perform a power function. --><Buttonandroid:id="@+id/pow"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="113dp"android:layout_marginTop="292dp"android:layout_marginEnd="210dp"android:layout_marginBottom="263dp"android:backgroundTint="@android:color/holo_red_light"android:onClick="doPow"android:text="n1^n2"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout> - After using this code in .Xml file the UI will be like:

- Below is the code for the xml file.
- Step3: Working with Java file
- Open the MainActivity.java file there within the class, make a method named doSum(View v).
- In this method, first of all, we have to link two edit box with variables so that we can use them for our input.
- So link those edit box with variables we have write
"EditText e1=(EditText )findViewById(R.id.num1);"
here num1 is id for textbox and we are just giving a variable name ‘e1’ to text box with id ‘num1’.
- Similarly, we have to use the same statement for the second textbox with the variable name ‘e2’.
- For third text box we have use
"TextView t1=(TextView) findViewById(R.id.result);"
here we have used TextView because we only have to display text avoiding it to be user changeable.
- Now we have to input number in form of string using getText() function.
- The input statement will be
"String s11=e1.getText().toString();"
- Here s11 stores the number entered in textbox 1.We have to do the same with another Textbox(e2). Now store the number in int form and apply addition. store the added value in another variable. To display stored in sum we have to use setText() as follows:
result.setText(final_sum.toString())
- final_sum stores the sum and it’s necesary to convert it to string(.toString()).
Java code for MainActivity.java is:
MainActivity.java
packagecom.example.gfg_my_first_app;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;publicclassMainActivityextendsAppCompatActivity {EditText e1, e2;TextView t1;intnum1, num2;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}// a public method to get the input numberspublicbooleangetNumbers(){// defining the edit text 1 to e1e1 = (EditText)findViewById(R.id.num1);// defining the edit text 2 to e2e2 = (EditText)findViewById(R.id.num2);// defining the text view to t1t1 = (TextView)findViewById(R.id.result);// taking input from text box 1s1 = e1.getText().toString();// taking input from text box 2s2 = e2.getText().toString();// condition to check if box is not emptyif((s1.equals(null) && s2.equals(null))|| (s1.equals("") && s2.equals(""))) {String result ="Please enter a value";t1.setText(result);returnfalse;}else{// converting string to int.num1 = Integer.parseInt(e1.getText().toString());// converting string to int.num2 = Integer.parseInt(e2.getText().toString());}returntrue;}// a public method to perform additionpublicvoiddoSum(View v){// get the input numbersif(getNumbers()) {intsum = num1 + num2;t1.setText(Integer.toString(sum));}}// a public method to perform power functionpublicvoiddoPow(View v){// get the input numbersif(getNumbers()) {doublesum = Math.pow(num1, num2);t1.setText(Double.toString(sum));}}// a public method to perform subtractionpublicvoiddoSub(View v){// get the input numbersif(getNumbers()) {intsum = num1 - num2;t1.setText(Integer.toString(sum));}}// a public method to perform multiplicationpublicvoiddoMul(View v){// get the input numbersif(getNumbers()) {intsum = num1 * num2;t1.setText(Integer.toString(sum));}}// a public method to perform DivisionpublicvoiddoDiv(View v){// get the input numbersif(getNumbers()) {// displaying the text in text view assigned as t1doublesum = num1 / (num2 *1.0);t1.setText(Double.toString(sum));}}// a public method to perform modulus functionpublicvoiddoMod(View v){// get the input numbersif(getNumbers()) {doublesum = num1 % num2;t1.setText(Double.toString(sum));}}}Output:
- Performing the addition operation.

- Performing the subtraction operation.

- Performing the multipltcation operation.

- Performing the division operation.

- Performing the power operation.

- Performing the modulus operation.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!


