Introduction: How to Create an Android Application
Over the past few years, Android is dominating the market shares of smartphone and tablet operating systems. As of August-2013, Android controlled more than 79% of the smartphone market and more than 62% of the tablet market, according to research by International Data Corp. Meanwhile, a new study projected that Android device sales can potentially cross 900 million a year by 2015.
Referring to the above mentioned statistics, it sounds pretty obvious that having Android application development knowledge and experience has become almost a necessary. So with this, let us begin learning Android app development with our very own loan interest calculator application.
Introduction to Android OS and Android App development
Android is a complete software package that comprises not only of the OS but also middleware and applications. It is a very powerful OS that supports a large number of discreet applications that make life simpler for the smartphone or tablet user. Android OS architecture comprises of a myriad of libraries, providers and drivers that provide a tonne of functionalities and features that can be leveraged for developing excellent applications. The core of the architecture is the Android Runtime.
The android runtime component of the architecture mainly contains the core java libraries and also the Dalvik Virtual machine. Core Java libraries provide a vast number of ready-to-use functionalities that developers can utilize in their applications. Dalvik virtual machine is the core runtime environment where the application actually runs. It is a kind of JVM that runs the .dex files as opposed to the regular .class files and is optimized to consume less memory and low processing power.
Until Android 4.3, Dalvik virtual machine used a technique similar to JIT (Just-In-Time) process wherein the code of the app is converted to an executable program every time the app starts. Android 4.4 has included a new runtime environment named Android Runtime (ART) that replaces the existing Dalvik Virtual machine. ART uses the ‘Ahead-Of-Time’ (AOT) process wherein the app code is converted to executable program when the app is installed. This improves performance of the application tremendously.
The architecture of Android OS is as shown above.
Step 1: Prerequisites for Starting App Development in Android:
Java JDK installed on the system: Java JDK needs to be installed in the system in which you are going to develop the Android application. The reason is that Android development is done majorly in Java Programming language. Other languages like C++, HTML5, etc. can also be used but Java is the most preferred and widely used language.
We can download JDK from Oracle site. Check the image below and click it to go to the download page.Another prerequisite is that the developer should be aware of Java programming language syntax.
Installing all the required software bundles
Direct ADT bundle install: To avoid doing all the above stuff, you can also directly download the Android Developer Tools (ADT) bundle from the google site. This bundle includes all the required android SDK software and also a version of Eclipse that has the ADT plugin already installed so that developers can directly start working on the app development. Click below image to download ADT.
Steps to be followed are: a.Extract the .zip file and open the folder eclipse folder in it. b.Double-click the “eclipse.exe” to launch eclipse IDE c.Select the workspace (preferrably the user directory in windows
Step 2: Creating a Very Basic Loan Interest Calculator Application Project
Now we are ready to create our first Android application using the Eclipse IDE. Our “Loan Interest Calculator” application takes the loan amount and the tenure of the loan as inputs and based on the value of loan amount, assigns a particular interest rate, and calculates the total interest paid by the end of the tenure. SO basically, it just performs a simple interest calculation given the loan amount and tenure. Follow the below mentioned steps to create the Android application project.
- Go to File->New->Android Application project
- Give a proper name to your application. Make sure that the package name has two strings separated by a period (.) and that there are no spaces in the “Project Name” field.
- Select the appropriate minimum required SDK and target SDK for the application. Click next
- In the next screen, make sure the “create activity” check box is checked. Click Next.
- An “Activity” is a single particular thing that the user can do and/or interact with. Generally activities are presented to the user as full-screen windows but can also be presented in other ways. In technical terms, an activity is the presentation layer/screen of the app
- Select Icon and other icon attributes. Click next.
- For the current app, let’s just select a Blank Activity. Click Next.
- In the next screen, type the activity name. For the purpose of this app, let’s just leave it to default “MainActivity”. Click next.
Congratulations! You have just created a very basic but complete Android Hello World Application!
Step 3: Components of the Project
Every Android application project has certain specific and necessary components that can be seen in the project workspace. The entire project workspace hierarchy can be viewed in the “Package Explorer” pane in the left of the editor. Package explorer pane in our project can be seen as shown in the image above.
The important components to be taken note of are highlighted in the image above. They are:
- ‘res’ folder
- ‘drawable-xxxx’ folders
- Layout folder
- ‘values’ folder
- AndroidManifest.xml file.
Each of the above components is explained below.
This is the file that contains the java code of our main activity (in our case, it is “MainActivity” class). Each activity extends the class “Activity” defined in the “android.app” package which has to be imported in the file using the “import” statement. This is done explicitly by Eclipse when we first create our project. Initially, the MainActivity.java file looks like this (image attached).
‘res’ stands for “Resources”. Basically, this folder contains all the resources (images/text strings/constants/etc.) that are required or that are being used in our application.
These folders are nothing but folders for storing images of different resolutions. For example, if your app is designed for devices with resolutions ranging from low to Full HD resolution, then while developing the application, you will have to save the same image with the same name but varying resolutions into the respective folders separately. That is, the image with low resolution goes in to the ‘drawable-ldpi’ folder, one with a little higher resolution goes in to ‘drawable-mdpi’ folder and those with higher resolutions go into drawable-hdpi, drawable-xhdpi, drawable-xxhdpi folders respectively depending on their resolutions.
When the app is started, the engine automatically recognizes the device resolution and displays the corresponding image to the user.
The layout folder contains the layout definitions of all the activities in our application. So first, what exactly is a ‘Layout’??
A layout is used for defining the UI structure of the activity (Remember, ‘activity’ is the actual UI that the user sees when he/she runs the application). Each activity has its own layout file that defines all the UI elements that the particular activity holds.The layout file defines all the UI elements in XML. We can either declare all the UI elements in XML or instantiate the layout elements at runtime or do both. The advantage of declaring layout elements in XML is that we can modify the layout as per our requirements without touching or recompiling the actual source code.
The layout xml file is interpreted by eclipse to display a visual or graphical representation of how the layout looks like when deployed in a device. Using the graphical representation, developers can very easily drag and drop UI elements on to the layout screen and the corresponding xml for the elements is automatically written in the file.
The ‘values’ contains definitions of all the constants (string constants, literal constants, value constants, etc.) that are being currently used in the application. For example, the ‘dimens.xml’ file can be used to save constant dimension values for elements in the application and these constants can be referenced in the layout file to apply the specified dimension to the element. Similarly, strings/xml can be used to store string constants that are used in the application. The live usage of these files will be shown later in the article.
The Manifest file declares and defines pretty much everything that our application consists of or makes use of. It is also an xml file that contains the declaration of all the activities present in the application, all the resources (if any) that are being used in the application, which activity is the launcher activity, what all permissions are required by the application to run correctly.
Remember the little pop up that shows up when you try to install an App from Google Play Store; The one that requires you to accept the permissions requested by the app? That is all mentioned here in the manifest file.
Step 4: Creating Layout Elements for Our Application
Any UI element that needs to be shown on the UI has to be included in the layout xml file for that corresponding activity. Our application has only 1 single activity that includes 3 TextView elements (static labels), 2 EditText elements (text boxes) and one button for calculating the interest amount.
So, to include the above elements, write the following code in the “activity_main.xml” file within the xml tags.
The above 6 lines of xml cause the layout to add a TextView (Label) element to the UI. Observe each line of code carefully:
- Line-1 begins the TextView element
- Line-2 defines a unique identifier for that particular TextView element.
- Line-3 and line-4 specify the width and height for the element. The value is “wrap_content” which basically means that the content should wrap based on the width and height of the device.
- Line-5 is important. This specifies what text has to be shown on the UI for this label. For that, it specifies a string resource named “hello_world” that is defined in the strings.xml file with the tag “string”. The strings.xml file is as shown below. It contains the definitions of all the string constants that are used in the application.
- The compiler takes the string resource from this file and replaces it in the line 5 of the previous TextView layout xml.
- Line-6 also represents a similar syntax wherein the textSize property of the TextView element is set using a resource that is stored in the “dimens.xml” file within the “dimen” tag. Please see dimens.xml file below.
In a similar way, we also define 2 other Textviews. Then we define EditText elements (text boxes) to take the input from the user. For defining an EditText element, the xml mark-up is as shown below.
- Line-10 specifies that the input type is number that accepts decimals. This helps to show the numeric keyboard as opposed to the regular QWERTY keyboard when the user touches inside the textbox.
Similarly, we have to add another EditText element for taking the loan tenure. Next, we have to declare a submit button for calculating the interest. For that, drag and drop a button on to the layout from the graphical designer. The xml mark-up is automatically generated and is as shown below. (Make changes to the android:text so that it reads from the string resources)
Step 5: Adding Logic to Calculate Interest and Display to the User
The last thing left to do is to add logic in our code to perform the simple interest calculations based on the loan amount entered and the loan tenure entered by the user. Since we need to perform this logic on the click event of the button, we firstly need to add an event handler that will listen to the click event of the button.
The complete definition of the listener is shown below. This includes the logic to calculate the interest and also display it to the user on to the UI.
Step 6: Running the Application
To run our newly created application, we can either run it in the Emulator that is included with the ADT bundle mentioned earlier, or you can use your own Android Device.
Note: To use your own android device, please follow instructions given here.
For now, let us just run it in the emulator. But before running the app, we first need to create an Android Virtual Device (AVD) using the Android SDK Manager provided with the ADT Bundle. Check the screen shot below to locate Android SDK Manager in Eclipse.
Here, we first need to create a virtual device. Follow the below steps to create one.
- Click on Android SDK Manager in Eclipse
- Click on the “New” button to create a new AVD
- Configure the AVD appropriately as specified in the image given below.
- Click Ok. This creates a new virtual device for testing/debugging. Next, click on “start”
- Depending on your PC’s configuration, the Emulator can take up to 15-20 minutes to load and start completely. Once loaded, the emulator looks like this.
- Now to run your application, go to Run ->Run as -> Android Application. This would load your application in the Emulator and the application is started as shown below. The above is the first screen.
Step 7: Activity Lifecycle
As mentioned earlier, an ‘Activity’ is the screen that the user sees when he/she loads the application. A user can move from one activity to another over the course of usage of the application. And thus, each activity goes through many different states throughout its lifetime. The following are the activity lifecycle states.
- Active or Running
Step 8: Android Manifest
As mentioned earlier, the AndroidManifest.xml file contains the declaration of all the elements/activities/permissions that the current application defines and uses. The AndroidManifest.xml file for our current project is as shown above.
As we can see, the file defines the following:
- version code and version name of the application
- Minimum SDK version and target SDK version. These are actually the API levels
- The icon, theme and label of the application
- The activities that are being used in the application. Currently only one activity is being used here. Other activities are shown in separate tags. Within this, the activity that is the “Launcher” activity (the one that shows up when the app first starts) is defined accordingly using the tag.
This concludes the tutorial on “How to create an Android Application”. Hope you found this useful. You can add many more functionalities to the existing app like putting validations, different styling to the elements, etc. to make the application more intuitive and responsive.
2 People Made This Project!
We have a be nice policy.
Please be positive and constructive.