Tuesday, 17 February 2015

Beginners Guide: Making the first Android Application Project

Every time I show my newly developed Android application in my college, the first question my friends ask and demand for an answer is 'how exactly should we make an Android application?' and 'where should we start from?'. So for the people having the same curiosity and interest for Android Apps, I have decided to spend some time writing a short tutorial guiding every one of them to become future Android developers. Before reading this, please note that I assume everyone who visited this page to create their first Android app, have a minimum knowledge about Java SE, the main programming language you will be dealing with, and XML to create the User Interface for you application.

Requirements to Make an Android Application:

  • Java JDK (Java Development Kit), the versions change from time to time, download it from here.
  • Android ADT Bundle, which includes Android SDK(manager) and Eclipse (the console where you will be creating apps). Download it from here.
Though Google recommends Android Studio, most of the developers use Eclipse. Download the above softwares and install them. Android ADT Bundle doesn't require any installation. Just download the zip file and extract it. When you are done, let's proceed.

Note: You can now develop Android apps on-the-go on your Android phone or tablet using the app AIDE. It works same as Eclipse. Apps developed using AIDE can be exported to Eclipse. Thanks a lot for the developers - original thread here.

Getting things ready:

When you open Eclipse for the first time you will be asked to select the location to your workspace, where all your developed apps will be stored in the future. Just create a folder in your desired location and link to it.

Creating the Android App:

So because this is your first Android application, all your app does is simply display a text on the center on screen.

Before making the app, you will need to know some terms related to Android development.

  • Activity - Activity is one what you see on your Android device's screen when the app is running. It is your app's user interface which includes text widgets, button widgets and more.
  • Class - Every Activity(XML file) will have a Java .class file running behind it.
  • Android Manifest - 'AndroidManifest.xml' is the heart of the application. It includes all the Java .class files used for running the application, app theme, and the sdk level used to create the app.

If you are getting confused, just read and ignore them. You will get to know them later by yourself. All you need to remember is "User Interface is managed by .xml files and programming is managed by .class files".

When you are ready with Eclipse, follow the below steps to create your first application.
  1. Click File --> New --> Android Application Project.
  2. Enter the Application name as anything you like. I'm entering MyFirstApplication.
  3. Click Next three times and now select 'Blank Activity' (We'll talk about this later).
  4. Leave the Activity name as 'MainActivity' and click finish. This creates two files. A .Class file named 'MainAcitivity.java' and a .Xml file named 'activity_main.xml'
The activity_main.xml file has the code that makes the UI of your Android application. The Xml file is referred as layout file, which by default is located at res/layout/xml file here.

First of all, remove the code in activity_main.xml and paste the below code.
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:text="Hello world" />  
Every layout file will have a type of layout which surrounds all the widgets. LinearLayout is what all the beginners start with. The next thing you need to know is layout_width and layout_height. These properties specify the dimensions of layout. The Orientation property specifies the order of widgets present inside the LinearLayout. The Gravity property specifies the location of child widgets inside the layout.

Inside the LinearLayout we have a 'TextView' widget that simply displays the desired text on the screen. The 'text' property specifies the text that needs to be displayed.

We are done with UI, now let's look at code which handles the xml file. Remove the code inside the MainActivity and paste the below code.     
 package com.example.myfirstapplication;  
 import android.app.Activity;  
 public class MainActivity extends Activity {  
   protected void onCreate(Bundle savedInstanceState) {  
The above code has a method onCreate, which runs the first time when the activity is created. You need not know about the arguments now. Next the UI required for this code is set using the method setContentView(R.layout.layout_name); This code is enough for your app to display the required text on the screen. Now its time to test your code.

Your app can be tested in Android virtual device but I recommend connecting a real device to pc and debug the app. To test your app, you need to enable 'USB Debugging' on your device. Usually this option is found in 'Developer Settings'. Enable it and connect your device using USB cable.

Now click 'Run --> Run' or press 'Ctrl + F11', you device should be automatically detected and listed. Your app will be automatically installed and opened in your device.

Congrats! you have made your first Android application. Go play with the code and see the changes in the application. These are basic steps to make the Android application. There are lot of topics to know in Android development. All the information you need can be found at developer.android.com and your doubts can be clarified at Stackoverflow.com.