Learn Android Development For Beginners

Write Your First Program

In this chapter you will learn how to create the application in Android. We will use Android studio IDE for developing the application. The android application development is same as other programming methodologies, if you have programming experience in any other development environment then you will not found any major difference in it and if you are new in development then you are supposed to learn java and some basic concepts of Object Oriented programming before starting this course.

Let’s start your journey to write first Android program.

1.    Lunch the Android Studio and click on File > New Project. A new project window will be open as you can see in figure 3.1

(figure 3.1)

2.    In this window Enter “My First App” as the Application Name and “MyFirstApp” as the Module Name. The Package Name “com.example.myFirstApp” is same as package naming convention in java. You can learn more about it in java. In Minimum required SDK select the minimum version of Android SDK that your application will support and also select the Target SDK. Make sure that Create Activity is checked to inform android studio to automatically create one activity for you. We will learn about the Activity in the next chapter, but for now it is a UI interface you will interact with it. Press the next button.

3.     In the next window choose the icon for your application. By default android studio choose android icon for your application. You can also do some other settings that are available on the window. In figure 3.2 you can see the different settings available on the window

(figure 3.2)

4.     In the next form you can select the style of your activity. Activity is same like a window’s form. For now select the “Blank Activity” as shown in figure 3.3. Press the next button to move on next step.

(figure 3.3)

5.     In this step you need to set the name of your activity and layout. We will discuss later what is the activity name and layout name. For now you can say “Activity Name” is the name of class and the “Layout name” is the name of your UI file that will contains all the controls and that can be access in activity class using different techniques. Enter “MainActivity” as Activity Name and “activity_main” as layout name.

Leave the Navigation Type as None. We will discuss it later.

(figure 3.4)

Press the finish button and Android studio will create a new solution for you as per your provided information. We will explain the structure and different areas of application later. But first we must execute our first application. Click on Run > Run “MyFirstApp” from menu or click on Play button from the toolbar to run the application.

(figure 3.5)

For running application you must have configured virtual device using “AVD Manager”. You can find the guidelines to configure virtual device here. When you will click on the Run button the android studio will open window to choose the Virtual device for your application.

(figure 3.6)

If any virtual device is already running you can pick it from “Choose a running device” section otherwise you can lunch new emulator by selecting from available emulators list. If you haven’t configured any device yet, don’t worry you can configured it from here by click on browse button […] next to the dropdown. We already have two virtual devices configured. So select the device and press ok button. Wait for few minutes virtual device may take some time to start.

(figure 3.7)

When virtual device will be started, you will see “Hello World!” on the emulator. Isn’t it easy and simple as other programming languages are? Now there is no need to close emulator, just keep it running and start playing with code and simple run application again and in choose device window select the running device that will save your time.

Understand the structure of Android Application

Now let’s understand the structure of your first application. The main concept or folder segregation is same in different development IDE for android e.g. eclipse, android studio etc. But the management of different application is somehow different in these IDEs.

(figure 3.8)

In figure 3.8 you can see the Project panel on left hand side that is showing the structure of the solution. If Project panel hide, you can show it by click on View > Tool Window > Project. Under MyFirstApp / Src / Main folder that is the area that is more important to understand. Following is the brief description of some folders.

  • Java” folder contains the all your code files written in java.
  • res” folder contains all the resource files those are associate to application e.g. images, styles, layout, string values and menu items etc.
  • AndroidManifest.xml” file contain the information of activities, services, permissions etc.

We will explain all the area in details step by step later. Just remember in the new project wizard we enter the “MainActivity” as Activity name and “activity_main” as layout name. The android studio has created the MainActivity file under the Java / com.example.myfirstapp/ folder and “activity_main.xml” under the res / layout folder.

Layout file

The layout file is use to design the UI of the application. You can find the layout folder under src / main / res path as shown in figure 3.9.

(figure 3.9)

Open the file activity_main.xml by double click on it. You can design the UI using the designer or Text editor. You can switch between these two modes from the bottom of the designer window. You can see these tabs in the figure 3.10.

(figure 3.10)

In the Design mode you can design the UI by drag drop and you can do setting using the Properties Palette. Switch to the text view by click on the “Text” tab at the bottom of the designer window. You will see the XML that is used to design the UI in Android development. When you design the UI using Designer the IDE actually writes XML at the back end and the properties you set from designer written as an attributes in the XML.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="@string/hello_world" />


In the Text designing view you can see that the RelativeLayout is the parent tag that’s mean you are using relative layout and in this layout you have TextView. If you have any programming background than you might know that you can do the changes from both Text designer view and the UI designer. In this chapter we will not go into the detail of the designing. But just for giving you the confidence we will change the “Hello World!” text of the text view. Go to the designer again and select the TextView control.

(figure 3.11)

Find the “text” property in the property palette and click on it. You will notice that the text written in property is “@string/hello_world” but the content are being shown the text view is “Hello World”. Don’t worry the @string means get text from the resource file and “hello_wold” is the id of resource that is stored in string resource. We will learn about resource files later, but for now simply type “Hi! It’s my first program.

(figure 3.12)

You can see new changed text in the designer and when you will run application again, you can see new text instead of “Hello World”.


MainActivity.java file is the file that contains the code. Open the MainActivity.java from “Src > main > java > com.example.myfirstapp > MainActivity.java”.

(figure 3.13)

The file contains the code written in java. You can see it in code below.

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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;

MainActivity class is inherited from the Activity class that represents the UI form for android application. In the OnCreate method the statement setContentView(R.layout.activity_main); set the layout for this activity. As we know the Android place separate file for code and Layout. We are not going in depth but later on we will discuss it in detail.

Explain AndroidManifest.xml

As a programmer you must know that each program always has one entry point from where the application starts running. In many programming languages we have Main() method. In Android development we provide the startup activity name in the AndroidManifest.xml file. This file contains the configuration related data of the application.

    android:label="@string/app_name" >

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

The configuration setting inside the <intent-filter> tag is use for setting startup activity. The main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category.

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

If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.

We define all the activities, services and permissions in this file. When we add any item e.g activity using designer, the IDE automatically add required information in the Manifest file, otherwise we need to add it manually.