Let’s Code!

Posted on Updated on

Hi all!

So, I am back again. This time, we will delve into the coding arena and try to understand the code that is automatically generated when you create a new project (Remember! when we had run the project in the post titled Let’s Begin! , an app was launched in the emulator in spite of the fact that we had not written any code ourselves).

How did that happen? We have already seen that the layout which is automatically created when we make a new project is exactly same to the screen of that app. What about the rest of the its functioning? Who tells Android to set the screen to that particular layout? Let’s see!

If you open the project, click on the package name (in our case, it was com.hello.world) and open the source file (which is HelloWorldActivity.java in our HelloWorld project discussed in previous posts), you will find a few lines of code. The code which does the task mentioned above. But, the question is How?

The first line of code is simply to tell the package name to which this class belongs to(Class? For now, it is sufficient to understand that it is the most basic component of a JAVA code, which encapsulates other functions and variables . We will look at them in a greater depth in the coming posts). It’s the same as selected while making the project. The second line of code simply imports the android classes that are already present in the Android framework and provide us the functions required in the code.

Now, you can see we have declared a class with an access modifier called public which poses no restriction on the access (For those who are unfamiliar with it, we will come to that later). The name of the class is same as the name of the java file. Now, the code says that this class extends an Activity. So, what is an Activity? An activity is the top level component ( a pre-built class) which controls what you see on the screen. It may either cover the entire screen or a part of it, but only one activity runs at a time in Android. So, even if the activity covers just a part of the screen, the remaining part will be inactive till the time that activity is running.

So, the third line of code basically declares a class with no access restrictions, which extends an Activity (because of which we call this class an Activity as well!) so that it can interact with the screen, which means that this class can use all the functions and variables provided by the Activity class.

The rest of the code simply defines a function which is automatically called when this activity is first created. We don’t have to remember the syntax as this function is automatically generated. There are two things happening in this function. We don’t need to discuss the first one.

What about the line setContentView (R.layout.main)? Can you guess what this line of code does?

In the next post, we will discuss this line of code and also, dive deeper into the coding world of Android App Development.

Till then, BYE!

8 thoughts on “Let’s Code!

    Prateek said:
    May 20, 2012 at 6:43 pm

    nikhil,suppose i am using many buttons….is it possible that access those buttons by ids thar are a part of an array?

      Nikhil Gupta responded:
      May 20, 2012 at 8:16 pm

      Actually, IDs are declared in the layout using @+id and hence, in the code, you can access buttons using R.id.* where * is the ID name given in the layout. So, there is no question of using array here? Hope you get the point!

        Prateek said:
        May 21, 2012 at 12:06 am

        ya,but suppose i have 3 ids b1,b2,b3.then in my .java file i declare Button b[3] and declare b[1]=(Button)findviewbyid(r.id.b1) n similarly for all three.can i do this…since if i cud do this then later on it wud b simple for me to do calculation.i tried dis and there was some error dat i cudnt understand

    Prateek said:
    May 21, 2012 at 12:09 am

    Also another problem.suppose i have char a;now i an using it in a condition as if(a== ‘+’ || ‘-‘) …do something…this is giving invalid error.how do i use logical operators then?

      Nikhil Gupta responded:
      May 21, 2012 at 12:30 am

      Please post the error that you are getting while using button array. Regarding the second comment, you are using an incorrect syntax. You must use if((a==’+’)||(a==’-‘)).

    Alex - a curious app developer :) said:
    May 23, 2012 at 4:32 am

    I’ve learn some good stuff here. Definitely price bookmarking for revisiting. I wonder how much attempt you set to create this sort of magnificent informative web site.

    Prateek said:
    May 23, 2012 at 3:18 pm

    […] of the Hello World app (other than the one which is automatically generated! Remember? Refer to Let’s Code!) , in which we will focusing on the use of buttons and toasts(?? Don’t worry! I will […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s