Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Android Applications and Their Categories
Next article icon

Creating a Calculator for Android devices

Last Updated : 29 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • CalculatorClass

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:

1

2



Next Article
Android Applications and Their Categories

S

Syed Hassan Ashraf
Improve
Article Tags :
  • Android
  • Project

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 met
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences