Hello World!! Android :)

Tarun Mall         No comments

Hi friends,

This is my second post on android.
The most basic code that we write while learning a new technology is always “Hello World”.
I am no one to break that convention, lets write Hello World :)

Before moving to interesting code part, there is some pretty boring theory part that you must understand before.

There are mainly four type of application component:

1. Activity:
An activity represents a single screen with user interface.

2. Services:
A service is a component that runs in background to perform long-running operations.

3. Content Providers:
A content provider manages a shared set of application data. You can store your data in file system or Database or on web, and you can access or modify them through content provider.

4. Broadcast Receivers:
BR is a component that responds to system wide announcements.

I copied these lines from the tutorials on Android Developers Site. If you want to know more about these components then you can always refer to those tutorials.

So, back to Hello World:

Our Aim:
To write an application/activity that say “Hello World”

How To:
We will write an activity and then add a TextView to it which will contain the text Hello World.

Where to Test:
Android Virtual Device.

To start a AVD we have to create an AVD first. Open AVD Manager
(Window>>AVD Manager)




Click on New


Enter the Name for the Virtual Device and Target (this will depend on which android version you installed)



Now you can see the device in the list, click on Start and then Launch.


Your Andriod Virtual Device will start


Now, there are two ways to write 'Hello World' one is create purely JAVA code and other is JAVA + XML. First method is highly discouraged as is less flexible, difficult to maintain and can lead to seriously mumbo jumbo kind of code in complex activities :)

We'll look at the first method first (as I believe unless you don't know what is bad, you'll never appreciate the good :) )

I believe that you setup the android development environment. If not than you can refer to my previous post of SETTING UP ANDROID DEVELOPMENT ENVIRONMENT.

Now first start a new Android Project.
File>>New>>Android Project


If you are not able to find the 'Android Project' under 'New' then you can go to
File>>New>>Others
then you can find 'Android Project' in there.


Now enter a name for the project.


Select a Target for the project


Enter the name of the package and click on finish


Eclipse will generate one default activity for you under the name of the package provided by you.
Now when an activity will start the first method which will be called is 'onCreate()', so we will simply override this method.

Now most of the user interface components directly descend from View class, we will use TextView component. So what we have to do is create a new TextView object, pass object of the current activity as context (all activities are context) and then we'll set this view to the activity :)

Check the following code:


The above was the code way of saying hello world :)
Right Click on the project name and
'Run as>>Android Application'
(wait for sometime as android virtual environment take time to start)

Now lets take a look at the more flexible way, in this way we will define the look and feel in a xml which will be under the folder
res>>layout.

First create an xml by name 'helloworld_layout.xml' (you can use any name)



You have to select the proper resource type, check into above pic.

Now you have to open the xml and switch to Graphical Layout view from lower left corner.


Now drag and drop TextView and edit its content by change the value of the Text field from the Properties panel.

Now the difficult part is over :)
Simply go to your class and in onCreate() method remove all lines and write

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld_layout);
}

here R is the class that is automatically generated when the application compiles and is used to access resources :)

We are done.
Right Click on the project name and 'Run as>>Android Application' (wait for sometime as android virtual environment take time to start)

If you are facing any problem please comment, I am glad to help :)

Published by Tarun Mall

A blogger by passion.You can find me tucked in my bed and blogging on weekends when not roaming around. Besides blogging, I love music and you can find my songs on my fb page:PraveenUnplugged
.
Follow on Youtube : Videos On Latest Happenings |ThingsToKnow
.
Follow us Talend In Action

0 responses:

© 2015 Techie's House. Designed by Bloggertheme9