“Hello World” Android Application Source Code Description

Hello World Source Code Description

At last, Now it is time to start coding!  You need to know details about source code description for your “Hello World” android application. We will learn about auto eclipse generated code for your application. Go to the “src” folder. In this folder, you will find the package folder, open the HelloWorld.java file. You will see the default code for an Android Activity:

As you can see, we create a new activity by extending the default Android activity class. Then we override the default onCreate function, which is called when the project is created. In there, we load our own layout from the resources and also call the onCreate function of the base class. Now let’s take a closer look at the layout file. You find it in the layout folder under resources e.g res\layout\main.xml. When you open it, it should look like this:

  •  RelativeLayout:     The positions of the children are specified in relation to the other children.
  •  TableLayout:           The child elements are placed with a grid.
  •  AbsoluteLayout:    The child elements are positioned based on absolute coordinates (in pixel).

Once you have chosen a layout type, you can add child elements. In the code given, there is already a TextView, which is used to display text on the screen. The current content is a reference to a resource defined in the values.xml file. As you will see, it uses the whole width of the screen, but is only as long as it needs to, to display the content. We might start with some small changes. Let’s change the text color of the TextView to green:

Now, launch the project and see the changes. Next, let’s add a new control called EditText:

When we want to access the controls in code, they need to have an ID. Next we create some code for the controls. Go to the HelloworldApp.java file.

We will analyze the code line by line. First of all, as before, we load the layout. Then we create a TextView and a EditText variable and load our interface objects in them (that’s what we need the ID for). Finally, we add a new OnKeyListener to the EditText control. In this OnKeyListener, we create the method onKey, which is called when a key is pressed, when the control is active. In the method, we perform two checks: the first to be sure that a key is pressed down (and not released), and the second to specify the key (in this case, the center key of the D-pad). If both checks are passed, we add the text of the EditText control to the TextView, and finally the text of the EditText control is deleted. Run and test the application. Great, you created your first real Android app.

 

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = ten

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">