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 .

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>

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
                    
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
 
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 .
android:layout_width="match_parent"
android:layout_height="match_parent"
The above two attributes state that the width and height of the layout matches the screen width and height respectively .
tools:context=".MainActivity"
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 .
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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 : 
MainActivity.java
package com.example.myfirstproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    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 .
r.java
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 .
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstproject"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
The first 5 lines are self explanatory .
The lines after that 
<uses-sdk 
android:minSdkVersion="8"
              
     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
<intent-filter>
<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

No comments:

Post a Comment

Top