10 June 2014

Understanding Hello World in Android ......

You may have made the first Android application with a text "Hello World" in the center of the screen by following the steps in the previous post .

 Now lets understand the logic behind building it .
After your application has loaded you may have seen the page shown below .
activity main

The XML Activity
Click on the tab activity_main.xml circled in the above diagram . You will see an xml code like this .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity" >

<TextView android:layout_width="wrap_content"
android:text="@string/hello_world" />

What is a Relative layout ? Layout is used to place the elements like TextView , Button , CheckBox etc.  (here TextView) in a proper fashion on the screen . Relative Layout allows you to place elements according to position of another element or screen borders .
The Text "Hello World" here is set up in the center of the screen
Are you able to see the above two attributes in the code :
  • The layout_centerHorizontal="true" the TextView is placed at the horizontal center of the layout (the page here) . Hello world is placed at distance of width/2 from the top-left corner .
  • The layout_centerVertical="true" the TextView is placed at the vertical center of the layout (the page here) . Hello world is placed at distance of  height/2 from the top-left corner .
The above two attributes state that the width and height of the layout matches the screen width and height respectively .
The tools:context refers to name of the java class we are talking in context with .  
What is a TextView ? 
TextView is a box which we use to display a text that is concurently displayed on the screen .
layout property
 The above two attributes in the TextView field says that width and height of the TextView will automatically gain the width and height of the String typed in it .
The Java class 
The java class is found in the src directory .
java class
The name of the java class is MainActivity.java (by default) .It includes : 
package com.example.myfirstproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

  1. At the top the java file includes the name of the package in which it is stored i.e. com -> example -> MyFirstProject .
  2. The list of imported classes, packages etc.
  3. The MainActivity class
  4. The onCreate() method .
  5. The onCreateOptionsMenu is of no use here (there will be no effect on the output even if you remove it) .
Bundle : It is a class which stores objects in a map fashion . Every Object is associated with a unique-id 
(key) . Bundle class is used to retrieve objects in Android since it is faster then the conventional fashion to retrieve objects from the memory .

Activity : It gives you the right to command your application by user inputs like what happens on pressing a button ? what happens on scrolling the screen ? etc .
More to learn about Activity class here .
onCreate() : It is the first method called when the application starts executing . It takes a instance on class Bundle as an argument .

setContentView() : The xml file you have made early has a unique id assiciated with it . To view the xml file or the "Hello World" string on the screen (the layout) the id is called the the DVM.
         The id related to the layout is stored in R.java file .
The R.java file is stored in /gen shown in the below screenshot .
The AndroidManifest file :
It is just an index of various classes used in the application .
To view the contents of AndroidManifest file click AndroidManifest.xml and then click Androidmanifest.xml the the tabs below as shown in the figure .
android manifest
The contents of AndroidManifest.xml file (by default) are shown below .
<?xml version="1.0" encoding="utf-8"?>
android:versionName="1.0" >

android:targetSdkVersion="16" />

android:theme="@style/AppTheme" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

The first 5 lines are self explanatory .
The lines after that 
     android:targetSdkVersion="16" />
Displays the minimum and maximum API level for our project . We can change it to fit our application .
The <activity> tag displays the name of the java class to be called at the time of execution
<action android:name="android.intent.action.MAIN"
The intent-filter composes the Intents to which the specified class will respond to .
The intent is used to send a message to a specific component to request an action .
The action is used to perform the action (launching the mainActivity at MAIN (first position) .
The MAIN element specifies that the class is going to be launched first .
<category android:name="android.intent.category.LAUNCHER" />
category wraps the intents in a group containing similar intents . Here the category name is LAUNCHER which is predefined category for a launching intent .

Stay Tuned with Made In Android

Previous Page Next Page Home