Android Tutorial – Simple Calculator Android App | Calculator Source Code

Android Tutorial – Simple Calculator Android App | Calculator Source Code

June 22, 2018 0 By Aditya Rajput

Lets Create Our Very Own Android Calculator App – Calculator Source Code for Your Help

android calculator

In this blog we will create a calculator android appThis is a simple calculator with limited functionality.

Assumptions

  • You have Android Studio Installed in your system.
  • You are clear with concepts of Java and Android Studio.

So lets get started with our Android Calculator App, here I will describe the steps Required For Creating your App. I have Provided Calculator Source Code And And I will be Describing these steps in brief  :

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name “ Android Calculator”  and leave other fields blank as it is, then click NEXT.
  • Select the Minimum SDK API 15: Android 4.0.3(IceCreamSandwich)”I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features. After selecting your API click on NEXT.
  • Select the blank activity and click NEXT.
  • Leave the activity name “MainActivity” and leave everything as it is. Click Finish.
  • After clicking Finish, it takes around 5 minutes to build Activity.

Now let’s do code in our MainActivity.JAVA file.

  • So open you MainActivity.java file(app  -> java -> com.buzzhipster.www.assignment1 -> MainActivity.java)

You can find the explanation of highlighted line below the code.

here is our calculator source code of java file.

<code>
package com.buzzhipster.www.assignment1;



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button button0 , button1 , button2 , button3 , button4 , button5 , button6 ,
            button7 , button8 , button9 , buttonAdd , buttonSub , buttonDivision ,
            buttonMul , button10 , buttonC , buttonEqual ;

    EditText edt1 ;

    float mValueOne , mValueTwo ;

    boolean mAddition , mSubtract ,mMultiplication ,mDivision ;

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

        button0 = (Button) findViewById(R.id.button0);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);
        button8 = (Button) findViewById(R.id.button8);
        button9 = (Button) findViewById(R.id.button9);
        button10 = (Button) findViewById(R.id.button10);
        buttonAdd = (Button) findViewById(R.id.buttonadd);
        buttonSub = (Button) findViewById(R.id.buttonsub);
        buttonMul = (Button) findViewById(R.id.buttonmul);
        buttonDivision = (Button) findViewById(R.id.buttondiv);
        buttonC = (Button) findViewById(R.id.buttonC);
        buttonEqual = (Button) findViewById(R.id.buttoneql);
        edt1 = (EditText) findViewById(R.id.edt1);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"1");
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"2");
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"3");
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"4");
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"5");
            }
        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"6");
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"7");
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"8");
            }
        });

        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"9");
            }
        });

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"0");
            }
        });

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

                if (edt1 == null){
                    edt1.setText("");
                }else {
                    mValueOne = Float.parseFloat(edt1.getText() + "");
                    mAddition = true;
                    edt1.setText(null);
                }
            }
        });

        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText() + "");
                mSubtract = true ;
                edt1.setText(null);
            }
        });

        buttonMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText() + "");
                mMultiplication = true ;
                edt1.setText(null);
            }
        });

        buttonDivision.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText()+"");
                mDivision = true ;
                edt1.setText(null);
            }
        });

        buttonEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueTwo = Float.parseFloat(edt1.getText() + "");

                if (mAddition == true){

                    edt1.setText(mValueOne + mValueTwo +"");
                    mAddition=false;
                }


                if (mSubtract == true){
                    edt1.setText(mValueOne - mValueTwo+"");
                    mSubtract=false;
                }

                if (mMultiplication == true){
                    edt1.setText(mValueOne * mValueTwo+"");
                    mMultiplication=false;
                }

                if (mDivision == true){
                    edt1.setText(mValueOne / mValueTwo+"");
                    mDivision=false;
                }
            }
        });

        buttonC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
            }
        });

        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+".");
            }
        });
    }



}
</code>

Lets Now define the various lines used in the android calculator app –

Here we have 1 EditText. It defines the type of content.

  • Line 15 – 19: Here we created the reference of Buttons and EditText.
  • Line 21: Here we created two float variable for as value1 and value2.
  • Line 26: We override the method onCreate() which is the method of Activity class.
  • Line 50 – 55: We set click listener on Button1. If we click on Button 1, EditText will display 1.

                                        edt1.setText(edt1.getText()+“1”);

  • We have implemented the same logic for every button.
  • Line 120 – 132: Here we have set the click listener on Add button.
  • Here we put the condition as, if we EditText is Null then we set EditText as empty value. Else we add the two value which are clicked before add button clicked and after add button clicked.
  • We also set the mAddition Boolean value to True. This true represent that add button is clicked and this will be used when user click “=” button.
  • We implement the same logic for other buttons also like buttonSub, ButtonMul, buttonDivision.
  • Line 161 – 188: Here we set click listener on “=” button. Here we put condition like if user click Add button the mAddition value is set True on the click listener of Add button.
  • After the action performed, we set the mAddition value to false, so that we can perform Add action again.

Below is the calculator source code of layout file , with help to design front end for the android calculator app :

 

<code>
<RelativeLayout xmlns: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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/relative1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt1"/>


    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/button1"
        android:layout_marginTop="94dp"
        android:layout_below="@+id/edt1"
        android:layout_toStartOf="@+id/button4"
        android:layout_alignRight="@+id/button4"
        android:layout_alignEnd="@+id/button4" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button1"
        android:layout_toLeftOf="@+id/button3"
        android:layout_toStartOf="@+id/button3" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/button3"
        android:layout_alignTop="@+id/button2"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        android:id="@+id/button4"
        android:layout_below="@+id/button1"
        android:layout_toLeftOf="@+id/button2" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/button5"
        android:layout_alignBottom="@+id/button4"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:id="@+id/button6"
        android:layout_below="@+id/button3"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignStart="@+id/button3" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        android:id="@+id/button7"
        android:layout_below="@+id/button4"
        android:layout_toLeftOf="@+id/button2" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:id="@+id/button8"
        android:layout_below="@+id/button5"
        android:layout_alignLeft="@+id/button5"
        android:layout_alignStart="@+id/button5" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/button9"
        android:layout_below="@+id/button6"
        android:layout_alignLeft="@+id/button6"
        android:layout_alignStart="@+id/button6" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/buttonadd"
        android:layout_alignTop="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:layout_marginLeft="46dp"
        android:layout_marginStart="46dp"
        android:layout_alignRight="@+id/edt1"
        android:layout_alignEnd="@+id/edt1" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/buttonsub"
        android:layout_below="@+id/buttonadd"
        android:layout_alignLeft="@+id/buttonadd"
        android:layout_alignStart="@+id/buttonadd"
        android:layout_alignRight="@+id/buttonadd"
        android:layout_alignEnd="@+id/buttonadd" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"
        android:id="@+id/buttonmul"
        android:layout_below="@+id/buttonsub"
        android:layout_alignLeft="@+id/buttonsub"
        android:layout_alignStart="@+id/buttonsub"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        android:id="@+id/button10"
        android:layout_below="@+id/button7"
        android:layout_toLeftOf="@+id/button2" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/button0"
        android:layout_below="@+id/button8"
        android:layout_alignLeft="@+id/button8"
        android:layout_alignStart="@+id/button8" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        android:id="@+id/buttonC"
        android:layout_below="@+id/button9"
        android:layout_alignLeft="@+id/button9"
        android:layout_alignStart="@+id/button9" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"
        android:id="@+id/buttondiv"
        android:layout_below="@+id/buttonmul"
        android:layout_alignLeft="@+id/buttonmul"
        android:layout_alignStart="@+id/buttonmul"
        android:layout_alignRight="@+id/buttonmul"
        android:layout_alignEnd="@+id/buttonmul" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/buttoneql"
        android:layout_below="@+id/button0"
        android:layout_marginTop="37dp"
        android:layout_alignRight="@+id/buttondiv"
        android:layout_alignEnd="@+id/buttondiv"
        android:layout_alignLeft="@+id/button10"
        android:layout_alignStart="@+id/button10" />

</RelativeLayout>
</code>

ok, we are ready to run our calculator android app.

Congratulations! If you reach this point means you have followed all the steps correctly

and if your calculator source code looks something like that of mine then your calculator android app should be up and running fine.
If I have missed any point regarding this or had done anything wrong in their please let me know it in comments.

Thanks for Reading.