Friday, January 24, 2014

Android app development - Hello world!

We saw about how to install the Android ADT in my previous post. Now we'll see how to develop a basic android app.

Open the "exclipse.exe" application from the "eclipse" folder of the installation directory. Don't get bogged down by the complexity of the layout!
We will now open a new project.
File - New - Android Application Project.

In the New Android Application window that appears, we will give a name to the application. (App names preferably start with a capital letter). The fields "Minimum Required SDK", "Target SDK", "Compile With" and "Theme" are pretty self explanatory from their names. I presume you should be aware of the fact that the version for which you are developing an app should be selected in the "Target SDK" and "Compile With" drop down list boxes. Keep clicking next and hit finish.

Now, if you are witnessing the coding behind android apps for the first time, brace yourself! Even I never expected "Java" to be here! I'm mostly from an electrical and electronics background. Though I had some coding classes at college, that was just at novice level. But, I could manage to grasp some basic concepts and believe me, these basics are more than enough for a hobbyist programmer.

On the left, there is the project explorer pane. As of now, we need to understand just a few areas we are going to meddle with. I've highlighted them for you.


"MainActivity.java" is like the main function of a c program. It is where all the function blocks are brought together and are given their functionality. The "activity_main.xml" file under the "layout" area of the "res" folder is where the various elements (like text input, number input, buttons, etc.) are defined and arranged. This will decide how the layout will look when the app runs on any android platform. Next is the "strings.xml" file under the "values" area of the folder. This is the file that holds the variables that are used in the app.

Open the "activity_main.xml" file. Voila! On the right pane, we can visualize how the app will look like when it is actually running. Don't be baffled. The default blank template comes with a "Hello world!" message already!

You may need to select the Graphical Layout tab to see this.


Now that we are done with the development, we need to run it, to see it live!
We have two options;
  • Simulate on a virtual android device.
  • Use a real android device.
We'll see how both work individually.

Running the app on a simulated device.

We have to first create a simulated device in order to run the app in it. Lets begin by clicking on the Window menu and selecting the Android Virtual Device Manager. Click New to add a new Android Virtual Device (AVD).

Give the AVD a name. I'll name it vijay. I've used Nexus 4 as the Device and "Android 4.2.2 - API Level 17" as the Target. All the remaining setting depend on the hardware configuration of your laptop or PC that is currently running the eclipse application. If you use host GPU, you'll have a faster emulation, at the cost of your laptop's graphic card! Additionally, you can select the snapshot option to optimize the speed. This will save the last state of the emulator while you close it, so that it will start quickly on your next run. (Obviously, the first run will be slower.) Close the Virtual Device Manager.

Select the "MainActivity.java" file from the Project Explorer by double clicking it. Click the run button on the tool bar or under the run menu (Ctrl + F11 is the shortcut). In the window that appears, select run as "Android Application" and click OK.

In the Android Device Chooser window, select the virtual device you just created and click OK. Now, wait patiently till the device boots up. This really tests the degree of patience in us, especially if the processor of your laptop is slow!

And when its done, its celebration time!


To maximize the emulator, use the shortcut Alt+Enter.
To change the orientation of the emulator, use Ctrl+F11.
To toggle network ON or OFF, use F8 key.


Running the app on a real device.

If you own a real android device, you could relish the fun of watching your first app perform in your own device. Firstly, you may need to enable "Developer options". It was openly available in previous versions of android. But from the version 4.2.2, they are hidden. To reveal this option, open "About device" from the settings menu and tap on the "Build number" several times. Now enable "USB debugging" under the Developer options. Connect the phone to your laptop. The USB driver will automatically be installed from the OEM site.

Now every time you click the run button in the eclipse application, you have an option to run the Android application on either the virtual device you created above or your phone.

Kudos! You've just experienced your first android app on your own phone!


Android app development - What lured me to it!

The invention of telephone was definitely one of the finest inventions of mankind! I'm quite fascinated on witnessing the development from the then wired, bulky telephones to the present wireless mobiles. The past decade has seen tremendous development in the way we interact with our fellow humans. I still remember the days when cordless phones were first used; I used to wonder how such a thing is even possible! And then there was this "pager", going viral among the business class. Within a few years, came the mobile phones. At that time, I didn't fathom that this technology would be the future of a revolution in the communication industry. Now I know exactly where we are heading to; at least in the present scenario dominated by the Android market!

Since the day I bought my first android device, I was always curious to know how things worked. I've always been raring to understand how android apps worked, but procrastinating, thinking that this was just for the computer geeks. But things proved wrong when I started experimenting. I'm writing this blog to share my experiences on how to develop apps for the android OS.

Lets get started straight away!

As of today's world, everything starts with a Google search! I too started off with those generic words, "how to create android apps".

The android development software is a freeware and can be downloaded from the following link. For the comfort of beginners, the download package consists of all prerequisites that are needed for the proper functioning of this software.

http://developer.android.com/sdk/index.html

After downloading the Android SDK from the above link, extract the contents of the file to the root folder. (i.e. C drive for most cases). You could extract them into a folder called "Development".
One word of caution is not to move or rename any of the folders. This may render certain internal links unusable and we may need to manually reestablish the links.

So finally, the installation path will be C:/Development/adt-bundle-windows-x86-20131030

Inside this "adt-bundle-windows-x86-20131030" folder, there are three folders (eclipse, sdk and workspace). We'll be developing apps using the "eclipse.exe" application in the "eclipse" folder. The workspace folder will contain the backups of the apps we develop. Additionally, there is one "SDK Manager.exe" application in the "adt-bundle-windows-x86-20131030" folder. This is used to install add-ons. For eg. the Android ADT will presently install only the  files required for running the apps in 4.4 (kitkat) version. If you want to run the app in a different platform, Jelly Bean say, you have to manually download the required files for version 4.2.

Now, give yourself a pat on the back for successfully installing the Android ADT. We'll see about displaying the generic "Hello world" message in our first android app in the next post!