Exploring My First Android Application


By now you have made your first Android App, also you might have understood Android Structure and Working from previous post. Now let us Explore My First Android Application which we had made previously.


1. MainActivity.java

Starting with our file MainActivity.java inside java folder which is called first on launching MyFirstApp.

Mainactivity.java inside src folder

Refer the below script :

package com.nestedif.myfirstapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {


  • Line 1 is the package name, which remains same throughout the application unless we use multiple packages in same project.
  • Line 3-4 are the imports from android library framework. You don’t have to remember any such imports. Just press Alt. + Enter (MAC-OS  Cmd + Enter), that’s it !! Android Studio will add libraries for you.
  • Line 6  your class name should be same as the file name (over here MainActivity). Since we want this file as Android Activity (Activity is a page which holds visual layouts) we have to extend the class with AppCompatActivity Class, so as to re-use methods & properties of Activity class.
  • Line 9  When-ever our Activity is loaded, it will invoke onCreate() method. We have passed “Bundle savedInstanceState” as a parameter to onCreate() method. Bundle saves the state of Activity Automatically whenever we either rotate our phone (i.e. portrait or landscape ) or whenever the application is paused or stopped (e.g. preferences like score , levels etc. can be saved in this Bundle ). For this we have called a constructor “onCreate(savedInstanceState)”  in next line 10.
  • Line 11 setContentView() attaches the XML layout corresponding to the Java file. In our case ‘activity_main.xml‘ is linked with ‘MainActivity.java‘.  Here  ‘R.layout.activity_main‘ means ‘activity_main‘ which is present inside ‘res > Layout‘ Folder.


Tip :

If you don’t remember some of the things, don’t worry at all, since most of the above code will be generated by Android Studio. You just have to focus on understanding the meaning of the code.




2. activity_main.xml

Now Lets us understand the Layout XML file called : activity_main.xml

activity_main.xml inside Res-Layout folder

Refer the below script listing :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Hello World!" />


This is the XML file which will provide the backbone of Graphical Layout for our Android application. Even this file can be made easily using Drag-&-Drop of Android Studio so if you don’t remember any of the line, it will do, just understand the purpose of the stuff.

  • Line 2-10 We declare this layout as a Relative Layout. Which means that any new item added will depend upon other items for their relative positions and alignments. This will be covered later. layout_width & layout_height are made “match_parent” which means this Relative Layout will fill over its parent i.e. whole screen. Padding has been added from all four sides of screen. Rest all things are not needed as of now to understand.
  • Line 12-14 Next you can see a  TextView which serves as a Label that we used in Java. We have specified its “ layout – height & width” as “ wrap_content ” which means that the Textview will occupy space as per the size of text over its parent. If we replace it with “ fill_parent ” then it will take shape of the outer container. (You can see the change better in case of a button, which will be covered soon).
  • Line 15 Using property android:text we can specify the text which we want to see it on the TextView. At this point you might be willing to write your name or some greeting message instead of “hello world”, go ahead.
  • Android has a concept where we don’t directly specify the text, instead we place all our text inside file named String.xml inside an identifier and use that identifier. e.g. android:text=”@string/app_name”. Here ‘@string’ specifies that we have all our text inside “res/values/string.xml” file & ‘app_name’ is the identifier which will hold the value inside the string.xml file.
  • Refer the below image and Open the file String.xml, there you will see a string identifier named “app_name”. Now open activity_main.xml and replace android:text=”Hello World!” with android:text=”@string/app_name”, save it and then Run the app.

Res - Values - String.xml


3. Android ManiFest.xml

Finally lets us understand the AndroidManifest.xml file. Even this file is self-generated on using Android Studio Wizard, still we might require small modifications based on your App’s requirement.

Android Manifest.xml

Refer this script listing for Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />




  • Line 3 declares Package name which will be used throughout our Android application.
  • Line : 5-10  Application tag will specify things like allowing OS to take backup of App Data or not, which icon to be used as app icon, what is the app name, support for Right-to-Left languages, app theme, etc.
  • Line : 11 Declares the Activity which you have used in your application. If we miss this, our app will crash, since it won’t be able to trace the Activity.
  • Line : 12 Intents help in Transition between two Activities (i.e. we can start any other Activity using Intent). So to prevent Activities to be invoked from unknown Activities of some other App, we place ‘Intent-Filter‘ such that only the specified Activities can invoke it or pass data to it.
  • Line : 13 This line declares our MainActivity.java as a ‘main‘ entry point of our App, whenever invoked.
  • Line : 25 It will decide our activity whether or not to be the Launcher Activity of our application. Suppose we have multiple Activity and we want to set a particular Activity as launching activity, then we add this to promote it as Launcher Activity.



Final Output :

OP First Android Application - hello world



Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.