Android Tutorial – Create Simple Calculator Android App

In my previous blog I wrote in details how can we create a simple contact android app. In that particular app I also explained concepts of Intent, Context Menu and many more.

You can find all my other articles in Android section.

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

Before going forward it will be good to go through below android blogs:

You can download the Contact Android App source code Project from below:

Create Calculator Android App

Assumptions

So lets get started with our calculator android App:

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name “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.hackpundit.www.assignment1 -> MainActivity.java)

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

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.
  • According to that, corresponding action will be performed respective to button clicked.
  • for example

    if Add button is clicked before the “=” button then Add action will be performed as you can see above.
  • After the action performed, we set the mAddition value to false, so that we can perform Add action again.

Below is the layout file, with help to design front end for the calculator:

Android Calculator

Calculator

Now, we are ready to run our calculator android app. To run our app I used genymotion emulator as it’s fast. Also its performance is better than default Android emulator.

If you have not already installed, I highly recommend Genymotion emulator. You can install from here: Android Emulator Genymotion Step by Step Installation. If you face any errors while installing android studio this is a good reference point.

Running our Calculator Android App

  • Click on Genymotion device manager. After selecting your custom device in Genymotion device manager window, click START.
  • Click on Run.
  • Choose Genymotion device and click OK.
  • Now you can see calculator android app running.
Android Calculator

Output 1

 

Android Calculator

Output 2

Congratulations! If you reach this point means you have followed all the steps correctly and your  alculator android app is up and running fine. If you are a beginner and learning android, I recommend go through below tutorial to get you speed up.

If I missed any point or you have any doubt regarding this blog just let me know in the comment section. If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to www.hackpundit.com with your email.

Please also share on Facebook and Twitter to help other android developers. Thank You..!