Monthly Archives: July 2011

Creating a basic Android app

I am exploring Android application development for some new projects so thought about sharing my notes here. So far it is turning out to be fun and very simple :).

I am using eclipse for development so first of all we will need to install ADT. Follow the steps mentioned here

http://developer.android.com/sdk/eclipse-adt.html

Once we have ADT installed, creating first android project is pretty simple. Create a new android project (right click->new project->Android project).

In the project creation window, provide details-

Provide name say test_project, Choose target say 2.1, package- com.kamalmeet.android.test and activity- test. Skip test project creation and click Finish

Now we will run this project on emulator. Just go to your project and run as ‘android application’, this will give a “Hello World Test” message.

Now let us see what happened when we created and executed the project. When we created a a new project it created some files. Here are the important ones to get started

We will see AndroidManifest.xml in base project directory. This is the root file of our project. This contains details of the activities, services etc for our project. For now you are seeing only one activity under application node in this xml file. Also note the package declaration in manifest, this is the base package of our project.

Now we will check our package under src/ folder. Lets take a quick look into com.kamalmeet.android.test. We see the activity java file test.java. It extends the  parent android.app.Activity class, and has one method it overrides, that is onCreate. No points on guessing that this is the first method getting called when activity loads. For now it has just one line setContentView(R.layout.main);

To understand meaning of this line, lets move to gen/ folder, which has the same package and a autogenerated R.java file. This file gets automatically generated on compilation. It provides a unique Id to all our visible contents. For example, we can see an inner class as layout which has an int main value.

What does this main value corresponds to? Checkout res/ folder. This has all out UI related stuff like images, view XMLS etc. under res we have res/layout/ folder. This is where we have our UI XML, in this case main.xml. As you can see android supports XML defined UI like just Flex, XAML etc. In addition we can define

You can see one node as LinearLayout and a child node of TextView. This is responsible for what we see on UI. We can see test being set as android:text=”@string/hello”. It picks up value from /res/values/strings.xml. In this XML you can see a string node where name is set as hello with value as “Hello World, test!” Change it to any value say “My first Android app” and re-run the application. Simple. Now we can go ahead and play around with this simple UI app.

Zinda ho tum?

Dilon mein tum apni betabiyan leke chal rahe ho.
Toh zinda ho tum!
Nazar mein khwaabon ki bijliyan leke chal rahe ho
Toh zinda ho tum!

Hawa ke jhonkon ke jaise aazad rehna seekho
Tum ek dariya ke jaise, leharon mein behna seekho
Har ek lamhe se tum milo khole apni baahein
Har ek pal ek naya samaa dekhe ye nigahein

Jo apni aankhon mein hairaniyan leke chal rahe ho
Toh zinda ho tum!
Dilon mein tum apni betabiyan leke chal rahe ho
Toh zinda ho tum!

-Javed Akhtar

Insertion Sorting

Insertion sorting, as the name suggest, is about inserting the elements of an unsorted array to their correct position one by one. To make it clearer, lets say we have an unsorted array. We take another array, which is empty initially and then we will keep adding numbers one by one into this array at their correct positions.

Say the original array A is

5, 3, 7, 2, 6

Now we take first element and add to zeroth position of a new array B, so B is now containing 5

next we pick 3 from original array and add to first position of B, it becomes 3, 5

After step 3 B is, 3, 5, 7 and then 2, 3, 5, 7 and finally 2, 3, 5, 6, 7

We have taken 2 arrays just to make things clear, the sorting functionality can be achieved with 1 array only. Instead of taking an additional array, we will think of original array as 2 sub-parts, at any given time first N-x elements are sorted and remaining x are not sorted.

Again taking the original array A

5, 3, 7, 2, 6

Step 1: 5 (sorted till here), 3, 7, 2, 6
Step 2: 3, 5 (sorted till here), 7, 2, 6
Step 3: 3, 5, 7 (S), 2, 6
Step 4: 2, 3, 5, 7, (S) 6
Step 5: 2, 3, 5, 6, 7 – Final

Complexity:

If there are N elements in array, at Nth point, we know we have N-x sorted and x unsorted. We pick up next element (N-x+1)st, and need to insert it into proper position in for N-x+1 elements. So in worst case we need to make N-x comparisons (in best case 1 comparison). The process has to be repeated N times for N elements.

So in worst case we have comparisons like

1+2+3+.. +N= N*(N-1)/2 which is quadratic i.e. N*N.

In best case (sorted array) we have 1+1+1+.. N times comparison which is order of N (linear).

We generally take worst case complexity so we will say Insertion sort has complexity of N^2.