Creating a Calculator for Android devices
Last Updated :
29 Sep, 2022
This post describes how to create a calculator application especially to help in competitive coding for android devices. The apk created can be used to install the application on different devices. The project has been designed for entry level android programmers.
The Calculator Application
- The calculator can handle arbitrary large numbers
- Arithmetic operations implemented : +,-,*,/
- Boolean operations implemented : and, or, not, xor
- Primality check, finding mod, exponent, GCD operations has also been implemented
You will also need
Android Studio to build and debug the application.
About the interface
First, we will create the GUI of our application. Android simplifies our work by letting us separate the designing phase from other work. Layouts are used to create the screens that you see when you open an app on an Android device. Each screen that you see usually sees has a different layout file and a different java file to handle interactions related to that screen (also known as user interfaces) and together they comprise an activity in android. The layout designer contains most of the simple widgets that can be arranged with just drag and drop and even the attribute value for those can be set from the GUI.
- The interface of our calculator application will consist of a grid layout that will contain all the buttons. The button consists of 10 number buttons (0-9), buttons for arithmetics, boolean and miscellaneous operations.
There are some more widgets to display text that are known as TextView in android. We have five TextView , two to show input text, one to show operation selected and one for result. The one left widget is used to display some permanent information, in this case it's my name.
- The layouts reside in res/layout folder and are defined in xml format. So, create an activity_main.xml layout file is res/layout directory and copy the code below. If you are using Android Studio activity_main.xml will be created automatically when you create a new project.
- Activity_main.xml : This code creates a user interface for the user to interact with in an XML file. LinearLayout and RelativeLayout are used to arrange these widgets (say button) in a hierarchy. In relative layout you can arrange the children with positions related to each other whereas linear layout arranges them in a linear fashion.
Working of Interface
So that's it for our designing phase but we still have to define the brains behind our user interface. For example, the thing that will happen when you will click a particular button or interact with any other layout object. This brain behind the activity thing is contained in
MainActivity.java file which handles all the responses to user interaction related to a particular interface and can also perform additional work.
- MainActivity.java: Open mainactivity.java (this file is also created automatically) and copy the following code in it. Here the MainActivity sets the above XML file as it's the corresponding interface and also handles interactions on that user interface, specifically the button clicks. Here is the code that goes into MainActivity.java file.
Performing calculations
The above class handles all user interactions and the only thing left is to calculate the actual result. Now we have to create one more class that handles all the calculation related work. This work has been separated from our MainActivity class. Create a new calculator class and copy the following code in it. This is used to handle all the calculations. Since all the member functions of this are defined as static, so we call them without making an object of calculator class.
Button click animations
There is one more little thing left. We still have to define the animation related to button presses, as when you press a button it’s zooms in and then goes back to it’s normal size. These animation has been defined in a separate xml file and called when needed. Create a file
scale.xml in
res/anim folder and paste the following code in it.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:duration="50"
android:pivotY="50%"
android:pivotX="50%"
android:repeatCount="1"
android:repeatMode="reverse"/> </set>
That's it, you are set to go. You can run the project in an emulator by clicking the Run button or you can install the generated apk and run it in a physical android device. The project will run perfectly in 5 inch screen (1280*720 resolution is perfect). Here are some screenshot obtained from running the app in Yu Yuphoria.
Output:
Similar Reads
Creating a Calendar View app in Android
This article shows how to create an android application for displaying the Calendar using CalendarView. It also provides the selection of the current date and displaying the date. The setOnDateChangeListener Interface is used which provide onSelectedDayChange method.onSelectedDayChange: In this meth
2 min read
How to Build Age Calculator in Android Studio?
Hello geeks, today we are going to make an application to calculate Age or time period between two dates. By making this application one can calculate his/her exact age, also one can calculate the exact difference between two dates. Prerequisites: Before making this application, you can go through t
6 min read
How to Create a Voting Application in Android?
In general, voting means comparing two or more entities on the basis of certain conditions. In this article, we will create a simple voting application that uses two different programming languages namely Java and Python, and ask the user to vote for their preferred language. A sample GIF is given b
3 min read
Secret Codes for Android Devices
Android devices are the latest essential element in human life. Earlier, human has mainly three essential needs. Those are the Food, Shelter & Cloth. But now, with the latest technology, this list is increasing rapidly. And there is no doubt that android devices are one of the major elements in
8 min read
Android Applications and Their Categories
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
5 min read
Android - Create a Pie Chart with Kotlin
A Pie Chart is a circular type of UI interface which is used to display the data in the form of percentages for different categories. This is used to display a vast amount of data. In this article, we will take a look at building a pie chart in an Android application using Kotlin. A sample video is
5 min read
How to Create a Quiz App In Android?
Android is an operating system which is basically made for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. Android is very popular nowadays among students and students are now choosing Android for their projects. It's very much important for
7 min read
Android | Creating a RatingBar
RatingBar in Android is used to display star-based ratings, commonly seen in applications for reviews and feedback. It allows users to set ratings and returns the rating as a float value for eg. 1.5, 3.0, 4.5, etc. To get the rating value, we use the .rating function in Kotlin and .getRating() funct
3 min read
Creating a Calculator using Calculator Widget in Flutter
If you need a Calculator in Flutter or need to do a small calculation in your flutter application, Writing your own code going to be tough, but Flutter gives you a SimpleCalculator Widget, Which allows you to create a Beautiful simple calculator. Only you need to set up the package in pubspec.yaml f
3 min read
How to Build a Simple Expense Calculator App in Android?
Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioHow to Create/Start a New Project in Android Studio?Running your first Android appRecyclerView in Android with ExampleShared Preferences in Android with Example A simple expense calculator let
10 min read