Tuesday, June 7, 2011

Hello Android World Example

Please try to follow the steps what i am saying in the begining and then i will explain about the code briefly.
First of all try to create a new project and in that project create the following java class.
STEP 1::
//MainActivity.java
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
STEP 2::
and in the second step  try to configure main.xml (res/layout/main.xml)in the following way.....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout> 
 STEP 3::
and in the third step create a strings.xml(res/values/strings.xml)in the following way..........
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello Android World</string>
    <string name="app_name">AndoridHelloWorldActivityEx</string>
</resources>
now deploy the application into your android supported mobile or android emulator.
Now it will display the output as given below.
So if u see the above window it is an android emulator installed in my computer.If i deploy my application into the emulator the i got the above output.
In the following figure i am showing how the project folders are configured in eclipse are given below.........

 So please see the above image I created a new project in eclipse and the project name is AndroidHelloWorldActivity in the project i created a new package and here the package name i choose is com.ibm and you may choose your own package name there is no restrictions in package name.

In the package i choose a new class called MainActivity.java and it should extend Activity class.
//MainActivity.java
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
  
the MainActivity.java should extend Activity class. I think u may get the doubt like why we should extend Activity class if i explain here u may get confused so pls try to create the application as i said and there after in the following discussions i will explain about everything.
 Here MainActivity is called as activity.What is an activity?
            Activity is nothing but a single screen in the application (just remember like this).
So to display the screen we need a function called onCreate(...).So according to that we created one function and it should call super onCreate(..) function and the next line is 
                               setContentView(R.layout.main);
when this function is called then it will go to res folder in that folder it will check for another folder called layout folder and in the layout folder in check for xml file called main.xml...........
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

 This xml file is very important because we can display the screens in the application in two way's one is using java functions and another way is using xml files.Here we are using xml file i.e main.xml file.
In the xml file first we need to create <LinearLayout.....> tag in that tag we can declare the textview,buttons,images,editViews etc..........So <LinearLayout> tag is very important.

Then what is the importance of Strings.xml file?
In the main.xml file we created one <TextView > tag see that one line is there like android:text="@string/hello"  here it calls the strings.xml file and in that checks for "hello" name attribute is there or not if it is there then take that string value and display it on the output screen.Here i can't say more than about strings.xml because if u are doing more and more applications u may easily understand the application easily..........................................

After conifiguring all this deploy the application into the emulator u may get the above described output..........................................
IN THE NEXT APPLICATION WE WILL SEE SOME MORE EXAMPLES ON ACTIVITY CLASS.

No comments:

Post a Comment