China Finance

Sep 18 2018

How to create a Calculator App for Android

This application will perform primary mathematical operations like:

This application is developed and composed of two layouts and two primary java classes.


Listing 1. home.xml layout

Figure 1. Quick Calculator

Java Class Files:

Every Android project contains a “res” (reources) folder. This folder is meant to comprise of number of subfolders.

Figure 2. Quick Calculator

In this layout one EditText is used as display and rest of object are button objects, which are labeled from 1 to 0 natural numbers, mathematical operations, equal button, and reset button.

Two java classes are created in order to manage these two layouts for this application, the first class named home class run home.xml layout for 3 seconds using Thread concept and loads the next layout after 3 seconds by closing the running layout (home.xml).

Note. Layout is closed by calling method finish() and other layout is loaded using new Intent mechanism.

Listing 3. Home class defined in the file.

This class extends an Activity class. Two methods of Activity class are overridden in this class, which are onCreate(Bundle) and onPause(). onCreate() method defines, which layout is to be loaded using method: setContentView(R.layout.home);

Next, it defines a new thread, which runs the layout for 3 seconds and load next activity layout by starting a new activity: startActivity(new Intent(“”));

Before starting a new activity, we should close running activity, if it will not be used in future. This can be achieved using onPause() method.

Listing 3. Calculator class defind into the file.

This class describe all the complete functionality for a calculator. This class extends Activity class and implements method of View.OnClickListener interface.

  • onCreate(Bundle) method loads the calc.xml layout.
  • operation() method define all the basic operation for calculator.
  • onClick() method is implemented of OnClickListener interface, which handle all the clicking buttons of calculator application.
  • These all buttons clicks are managed by switch case conditional statement, where case represents the layout attributes id and describe their functionality.
  • setOnClickListener(this) points out click event defined as a switch case in the onClick() method respectively.

Listing 4. XML. AndroidManifest.xml

The AndroidManifest.xml file is the main configuration file for Quick Calculator android application. This loads home layout by default using class Home.class, whose parameters are set into the activity name “.Home”. The next activity defines, how to load next layout (calc). This layout is loaded by Calc.class defined as parameters in this second activity.

These two activities are enclosed into the main xml tag. which describes information about the application representing, when an application starts.

Sample Output:

Figure 3. Sample Output of a Quick Calculator


We learned the process to come up with a simple calculator application in Android. See you next time.

Anant Khurana

Working in Software Development domain from 7 years now and is well equipped with programming languages like HTML, CSS, Java, PHP. NET etc.

What did you think of this post?

To have full access to this post (or download the associated files) you must have MrBool Credits .

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

You must be logged to download.

[Close] You must be logged to give your feedback.

Written by admin

Leave a Reply

Your email address will not be published. Required fields are marked *