MY mENU


Saturday, 11 February 2012

About Java Class in Android

The Main Java Class in Android Application With Some Explanation:


package org.hello.HelloAndroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Let us spend some time understanding the code that got auto generated. Let me clarify few things for you about the code. The first line declares the package “org.hello.HelloAndroid”. A package is a namespace that organizes a set of related classes and interfaces. So all the folders for this project will be contained in this package and they will contain different elements like images, sound files and Java source files.

         The next two lines are importing standard packages for the Android specific Java code. Import is the key word which is used to access the standard and no standard packages inside a java file. We then create our class HelloAndroid which inherits the Activity class. Activity is a standard class of Android.

        Inside the class we define a method onCreate() which is called when the activity is starting. This is where most initialization happens.


         The setContentView() inflates the activity’s UI and in our example it is calling the main xml discussed below to draw the user Interface.
        
       To sum up this class imports standard definitions and create a class which is a subclass(inherited) of an Activity. The class further has a method called as onCreate() which initializes and paints the UI from a main file. So now You understand what happened in that auto generated java class.

No comments:

Post a Comment