Learn Android Development For Beginners


An Activity is the important concept of the Android Application development. It usually represents the single screen in the application. It can be used in different ways as floating window or embedded inside of another activity. The class must be inherited form the Activity class.

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {



    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;


The Activity class takes care of creating a window. When activity first created it calls the OnCreate() where we usually initialize UI layout resource using setContentView(View) method.

Before moving the forward, First lets discuss the setContentView(R.layout.activity_main); statement. If you have any programming background then you must know that setContentView is the method/function and R.layout.activity_main is the parameter that we are passing.

What is R? The R is auto generated class that maintains the resources and it is used to access the resource in the code file.

In chapter 3 we have seen that res folder contains the different folder for resources e.g. drawable, layout, string etc. at the compile time android compiler generate R class that contains the separate sub class for each resource. Type R. in code file and the android studio popup the suggestion dialog that contains the name of all resources as you can see in figure below.

R in Android
(Figure 4.1)

Select or type layout and after dot write or select the name of your layout activity_main. So in simple word pass the layout id to setContentView() method to set layout for the activity.

The statement setContentView(R.layout.activity_main); means set the activity_main layout for the activity.