How to create Android Scientific Calculator Application

How to create Android Scientific Calculator Application

In this android tutorial, we are going to learn how to create Android Scientific Calculator. I have received request from our readers in relation to Android Scientific Calculator.

This tutorial will take time so you have to relax and follow the tutorial. If you have any question, don’t hesitate to contact.

If you are looking for android tutorial on how to create a Simple Calculator in Android, you can read one of my post on this topic.

We will be use android Button Views for the Scientific Calculator button, other button options that works with Shift/Inverse button will be represented with the TextView.

The display of the calculator will also be a TextView. We are also going to create two different layout folders(layout-normal and layout-large) in other to take care of different screen sizes in android.

In order to understand what we are out to achieve, we are going to have a look at the screen shot of the application we will create below.

Scientific Calculator

Before we start, it is important for you to understand the tools and environment I used in this application development tutorial. Feel free to use any tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 22

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidScientificCalculator

Package: com.inducesmile.androidscientificcalculator

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

If you are using Android Studio as your choice IDE, the new project will create a default

MainActivity.java file and its corresponding layout file. Open the two files because we are going to

make use of them.

Open the layout file, we are going to create our layout using Button and TextView controls. The layout code snippet is shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLay0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/calculator_background">
    <LinearLayout
        android:id="@+id/top_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/shift_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/float_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="@string/no_text"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/degree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="DEG"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/display_screen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/calculator_screen">
        <TextView
            android:id="@+id/display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="right"
            android:cursorVisible="true"
            android:textColorHint="@color/icons"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:singleLine="true"
            android:text="@string/zero_button"
            android:textColor="@color/primary_text"
            android:textSize="60sp" />
    </LinearLayout>
    <!--First Row-->
    <LinearLayout
        android:id="@+id/first_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/rcl"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/no_text"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/sto"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/no_text"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/m_minus"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/m_minus"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <!--Second Row Button-->
    <LinearLayout
        android:id="@+id/second_row"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/shift"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/divider"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:background="@color/calculator_foreground"
            android:text="@string/shift" />
        <Button
            android:id="@+id/rad"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/rad" />
        <Button
            android:id="@+id/abs"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/abs" />
        <Button
            android:id="@+id/mr"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/mr" />
        <Button
            android:id="@+id/ms"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/ms" />
        <Button
            android:id="@+id/m_plus"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/m_plus" />
    </LinearLayout>
    <!--Third Row Text-->
    <LinearLayout
        android:id="@+id/third_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/inverse_sin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:text="@string/inverse_sin"
            android:textColor="@color/divider"
            android:textSize="13dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/inverse_cos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:text="@string/inverse_cos"
            android:textColor="@color/divider"
            android:textSize="13dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/inverse_tan"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/inverse_tan"
            android:gravity="center"
            android:textSize="13dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/expo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/expo"
            android:gravity="center"
            android:textSize="13dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/ten_power_x"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/ten_power"
            android:gravity="center"
            android:textSize="13dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <!--Fourth Row Button-->
    <LinearLayout
        android:id="@+id/fourth_row"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/hys"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/rnd" />
        <Button
            android:id="@+id/sin_sign"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/sin_sign" />
        <Button
            android:id="@+id/cos_sign"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/cos_sign" />
        <Button
            android:id="@+id/tan_sign"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/tan_sign" />
        <Button
            android:id="@+id/natural_log"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/natural_log" />
        <Button
            android:id="@+id/log"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/log" />
    </LinearLayout>
    <!--Fifth Row Text-->
    <LinearLayout
        android:id="@+id/fifth_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/cube_root"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textSize="13dp"
            android:text="@string/cube_root"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/cube"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textSize="13dp"
            android:text="@string/cube"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/one_over_x"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:textSize="13dp"
            android:text="@string/one_over_x"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <!--Sixth Row Button-->
    <LinearLayout
        android:id="@+id/sixth_row"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/x_power_y"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/x_power_y" />
        <Button
            android:id="@+id/square_root"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/square_root" />
        <Button
            android:id="@+id/x_square"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/x_square" />
        <Button
            android:id="@+id/percent"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/percent" />
        <Button
            android:id="@+id/open_bracket"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/open_bracket" />
        <Button
            android:id="@+id/close_bracket"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="16sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/close_bracket" />
    </LinearLayout>
    <!--Seventh Row 5 Buttons-->
    <LinearLayout
        android:id="@+id/seventh_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="8dp">
        <Button
            android:id="@+id/seven_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/seven_button" />
        <Button
            android:id="@+id/eight_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/eight_button" />
        <Button
            android:id="@+id/nine_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/nine_button" />
        <Button
            android:id="@+id/single_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="1sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@drawable/deleteicon"
            android:text="@string/single_delete" />
        <Button
            android:id="@+id/clear"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/clear" />
    </LinearLayout>
    <!--Eigth Row 5 Text-->
    <LinearLayout
        android:id="@+id/eigth_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/factorial"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:textSize="13dp"
            android:text="@string/factorial"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/combination"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:textSize="13dp"
            android:gravity="center"
            android:text="@string/combination"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/permutation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/permutation"
            android:gravity="center"
            android:textSize="13dp"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <!--Nine Row 5 Buttons-->
    <LinearLayout
        android:id="@+id/ninth_row"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/four_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/four_button" />
        <Button
            android:id="@+id/five_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/five_button" />
        <Button
            android:id="@+id/six_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/six_button" />
        <Button
            android:id="@+id/multiplication"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/multiplication" />
        <Button
            android:id="@+id/division"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/division" />
    </LinearLayout>
    <!--Ten Row 5 Text-->
    <LinearLayout
        android:id="@+id/tenth_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/pi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:text="@string/pi"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/e"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:gravity="center"
            android:text="@string/e"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/comma"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/comma"
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text=""
            android:gravity="center"
            android:textColor="@color/divider"
            android:layout_weight="1"/>
    </LinearLayout>
    <!--Eleven Row 5 Buttons-->
    <LinearLayout
        android:id="@+id/Eleven_row"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/one_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/one_button" />
        <Button
            android:id="@+id/two_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/two_button" />
        <Button
            android:id="@+id/three_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/three_button" />
        <Button
            android:id="@+id/addition"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/addition" />
        <Button
            android:id="@+id/subtraction"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/subtraction" />
    </LinearLayout>
    <!--Twelve Row 5 Buttons-->
    <LinearLayout
        android:id="@+id/twelve_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="8dp">
        <Button
            android:id="@+id/zero_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:textColor="@color/calculator_color"
            android:padding="8dp"
            android:layout_marginRight="1dp"
            android:textAllCaps="false"
            android:background="@color/calculator_foreground"
            android:text="@string/zero_button" />
        <Button
            android:id="@+id/dot"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/dot" />
        <Button
            android:id="@+id/exp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/exp" />
        <Button
            android:id="@+id/ans"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/ans" />
        <Button
            android:id="@+id/equal_sign"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="24sp"
            android:padding="8dp"
            android:textAllCaps="false"
            android:layout_marginRight="1dp"
            android:textColor="@color/calculator_color"
            android:background="@color/calculator_foreground"
            android:text="@string/equal_sign" />
    </LinearLayout>
</LinearLayout>

We are using in-built View controls available in android. We can as well create a custom View that will take care of the calculator buttons and text.

In the layout file, we made use of many text string which we store in the strings.xml file located in the values folder.

Open this file, copy and paste the following code in the xml file.

<resources>
    <string name="app_name">Android Scientific Calculator</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="no_text"> </string>
    <string name="rcl">RCL</string>
    <string name="sto">STO</string>
    <string name="m_minus">M-</string>
    <string name="shift">SHIFT</string>
    <string name="rad">RAD</string>
    <string name="abs">ABS</string>
    <string name="mr">MR</string>
    <string name="ms">MS</string>
    <string name="m_plus">M+</string>
    <string name="inverse_sin">sin1</string>
    <string name="inverse_cos">cos1</string>
    <string name="inverse_tan">tan1</string>
    <string name="expo">ex</string>
    <string name="ten_power">10x</string>
    <string name="rnd">rnd</string>
    <string name="sin_sign">sin</string>
    <string name="cos_sign">cos</string>
    <string name="tan_sign">tan</string>
    <string name="natural_log">ln</string>
    <string name="log">log</string>
    <string name="cube_root">3?</string>
    <string name="cube">x3</string>
    <string name="one_over_x">1/x</string>
    <string name="x_power_y">Yx</string>
    <string name="square_root">/</string>
    <string name="x_square">x^2</string>
    <string name="percent">%</string>
    <string name="open_bracket">(</string>
    <string name="close_bracket">)</string>
    <string name="seven_button">7</string>
    <string name="eight_button">8</string>
    <string name="nine_button">9</string>
    <string name="single_delete">del</string>
    <string name="clear">AC</string>
    <string name="factorial">n!</string>
    <string name="combination">c(n,r)</string>
    <string name="permutation">p(n,r)</string>
    <string name="rate">Rate</string>
    <string name="four_button">4</string>
    <string name="five_button">5</string>
    <string name="six_button">6</string>
    <string name="multiplication">x</string>
    <string name="division">/</string>
    <string name="pi">ii</string>
    <string name="e">e</string>
    <string name="comma">,</string>
    <string name="one_button">1</string>
    <string name="two_button">2</string>
    <string name="three_button">3</string>
    <string name="addition">+</string>
    <string name="subtraction">-</string>
    <string name="zero_button">0</string>
    <string name="dot">.</string>
    <string name="exp">exp</string>
    <string name="ans">Ans</string>
    <string name="equal_sign">=</string>
    <string name="x_value">x</string>
    <string name="plus_minus">+/-</string>
    <string name="plot">Plot</string>
    <string name="asterisk">*</string>
</resources>

Also, we have stored the colors we used in the colors.xml file. Copy and paste the code to this file. If there is no colors.xml in your values folder try and create one before you continue.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="calculator_background">#2196F3</color>
    <color name="calculator_foreground">#135ba3</color>
    <color name="calculator_color">#d9dfe4</color>
    <color name="calculator_screen">#d1e3e7</color>
    <color name="icons">#FFFFFF</color>
    <color name="divider">#B6B6B6</color>
    <color name="primary_text">#212121</color>  
</resources>

Now that we have done with the layout file for our android scientific calculator, we will move on to MainActivity.java. We are going to get the instance references of our layout View controls. The View onClick listener is implemented for the button Views.

When a button is click, the text content of the button is retrieved and it is use to implement the action that the button is associated with.

The equal button is used to get the answer of when has been queue in the calculator display.

Open the MainActivity.java file, copy and paste the following code in this file.

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
    private TextView outputResult;
    private TextView shiftDisplay;
    private TextView degreeRad;
    private boolean isDegree = false;
    private boolean isInverse = false;
    private String lastResultObtain = "";
    private String currentDisplayedInput = "";
    private String inputToBeParsed = "";
    private Calculator mCalculator;
    private static String PREFS_NAME = "memory";
        private Button button0, button1, button2,button3,button4,button5,button6,button7,
            button8,button9,buttonClear, buttonDivide,buttonMultiply,buttonSubtract,
            buttonAdd, buttonPercentage, buttonEqual, buttonDecimal, closeParenthesis, openParenthesis, buttonAnswer,
            buttonSingleDelete, buttonExp;
    private TextView labelFactorial, labelCombination, labelPermutation, labelPi, labelE, labelComma, labelCubeRoot, labelCube,
            labelInverseX, labelInverseSin, labelInverseCos, labelInverseTan, labelExponential, labelTenPowerX, labelRCL,
            labelSTO, labelMMinus, labelFloat, labelDeg;
    private Button buttonSin, buttonLn,buttonCos, buttonLog, buttonTan, buttonSquareRoot,  buttonXSquare, buttonYPowerX,
            buttonRnd;
    private Button buttonShift, buttonRad, buttonAbs, buttonMr, buttonMs, buttonMPlus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mCalculator = new Calculator();
        outputResult = (TextView)findViewById(R.id.display);
        outputResult.setText("");
        shiftDisplay = (TextView)findViewById(R.id.shift_display);
        degreeRad = (TextView)findViewById(R.id.degree);
        button0 = (Button)findViewById(R.id.zero_button);
        button1 = (Button)findViewById(R.id.one_button);
        button2 = (Button)findViewById(R.id.two_button);
        button3 = (Button)findViewById(R.id.three_button);
        button4 = (Button)findViewById(R.id.four_button);
        button5 = (Button)findViewById(R.id.five_button);
        button6 = (Button)findViewById(R.id.six_button);
        button7 = (Button)findViewById(R.id.seven_button);
        button8 = (Button)findViewById(R.id.eight_button);
        button9 = (Button)findViewById(R.id.nine_button);
        buttonDivide = (Button)findViewById(R.id.division);
        buttonMultiply = (Button)findViewById(R.id.multiplication);
        buttonSubtract = (Button)findViewById(R.id.subtraction);
        buttonAdd = (Button)findViewById(R.id.addition);
        buttonPercentage = (Button)findViewById(R.id.percent);
        buttonDecimal = (Button)findViewById(R.id.dot);
        closeParenthesis = (Button)findViewById(R.id.close_bracket);
        openParenthesis = (Button)findViewById(R.id.open_bracket);
        buttonExp = (Button)findViewById(R.id.exp);
        buttonSquareRoot = (Button)findViewById(R.id.square_root);
        buttonXSquare = (Button)findViewById(R.id.x_square);
        buttonYPowerX = (Button)findViewById(R.id.x_power_y);
        buttonSin = (Button)findViewById(R.id.sin_sign);
        buttonCos = (Button)findViewById(R.id.cos_sign);
        buttonTan = (Button)findViewById(R.id.tan_sign);
        buttonLn = (Button)findViewById(R.id.natural_log);
        buttonLog = (Button)findViewById(R.id.log);
        buttonRnd = (Button)findViewById(R.id.hys);
        buttonDivide.setText(Html.fromHtml(Helpers.division));
        buttonSquareRoot.setText(Html.fromHtml(Helpers.squareRoot));
        buttonXSquare.setText(Html.fromHtml(Helpers.xSquare));
        buttonYPowerX.setText(Html.fromHtml(Helpers.yPowerX));
        buttonShift = (Button)findViewById(R.id.shift);
        buttonRad = (Button)findViewById(R.id.rad);
        buttonAbs = (Button)findViewById(R.id.abs);
        buttonMr = (Button)findViewById(R.id.mr);
        buttonMs = (Button)findViewById(R.id.ms);
        buttonMPlus = (Button)findViewById(R.id.m_plus);
        buttonClear = (Button)findViewById(R.id.clear);
        buttonSingleDelete = (Button)findViewById(R.id.single_delete);
        buttonEqual = (Button)findViewById(R.id.equal_sign);
        buttonAnswer = (Button)findViewById(R.id.ans);
        labelFactorial = (TextView)findViewById(R.id.factorial);
        labelCombination = (TextView)findViewById(R.id.combination);
        labelPermutation = (TextView)findViewById(R.id.permutation);
        labelPi = (TextView)findViewById(R.id.pi);
        labelE = (TextView)findViewById(R.id.e);
        labelComma = (TextView)findViewById(R.id.comma);
        labelCubeRoot = (TextView)findViewById(R.id.cube_root);
        labelCube = (TextView)findViewById(R.id.cube);
        labelInverseX = (TextView)findViewById(R.id.one_over_x);
        labelInverseSin = (TextView)findViewById(R.id.inverse_sin);
        labelInverseCos = (TextView)findViewById(R.id.inverse_cos);
        labelInverseTan = (TextView)findViewById(R.id.inverse_tan);
        labelExponential = (TextView)findViewById(R.id.expo);
        labelTenPowerX = (TextView)findViewById(R.id.ten_power_x);
        labelRCL = (TextView)findViewById(R.id.rcl);
        labelSTO = (TextView)findViewById(R.id.sto);
        labelMMinus = (TextView)findViewById(R.id.m_minus);
        labelFloat = (TextView)findViewById(R.id.float_number);
        labelDeg = (TextView)findViewById(R.id.degree);
        labelInverseSin.setText(Html.fromHtml(Helpers.inverseSin));
        labelInverseCos.setText(Html.fromHtml(Helpers.inverseCos));
        labelInverseTan.setText(Html.fromHtml(Helpers.inverseTan));
        labelExponential.setText(Html.fromHtml(Helpers.exponential));
        labelTenPowerX.setText(Html.fromHtml(Helpers.tenPowerX));
        labelCubeRoot.setText(Html.fromHtml(Helpers.cubeSquare));
        labelCube.setText(Html.fromHtml(Helpers.cubeRoot));
        labelPi.setText(Html.fromHtml(Helpers.pi));
        button0.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
        buttonDivide.setOnClickListener(this);
        buttonMultiply.setOnClickListener(this);
        buttonSubtract.setOnClickListener(this);
        buttonAdd.setOnClickListener(this);
        buttonPercentage.setOnClickListener(this);
        buttonEqual.setOnClickListener(this);
        buttonDecimal.setOnClickListener(this);
        closeParenthesis.setOnClickListener(this);
        openParenthesis.setOnClickListener(this);
        buttonSingleDelete.setOnClickListener(this);
        buttonExp.setOnClickListener(this);
        buttonSquareRoot.setOnClickListener(this);
        buttonXSquare.setOnClickListener(this);
        buttonYPowerX.setOnClickListener(this);
        buttonSin.setOnClickListener(this);
        buttonCos.setOnClickListener(this);
        buttonTan.setOnClickListener(this);
        buttonLn.setOnClickListener(this);
        buttonLog.setOnClickListener(this);
        buttonRnd.setOnClickListener(this);
        buttonShift.setOnClickListener(this);
        buttonRad.setOnClickListener(this);
        buttonAbs.setOnClickListener(this);
        buttonMr.setOnClickListener(this);
        buttonMs.setOnClickListener(this);
        buttonMPlus.setOnClickListener(this);
    }
    private void obtainInputValues(String input){
        switch (input){
            case "0":
                currentDisplayedInput += "0";
                inputToBeParsed += "0";
                break;
            case "1":
                if(isInverse){
                    currentDisplayedInput += "π";
                    inputToBeParsed += "pi";
                }else{
                    currentDisplayedInput += "1";
                    inputToBeParsed += "1";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "2":
                if(isInverse){
                    currentDisplayedInput += "e";
                    inputToBeParsed += "e";
                }else{
                    currentDisplayedInput += "2";
                    inputToBeParsed += "2";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "3":
                if(isInverse){
                    currentDisplayedInput += ",";
                    inputToBeParsed += ",";
                }else{
                    currentDisplayedInput += "3";
                    inputToBeParsed += "3";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "4":
                if(isInverse){
                    currentDisplayedInput += "!(";
                    inputToBeParsed += "!(";
                }else{
                    currentDisplayedInput += "4";
                    inputToBeParsed += "4";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "5":
                if(isInverse){
                    currentDisplayedInput += "comb(";
                    inputToBeParsed += "comb(";
                }else{
                    currentDisplayedInput += "5";
                    inputToBeParsed += "5";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "6":
                if(isInverse){
                    currentDisplayedInput += "permu(";
                    inputToBeParsed += "permu(";
                }else{
                    currentDisplayedInput += "6";
                    inputToBeParsed += "6";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "7":
                currentDisplayedInput += "7";
                inputToBeParsed += "7";
                break;
            case "8":
                currentDisplayedInput += "8";
                inputToBeParsed += "8";
                break;
            case "9":
                currentDisplayedInput += "9";
                inputToBeParsed += "9";
                break;
            case ".":
                currentDisplayedInput += ".";
                inputToBeParsed += ".";
                break;
            case "+":
                currentDisplayedInput += "+";
                inputToBeParsed += "+";
                break;
            case "-":
                currentDisplayedInput += "-";
                inputToBeParsed += "-";
                break;
            case "÷":
                currentDisplayedInput += "÷";
                inputToBeParsed += "/";
                break;
            case "x":
                currentDisplayedInput += "*";
                inputToBeParsed += "*";
                break;
            case "(":
                currentDisplayedInput += "(";
                inputToBeParsed += "(";
                break;
            case ")":
                currentDisplayedInput += ")";
                inputToBeParsed += ")";
                break;
            case "%":
                if(isInverse){
                    currentDisplayedInput += "1÷";
                    inputToBeParsed += "1÷";
                }else{
                    currentDisplayedInput += "%";
                    inputToBeParsed += "%";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "ln":
                if(isInverse){
                    currentDisplayedInput += "e^";
                    inputToBeParsed += "e^";
                }else{
                    currentDisplayedInput += "ln(";
                    inputToBeParsed += "ln(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "log":
                if(isInverse){
                    currentDisplayedInput += "10^";
                    inputToBeParsed += "10^";
                }else{
                    currentDisplayedInput += "log(";
                    inputToBeParsed += "log(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "√":
                if(isInverse){
                    currentDisplayedInput += "3√(";
                    inputToBeParsed += "crt(";
                }else{
                    currentDisplayedInput += "√(";
                    inputToBeParsed += "sqrt(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "Yx":
                currentDisplayedInput += "^";
                inputToBeParsed += "^";
                break;
            case "sin":
                if(isInverse){
                    currentDisplayedInput += "asin(";
                    inputToBeParsed += "asin(";
                }else{
                    currentDisplayedInput += "sin(";
                    inputToBeParsed += "sin(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "cos":
                if(isInverse){
                    currentDisplayedInput += "acos(";
                    inputToBeParsed += "acos(";
                }else{
                    currentDisplayedInput += "cos(";
                    inputToBeParsed += "cos(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "tan":
                if(isInverse){
                    currentDisplayedInput += "atan(";
                    inputToBeParsed += "atan(";
                }else{
                    currentDisplayedInput += "tan(";
                    inputToBeParsed += "tan(";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "exp":
                currentDisplayedInput += "E";
                inputToBeParsed += "E0";
                break;
            case "x2":
                if(isInverse){
                    currentDisplayedInput += "^3";
                    inputToBeParsed += "^3";
                }else{
                    currentDisplayedInput += "^2";
                    inputToBeParsed += "^2";
                }
                toggleInverse();
                toggleShiftButton();
                break;
            case "rnd":
                double ran = Math.random();
                currentDisplayedInput += String.valueOf(ran);
                inputToBeParsed += String.valueOf(ran);
                break;
            case "ABS":
                currentDisplayedInput += "abs(";
                inputToBeParsed += "abs(";
                break;
            case "MR":
                String mValue = getStoredPreferenceValue(MainActivity.this);
                String result = removeTrailingZero(mValue);
                if(!result.equals("0")){
                    currentDisplayedInput += result;
                    inputToBeParsed += result;
                }
                break;
            case "MS":
                clearMemoryStorage(MainActivity.this);
                break;
            case "M+":
                if (isInverse){
                    double inputValueMinus  = isANumber(outputResult.getText().toString());
                    if(!Double.isNaN(inputValueMinus)){
                        subtractMemoryStorage(MainActivity.this, inputValueMinus);
                    }
                }else{
                    double inputValue  = isANumber(outputResult.getText().toString());
                    if(!Double.isNaN(inputValue)){
                        addToMemoryStorage(MainActivity.this, inputValue);
                    }
                }
                toggleInverse();
                toggleShiftButton();
                break;
        }
        outputResult.setText(currentDisplayedInput);
    }
    @Override
    public void onClick(View view) {
        Button button = (Button) view;
        String data = button.getText().toString();
        //Toast.makeText(this, "Click " + data, Toast.LENGTH_LONG).show();
        if(data.equals("AC")){
            outputResult.setText("");
            currentDisplayedInput = "";
            inputToBeParsed = "";
        }
        else if(data.equals("del")){
            String enteredInput = outputResult.getText().toString();
            if(enteredInput.length() > 0){
                enteredInput = enteredInput.substring(0, enteredInput.length() - 1);
                currentDisplayedInput = enteredInput;
                inputToBeParsed = enteredInput;
                outputResult.setText(currentDisplayedInput);
            }
        }else if(data.equals("=")){
            String enteredInput = outputResult.getText().toString();
            // call a function that will return the result of the calculate.
            String resultObject = mCalculator.getResult(currentDisplayedInput, inputToBeParsed);
            outputResult.setText(removeTrailingZero(resultObject));
        }else if(data.equals("Ans")){
            String enteredInput = outputResult.getText().toString();
            enteredInput += lastResultObtain;
            outputResult.setText(enteredInput);
        }else if(data.equals("SHIFT")){
            if(!isInverse){
                isInverse = true;
            }else{
                isInverse = false;
            }
            toggleShiftButton();
        }else if(data.equals("RAD")){
            buttonRad.setText("DEG");
            degreeRad.setText("RAD");
        }
        else if(data.equals("DEG")){
            buttonRad.setText("RAD");
            degreeRad.setText("DEG");
        }else{
            obtainInputValues(data);
        }
    }
    private String removeTrailingZero(String formattingInput){
        if(!formattingInput.contains(".")){
            return formattingInput;
        }
        int dotPosition = formattingInput.indexOf(".");
        String newValue = formattingInput.substring(dotPosition, formattingInput.length());
        if(newValue.equals(".0")){
            return formattingInput.substring(0, dotPosition);
        }
        return formattingInput;
    }
    private void toggleInverse(){
        if(isInverse){
            isInverse = false;
        }
    }
    private void toggleShiftButton(){
        if(isInverse){
            shiftDisplay.setText("SHIFT");
        }else{
            shiftDisplay.setText("");
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private double isANumber(String numberInput){
        double result = Double.NaN;
        try{
            result = Double.parseDouble(numberInput);
        }catch(NumberFormatException nfe){
        }
        return result;
    }
    private void addToMemoryStorage(Context context, double inputToStore){
        float returnPrefValue = getPreference(context);
        float newValue = returnPrefValue + (float)inputToStore;
        setPreference(context, newValue);
    }
    private void subtractMemoryStorage(Context context, double inputToStore){
        float returnPrefValue = getPreference(context);
        float newValue = returnPrefValue - (float)inputToStore;
        setPreference(context, newValue);
    }
    private void clearMemoryStorage(Context context){
        setPreference(context, 0);
    }
    private String getStoredPreferenceValue(Context context){
        float returnedValue = getPreference(context);
        return String.valueOf(returnedValue);
    }
    static public boolean setPreference(Context c, float value) {
        SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
        settings = c.getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat("key", value);
        return editor.commit();
    }
    static public float getPreference(Context c) {
        SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
        settings = c.getSharedPreferences(PREFS_NAME, 0);
        float value = settings.getFloat("key", 0);
        return value;
    }
    
}

When you paste the above code on MainActivity.java file, you will notice that there are other class we will use to complete the functions of the scientific calculator.

We will create a Helper class which we will use to create HTML formatted string and few functions.

The code that should go into this file is pasted below.

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class Helpers {
    public static String division = "&divide;";
    public static String inverseSin = "sin<sup>-1</sup>";
    public static String inverseCos = "cos<sup>-1</sup>";
    public static String inverseTan = "tan<sup>-1</sup>";
    public static String exponential = "e<sup>x</sup>";
    public static String tenPowerX = "10<sup>x</sup>";
    public static String cubeSquare = "3&radic;";
    public static String cubeRoot = "x<sup>3</sup>";
    public static String yPowerX = "Y<sup>x</sup>";
    public static String squareRoot = "&radic;";
    public static String xSquare = "x<sup>2</sup>";
    public static String pi = "&pi;";
    public static void displayErrorMessage(Context context){
        Toast.makeText(context, "Input field must not be zero", Toast.LENGTH_LONG).show();
    }
    public static boolean isZero(EditText input){
        if(Double.parseDouble(input.getText().toString()) == 0){
            return true;
        }
        return false;
    }
    public static int getTopicId(Bundle bundle, String inputValue){
        int id = 0;
        if(bundle != null){
            id  = bundle.getInt(inputValue);
        }
        return id;
    }
}

Finally, we are going to write a Math Parse class that will take our inputted math equation and evaluate it. The class will return the result of the math equation. If the math equation is incorrect an Error message will be displayed on the screen.

We are going to make use of a third-party library for math evaluation. There are many libraries out there but we are going to use Javalutor. Add this library to your application. If you don’t know how to add java third-party library to your project, I will suggest that you read one of my tutorial on this topic.

I adopted the Math Parse class using the Javalutor library from here, although I have modify the class to suit our own need but credit goes to the originator.

The Calculator class math parse code is shown below. Create this class in your application and copy and paste the below code in it.

import com.fathzer.soft.javaluator.DoubleEvaluator;
import com.fathzer.soft.javaluator.Function;
import com.fathzer.soft.javaluator.Parameters;
import java.util.ArrayList;
import java.util.Iterator;
public class Calculator {
    // The function has one argument and its name is "sqrt"
    final Function sqrt = new Function("sqrt", 1);
    final Function factorial = new Function("!", 1);
    final Function cuberoot = new Function("crt", 1);
    final Function combination = new Function("comb", 2);
    final Function permutation = new Function("permu", 2);
    Parameters params;
    DoubleEvaluator evaluator;
    private double previousSum = 0;
    private double currentSum = 0;
    private String currentDisplay = "";
    //private String expressionUsedForParsing ="";
    private boolean isRadians = false;
    public Calculator() {
        addFunctions();
        //Adds the functions to the evaluator
        evaluator = new DoubleEvaluator(params) {
            @Override
            protected Double evaluate(Function function, Iterator arguments, Object evaluationContext) {
                if (function == sqrt)
                    return Math.sqrt((Double) arguments.next());
                else if(function == cuberoot){
                    return Math.cbrt((Double) arguments.next());
                }
                else if(function == combination){
                    double numberInputs = 0;
                    ArrayList<Double> saveValue = new ArrayList<Double>();
                    while(arguments.hasNext()) {
                        numberInputs = (Double) arguments.next();
                        saveValue.add(numberInputs);
                    }
                    double firstArgument = saveValue.get(0);
                    double secondArgument = saveValue.get(1);
                    double denominator = getFactorial((int) firstArgument);
                    double nominator = getFactorial((int)secondArgument) * (getFactorial((int)(firstArgument - secondArgument)));
                    return denominator / nominator;
                }
                else if(function == permutation){
                    double numberInputs = 0;
                    ArrayList<Double> saveValue = new ArrayList<Double>();
                    while(arguments.hasNext()) {
                        numberInputs = (Double) arguments.next();
                        saveValue.add(numberInputs);
                    }
                    double firstArgument = saveValue.get(0);
                    double secondArgument = saveValue.get(1);
                    double denominator = getFactorial((int) firstArgument);
                    double nominator = (getFactorial((int)(firstArgument - secondArgument)));
                    return denominator / nominator;
                }
                else if (function == factorial) {
                    double result = 1;
                    double num = (Double) arguments.next();
                    for (int i = 2; i <= num; i++) {
                        result = result * i;
                    }
                    return result;
                } else
                    return super.evaluate(function, arguments, evaluationContext);
            }
        };
    }
    private int getFactorial(int n)    {
        int result;
        if(n==0 || n==1)
            return 1;
        result = getFactorial(n-1) * n;
        return result;
    }
    public void addFunctions() {
        params = DoubleEvaluator.getDefaultParameters();
        params.add(sqrt);
        params.add(factorial);
        params.add(cuberoot);
        params.add(combination);
        params.add(permutation);
    }
    public String getResult(String currentDisplay, String expressionUsedForParsing) {
        //Tries to parse the information as it is entered, if the parser can't handle it, the word error is shown on screen
        try {
            System.out.println("Displayed Output " + expressionUsedForParsing);
            currentSum = evaluator.evaluate(fixExpression(expressionUsedForParsing));
            currentSum = convertToRadians(currentSum);
            currentDisplay = String.valueOf(currentSum);
            //previousSum = currentSum;
        } catch (Exception e) {
            currentDisplay = "Error";
        }
        return currentDisplay;
    }
    public double convertToRadians(double sum){
        double newSum = sum;
        if(isRadians == true)
            newSum = Math.toRadians(sum);
        return newSum;
    }
    //Used to show display to user
    public String getCurrentDisplay() {
        return currentDisplay;
    }
    //Handles fixing the expression before parsing. Adding parens, making sure parens can multiply with each other,
    public String fixExpression(String exp) {
        int openParens = 0;
        int closeParens = 0;
        char openP = '(';
        char closeP = ')';
        String expr = exp;
        for (int i = 0; i < exp.length(); i++) {
            if (exp.charAt(i) == openP)
                openParens++;
            else if (exp.charAt(i) == closeP)
                closeParens++;
        }
        while (openParens > 0) {
            expr += closeP;
            openParens--;
        }
        while (closeParens > 0) {
            expr = openP + expr;
            closeParens--;
        }
        expr = multiplicationForParens(expr);
        return expr;
    }
    //Used to fix multiplication between parentheses
    public String multiplicationForParens(String s) {
        String fixed = "";
        for (int position = 0; position < s.length(); position++) {
            fixed += s.charAt(position);
            if (position == s.length() - 1)
                continue;
            if (s.charAt(position) == ')' && s.charAt(position + 1) == '(')
                fixed += '*';
            if (s.charAt(position) == '(' && s.charAt(position + 1) == ')')
                fixed += '1';
        }
        return fixed;
    }
}

This brings us to the end of this tutorial, If you find anything confusing kindly contact me with your questions or use the comment box below.

Now, when you run your application you will see the interface that looks similar to the sample that was shown earlier on.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

If you like this tutorial, you can see the Scientific Calculator in action in my Complete Mathematics App in Google Play Store. You are free to use and modify the source code for your own use.

29 Comments

    • Inducesmile
    • Inducesmile
    • Inducesmile
    • Inducesmile
    • Inducesmile

Add a Comment