Saturday, April 14, 2012

Hello word !! - Your First Android Project

Hello World is a traditional first program because it shows the very simple task of letting the user know you are there (or letting them know that you know that they are there).  It’s simple I/O to send a simple message.  I am going to give a very brief tour of some of the ways you can communicate with a user in Android in this post.  Half Dozen Hello Worlds will highlight some simple I/O and get you started writing your first Android programs.  First let’s go ahead and open Eclipse, where you have already setup Android, and create a new Android Project.  Go to File -> New -> Project.  Select Android Project and click next.

Fill out the information on the next screen for your project.


































You can put whatever you like for the project name, and application name.  Note that I selected Android SDK 1.1.  It is a good idea to select the oldest SDK which has all of the features your program needs, to increase compatibility across devices.  You can think of Create Activity as being similar to create Main Method.  The name of the Activity you place here is the class that will be called when Android tries to run your code.  Once you click Finish you should see the Project.






















If you expand the project, then expand the src folder, and then the package you will see a Java file. This file will be named whatever you called your Activity (so mine is Hello.java).  For simplicity I am going to assume you used the same settings I did when creating your project.  If you did not just substitute your Activity name for mine.  Double click on Hello.java and look at the code that Eclipse has already provided you.

package com.learnandroid.hellowworld;
 
import android.app.Activity;
import android.os.Bundle;
 
public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Just to give a quick overview.  The package declaration just places this class in the package we specified when we created the project.  The two import statements just import the minimum Android libraries needed for an Activity.  Since Eclipse created this as an Activity it is going to inherit from the Activity class, and we are overriding onCreate, which is called when an Activity is created.  We call onCreate of the superclass.  Finally, we are at the one line of code we really care about right now.
setContentView() tells android what to display to the user.  We are going to change this from the default so that our code looks like this:

package com.learnandroid.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView helloText = new TextView(this);
        helloText.setText("Hello World");
        setContentView(helloText);
    }
}
 
Let’s briefly look at exactly what we did before moving on.  TextView is an android widget that displays text.  You can think of it as a label, something the user can read but cannot edit.  We instantiated a new TextView.  Then we called the setText method to set the text of it to “Hello World”.
Next we used setContentView to tell Android that our TextView was what we wanted to display in the main part of our application.
Once your code looks like mine, go to the Run Menu and select Run…  In the Run As Popup select Android Application.  Your Android Emulator should launch.  The Emulator will normally start on a lock screen, but as soon as you unlock the device you should see your application launch.



 Congratulations!  You have built the first Hello World. 
 
 
 



















Friday, April 13, 2012

Setting up the android environment with eclips






Before starting anything first we need to setup environment. Follow the simple steps to install all the prerequisites for android development. 
Click here for minimum system requirement 
1. Download and install JDK 1.6 or latest . you can get it from here
2. Download and install Android SDK and AVD, you can get it from here
3. Download Eclips, you can get it from here
4. Get the Eclips plugin for android.

Lets look at each step in detail


1. Installing JDK 1.6

You can get latest JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Download it and install if you do not have latest SDK. 

2. Installing Android SDK

First, we need to download the Android SDK.  You can get it here (just select your operating system).  Untar or Unzip this and put it in a folder of your choice (note the folder location, we’ll need it later).  Ironically, the latest Android SDK does not actually come with an Android SDK.  This is a little bit confusing, but Google is trying to provide us with tools to keep up to date instead of just giving us the actual SDK.  What that means for right now, however, is that we have a little bit more work to do before we actually have the SDK.  Go into the unpacked Android SDK folder and you should see a folder named Tools.  Open that and run the “Android” you find in this folder.  This should bring up the Android SDK Manager.

 If you click on Available Packages there should be a repository listed (in mine it lists https://dl-ssl.google.com/android/repository/repository.xml).  Check the repository and click on refresh.  It should give you a list of available Google APIs and Android SDKs.
If you get an error about not being able to access the repository via HTTPS click on the Settings Menu on the left.  Put a checkmark next to Force https://… sources to be fetched using http://… and click Save and Apply.  Go back to Available Packages and click refresh again.  Put a checkmark next to the SDK you want to install, and click install selected.  The SDK version you want is going to be determined by what is installed on your Android device (or your consumer’s android devices).  If you don’t know, just get the latest for now.  You can always come back and get other versions.
After it finishes downloading you should see the SDK listed in Installed Packages.



2. Installing Eclipse

Eclipse is a free, open source IDE. Basically, an environment that will give you everything you need to get started programming in Java. Now you might be saying, “Wait a minute! I want to program in Android, not Java!” Your Android programs are going to be written in Java and run by a Virtual Machine on the Android device. Just think of Android as the framework and Java as the programming language you use to access the framework.
You can download Eclipse by going here and clicking on “Eclipse IDE for Java Developers” on the page.  I am not giving you the direct download link because Eclipse is multiplatform and clicking on that link will take you to the correct version of the software.  You might not want the 64 bit Ubuntu version of the software that the link provides me.  At the time of this writing, the current version of Eclipse is Galileo (version 3.5).
Now untar or unzip Eclipse and run it directly from the folder (there is no need to install anything).  It will ask you for a workbench location.



You can safely accept the default value and just click OK here.
At this point you will be at a welcome screen for Eclipse.  It has a link for an overview of Eclipse, and another for Tutorial on using Eclipse.  If you have never used Eclipse before it might be a good idea to check these out.

3. Get the Android Eclipse Plugin

To get setup for Android development, we are going to open the help menu and select “Install New Software”.  On this screen click Add.  In the window that pops up put in a name for your reference (I just used “Android SDK”) and the URL https://dl-ssl.google.com/android/eclipse/



When you click OK there should be an entry for “Developer Tools” in the list.  Click the checkbox next to this and click on Next.  You will see a screen listing what is going to be installed (Android DDMS and Android Development Tools).  Click Next again, read the license agreement.  If you accept the agreement and click finish then you should see the software install into your Eclipse environment.  Finally, you should get a message recommending that you restart Eclipse.  Tell it to restart.
After you restart click on the Window menu and select Preferences (Eclipse Menu and Preferences if you’re on a Mac).  Click on Android and you will probably get a message complaining that Eclipse doesn’t know where the SDK is located.  Click on browse next to SDK Location and browse to the folder where you saved the SDK earlier.  Click Apply and you should see Android and the Version of the SDK you selected under Target Name.  Click OK.
Now the final step in setting up your Plugin is making a Virtual Device.  Under the Window Menu you should see Android SDK and AVD Manager.  This is a shortcut to the same Android SDK Manager we accessed earlier by running the Android executable in the SDK folder.  Click on Virtual Devices and Click on the New button on the right.  Give this whatever name you like (I called mine DebugAVD since that is how I will be using it) and select your SDK from the Target drop down.  Click on Create AVD and you should get a confirmation message.  Your AVD should now be listed on this screen.
That’s it, you’re ready to go.  Go to File, New, Project and you will see an Android folder.  Expand the folder, click on Android Project.  Fill out the form here, click finished, and you’ll be ready to program.

 Thats it !!!!. Now you are ready to do a magic with Android.