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?
- Open Eclipse
- 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. - Go to File –>New
- Click on Project.
- Select Android Project from Android directory/folder.
- Click Next button.
- Type your project name. [Ex: HelloWorld]
- Click Next.
- Select a build target. [Ex: Android 2.2]
- Click Next.
- Type a valid Package Name. [Ex: com.demo.mydemo]
- Make sure that Create Activity checkbox is checked.
- Click Finish.
- Now the project can be seen, listed on the Navigator.
- It contains some pre-generated files and folders.
- Open the Activity file located under src folder .[Ex: src –> com –> demo –> mydemo –>]
- Delete the entire contents from it.
- 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 ]
- Run the project from menu Run –> Run
or, right click on the project on Navigator and then click on Run As –> Android Application - 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/
Pingback: Android : Simple example of Intent « Suvendu's Blog
Pingback: Android : Using Button & Click-Event with example « Suvendu's Blog
Pingback: Android : Using CheckBox with Example « Suvendu's Blog
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
Reblogged this on itech and commented:
Create your first android applcation
Nice tutorial! Bookmarked it if I decide to get into android someday 🙂
Thanks for those beautiful words of compliments 🙂
very nice tutorial you can also check this one
http://www.pavanhd.blogspot.in/2013/05/how-to-create-android-virtual-device.html
Thanks for the clean explanation.. This website http://www.compiletimeerror.com/2012/12/creating-first-androidapplication-in.html also addresses something similar.. Have a look, may help..
really helpful
Pavan @(pavanh)
Hi very nice tutorial , very detailed and explanatory and helpful for beginners
nice
Pingback: creating android apps with eclipse | androidtrick
Pingback: Android: Creating ‘HelloWorld’ application in Android with Eclipse | Acordo Coletivo (Petroleiros, Bancários, Prof de Saúde)
Hiii this is Ramesh
I want query for updating the column of tables dynamically using stored procedure in sql server