Friday 8 June 2012

Descripition of your First Android program

Now I explain one by one first of all we see the architecture of file formed 

There many important file and directories are created.
AndroidManifest.xml : It is configuration file which is formed for Helloandroid application

Default.properties or project.properties: It is used by Eclipse and android. Hence please do not edit this file.

/src folder: It contain source code of Helloandroid

/gen/hell.abh.com/HelloandroidActivity.java: The name of Application which is created i.e. named as "HelloandroidActivity"


/gen/hell.abh.com/R.java: It is generated Resource file  do not edit this file.

 /assets folder:Here uncomplied file resource stored.

/res folder: it is place where application resource include such as graphic,animation ,layout file and row file.

/res/drawable:Application icon graphic resource which is default stored.

/res/layout/main.xml: It is layout file used for Helloandroid project.It is very important file.

/res/values/string.xml: It is path where string resources are defined. Here string is resources for android. actually it is value folder it is very important folder it define resouces such as
 arrays.xml-Xml file define the array
colors.xml-Xml file define color.It define background color for UI
dimens.xml-Xml file which define value of UI eg 40px,40px by R.dimens class,
string.xml-Xml file used to define text String to be used in your application
styles.xml-Xml file which is define the style of application

 

 I am going to explain the main.xml which lie under /res/layout/main.xml   
    The layout of the application can be determine by the xml.file or source code . main.xml is a file which determine the layout your application i.e. user interface before explaining each line of main.xml I want to tell what is view ?


        View:  Views are user interface as object such as button or TextField, Radiobutton etc. and ViewGroup is container which determine how the views are laid out such as Grid or vertical. you would notice one important thing by seeing the above chart it is clear that ViewGroup may consist of ViewGruop and View also but View can never consist ViewGroup.
      When you make any new android application. Two view is always formed LinearLayout and TextView . 

Line 1:<?xml version="1.0" encoding="utf-8"?>
xml version="1.0" is simple version which is used currently specify in form of "1.x"
 encoding ="utf-8It may be possible that Xml file may contain the non ASCII characters, so we have to use unicode

Line 2:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 <LinearLayout is Tag of LinearLayout which is default  defined in the android application LinearLayout Tag.The namespace is defined by the xmlns attribute in the start tag(LinearLayout) of an element.The namespace declaration has the following syntax. xmlns:prefix="URI". namespace(prefix android) is used in each line main.xml file.



Line 3: android:layout_width="fill_parent"
android is namespace and layout_width is the attribute of LinearLayout tag. fill_parent is value of attribute .fill_parents stretches the contents to fill container.you can choose the number of value by pressing CTRL+SPACEBAR 

Line 4: android:layout_height="fill_parent"
layout_height is attribute of LinearLayout tag. The value of layout_height is fill_parent and wrap_content.The wrap_content means Views occupies the place in container as it should be.

Line 5:android:orientation="vertical"
android:orientation define the layout orientation it may be specify the vertical or Horizontal


Line 6:<TextView
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

 TextView is simple a Text.android:layout_width="fill_parent" and android:layout_height="wrap_content" has a same explanation as i above is given

 android:text="@string/hello" />
There is two method for set the android:text
(1) The first Method  is the set the android:text attribute to raw string
<TextView
    -----------
   -----------
android:text="It is my first porgram" />

(2)The second method is the use the string resources called "my_program" which should be define in string.xml file
 
<TextView
    -----------
   -----------
android:text="@string/my_program" /> 

When you use first method android SDK give warning   "Hardcoded string "hello", should use @string resource" you may ignore. when you use second method you must edit explcity in string.xml file in resource file 
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">It is my first android program</string>
    <string name="app_name">Helloandroid</string>

</resources>

but i prefer that you must use second method.


 It is Graphical representaion of string.xml. Here is two string resouce is given 
hello(String)
app_name(String)
you can change the value of both attribute.Here we change the value of Hello string "It is my first android program". you must save before going forward


When you change These change reflect in the  file (string.xml). resource file when you edit
 String resources through the Graphical mode, automatically string.xml resource file updated


Now execute the program once again.your change will reflect in program.

package hello.abh.com;

import android.app.Activity;
import android.os.Bundle;

public class HelloandroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Line 1: package hell.abh.com this is namespace conversion in every android application

Line 2:  import android.app.Activity Since each application is start with a activity Activity is necessary to imported


Line 3: import android.os.Bundle is another package which is needed.

Line 4: HelloandroidActivity is our class which extends Activity class.

Line 5:public void onCreate(Bundle savedInstanceState) it is method of Activity class which is overridden it has parameter savedInstanceState.It is current state information is bundled as savedInstanceState in other word we can say that All current state information is bundled as in savedInstanceState  that is object which held in memory.Here we will not directly handle but we must be aware of the presence 

Line 6:super.onCreate(savedInstanceState) This line is very important because it call to base Activity class to perform setup work for Helloandroid. If you do not write this line , you will receive run-time exception error. Remember it always call under onCreate(BudlesavedInstanceState)



Before explaining the line setContentView(R.layout.main) we want to tell what is R in this code
R is stand for Resources. we have been  talking about only two file main.xml and Helloandroid.java.source file.main.xml is a xml file which determine the layout of the application and Helloandroid is source code which used main.xml file with the help or R.class file.R.class file act as communication link between your source code and main.xml file.
It has all  reference ID of all the resources in your res/ directory .source code link the main.xml file through the reference IDs which is generated in R.Java file.

if you see carefully  R.java file four reference id ic_launcher,main,app_name,hell. When you complied your application There is  aapt tool that generate generates the R. class. 
I told you that R.java file create all Reference id for /res folder if you see carefully in the Right hand of panel you will see ic_launcher.png under /res/drawable-hdpi file that is default image which shown when you execute the program.There is main.xml under /res/layout/main.xml and /res/values/string.xml first one is main.xml resource and latter strings.xml resource

There are two ways you can access a resource:
  1. In source code: By using syntex:eg Syntex- R. resource type.resource name
    R.string.hello.actually since it is static class 
    string is the resource type and hello is the resource name.
  2. In XML file: By using  a special XML syntax that also use  resource ID defined in your R class, such as:
    @string/hello
    string is the resource type and hello is the resource name. You can use this syntax in an XML resource any place.  
setContentView(R.layout.main)- It is  method setContentView (R.layout.main) which  sets the Activity's contents to specific resources that is main.xml (layout directory) through pointer in (R,java).file. The main.xml file contains size of Helloandroid screen and TextView.
  
Android is based upon the MVC pattern but There is no programming which is universally perfectly framed  in MVC pattern. MVC is a concept rather than a programming framework . You can implement your own MVC in any platforms. As long as you stick to the following basic idea
  1. Model: What to render
  2. View: How to render
  3. Controller: Events, user interface input
All about the view concern of the android application we done by main.xml file and we placed externally all resources in /res folder.

2 comments: