Android Tutorial for Beginners – Create Simple Calculator

Seagatesite – I’ll share about android tutorial on Android Tutorial for Beginners – Create Simple Calculator. Continuing my previous article about the Introduction Android Studio for Beginners, as a warm up we learn to understand the android programming ranging from the simplest applications first. I will create a simple calculator as a tutorial for beginners to learn android. Simple calculator that we will create will be able to do mathematical calculations operations, such as addition, subtraction, multiplication and division.

A simple calculator represents the concept of an overall application. There is input, process and output. By understanding this simple calculator application creation, allows us to understand how to learn android programming.

Android application that we will create more or less like the following picture

Learning Android Simple Calculator Project

There are 3 EditText, 4 Button, 3 TextView, users can enter any number in the field provided, then if the button is clicked will perform its function and if there is an error displays an error message via toast

Lets begin How to Create Simple Calculator for Beginners

1. Create new android project as CalcAPP ( File -> New -> New Project)

Create New Project Learning Android Create Simple Calculator Applications For Beginners
Choose Phone And Tables Minimum Sdk Learning Android Create Simple Calculator Applications For Beginners
Choose Blank Activity Learning Android Create Simple Calculator Applications For Beginners
Customize Activity Learning Android Create Simple Calculator Applications For Beginners

2. Change RelativeLayout to be LinearLayout by changing the text on the display as shown below.

Change Relativelayout To Linearlayout Learning Android Create Simple Calculator Applications For Beginners

Or copy this below

Then add a LinearLayout (horizontal) by 4 units to the root layout (LinearLayout) and each TextView, EditText and Button into each LinearLayout as shown below

Create Edittext Textview Button Learning Android Create Simple Calculator Applications For Beginners

Then edit each widget on the layout by adding text and ID. Or you can copy the following script on activity_main.xml

Why Should change of RelativeLayout be LinearLayout?

I quote from the site http://themasterworld.com/

Linear layout is one of the most important fundamental in android that provides facilities to developer to create there UI. Linear layout is the layout that aligns all layout in one direction horizontal and vertical. Orientation is available in linear layout. vertical and horizontal. It is used for situation when children are rendered from left to right and top to bottom.

LinearLayout make your application look more neat and powerful, easy to set up the display in a linear layout.

3. Add function as addition, subtraction, multiplication and division on MainActivity.java like the following code

4.  Your application is ready for use, please Debug your App by clicking Run -> Debug App (Shift + F9)

Build App Android

Tutorial Learning Android Create Simple Calculator Applications For Beginners

Select your device emulator, in this example, I use genymotion as android emulator. How to install genymotion on linux / windows I will discuss in a future article. Results of a simple calculator application will be as follows

Learning Android Create Simple Calculator Applications For Beginners 2

Explanation :

Layout interfaces on android applications created using programming language XML. In the simple calculator program that was created above, has a parent component (root), which a parameter LinearLayout has layout_width = match_parent and layout_height = match_parent. So this application has a fullscreen view. Different from the parent, LinearLayout contained within the parent root has layout_width parameter = match_parent and layout_height = wrap_content. Match_parent parameter identifies wide layout adapts to the parent component, whereas wrap_content parameter identifies the size of the layout adjusts its components.

Then in MainActivity.java, at the beginning of the line is declared objects such as buttons and EditText. Each activity has onCreate() method. This method was first executed by the system when the application is running. Each activity also has a layout’s partner. OnCreate() method used to declare components or widgets anything contained in the layout’s partner.

Each button be activated with setOnClickListener() method. SetOnclickListener() method will be paired with the onClick() method , which is tasked to handle the process if the button’s clicked.

There are two ways to implement the OnClickListener method on android. for example as the following code line

Example one

Example two

All of the above is true, but the second way is more simple than the first example.

In other opportunity I will discuss how to create Android APK with Android Studio and how to add the Android App Icon

Thus article about Android Tutorial for Beginners – Create Simple Calculator , hope useful.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.