Android: Creating ‘HelloWorld’ application in Android with Eclipse Indigo

Introduction:

Another ‘HelloWorld’ application ???

Yes, the name ‘HelloWorld’ is chosen because it is one of the most searched word when a fresher starts doing a program with a language/platform which is new to him/her. I didn’t really code a great logic for this application except a very few lines of code- just to show some text(lets say: ‘HelloWorld’) on the screen.

Prerequisites:

As I am going to develop an android application so, I need android SDK and Eclipse to be already installed on my system. For a quick installation guide you may read my post on Installing Android SDK on windows: Complete steps.

Target Readers:

I have made this post specially to code on Eclipse Indigo as I assume, the readers of this post are freshers in Android apps development. I am going to explain it right from the New Project dialogue box to   the message on Emulator Screen and I have Eclipse Indigo installed on my system as well. With a different version of Eclipse the code will be same but, the instruction may vary a little bit.

What to Do?

  1. Open Eclipse
  2. Open the Emulator
    [if you don’t have created an AVD then create one using below few steps otherwise you can skip till Step 3]
    *Go to Window menu and click on AVD Manager, which will open Android Virtual Device Manager in  a new window.
    *Click on New button
    *A new window will be opened and will ask for Name.
    *Type a name there.[Ex: MyDevice]
    *Then choose target android version from dropdown Target. [Ex: Android 2.2 -API Level 8]
    *Now click on Create AVD
    *OK, your AVD is now ready to run which you can see in the AVD list on the AVD Manager window.
    *Now, choose the device you just have created and then click on Start button.
  3. Go to File –>New
  4. Click on Project.
  5. Select Android Project from Android directory/folder.
  6. Click Next button.
  7. Type your project name. [Ex: HelloWorld]
  8. Click Next.
  9. Select a build target. [Ex: Android 2.2]
  10. Click Next.
  11. Type a valid Package Name. [Ex: com.demo.mydemo]
  12. Make sure that Create Activity checkbox is checked.
  13. Click Finish.
  14. Now the project can be seen, listed on the Navigator.
  15. It contains some pre-generated files and folders.
  16. Open the Activity file located under src folder .[Ex: src –> com –> demo –> mydemo –>]
  17. Delete the entire contents from it.
  18. Now, Paste following code-
    
    package com.demo.mydemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    public class HelloWorldActivity extends Activity {
     TextView tv;
     @Override
     public void onCreate(Bundle bndl) {
     super.onCreate(bndl);
     tv=new TextView(this);
     setContentView(tv);
     tv.setText("This is a simple HelloWorld android application !");
     }
    
    }
    

    [Note: Change the package name if you  have typed a different name earlier while creating project and similarly change the activity name if you have defined with a different name ]

  19. Run the project from menu Run –> Run
    or, right click on the project on Navigator and then click on Run As –> Android Application
  20. Now you can see the result on the emulator screen.

That’s it !!!!

Analysis :
Here package is the one which you have defined earlier while creating the project. Then like Java, you have to import all the classes you reference. The onCreate() method is invoked when the activity starts. To initiate the activity we have to call super.onCreate(). You  need to call super.OnCreate(), every time when overriding the onCreate() method of an Activity. It should be the very first line in the overriding method. Then I have created an instance of TextView, where I am supposed to put some random text or, some message or, “HelloWorld” etc. Here setContentView() sets the TextView, which we have created earlier to the screen so that it can viewed by the user. Then I have assigned some text (i.e,This is a simple HelloWorld android application !”) to the TextView…Yes, I know- I am deviating from the Introduction of the article where I said that it will show ‘HelloWorld’ but, these text doesn’t matters really, what I wanted to show is a custom message or some random text on the emulator screen.  That’s all.

Conclusion :

Now, we are able to create a very simple application in android. I will post some more articles on Android Apps Development in my successive posts.. So, tuned.. In case any query you can comment here.

 

If you want install a .apk file in your emulator then you may read my post here for a easy solution.

https://suvendugiri.wordpress.com/2012/01/24/android-installing-dotapk-file-on-emulator-in-windows/

15 thoughts on “Android: Creating ‘HelloWorld’ application in Android with Eclipse Indigo

  1. Pingback: Android : Simple example of Intent « Suvendu's Blog

  2. Pingback: Android : Using Button & Click-Event with example « Suvendu's Blog

  3. Pingback: Android : Using CheckBox with Example « Suvendu's Blog

  4. does not work … only a get a screen with the image of a startup cellphone view … unlock/sound-on button appears including time, date, and charging(50%). the ‘This is a simple HelloWorld android application !’ in the app never shows up.

    btw: … i used the 2.3.3 platform to match my cellphone for my AVD.

    thx

  5. Pingback: creating android apps with eclipse | androidtrick

  6. Pingback: Android: Creating ‘HelloWorld’ application in Android with Eclipse  | Acordo Coletivo (Petroleiros, Bancários, Prof de Saúde)

  7. Hiii this is Ramesh

    I want query for updating the column of tables dynamically using stored procedure in sql server

Leave a reply to playtyping Cancel reply