Signup/Sign In

Steps to Create a Simple Android Application Project

In the previous tutorial we learned how we can setup our system for Android Application Development and how to create an AVD(Android Virtual Device) to test our applications. So now in this tutorial, we will be creating our first Android Application and will try to understand the basics of Android Application development.


App Basics

Our first application will be a simple application with one activity and a layout, in which we will just print Hello World! on the device screen. Here are a few things that you must know, don't worry about it, you will understand all these, as we move forward:

  • An activity is an instance of Activity, a class in the Android SDK. An activity is responsible for managing user interaction with a screen of information. Whenever we create a new activity, we are actually writing a subclass of Activity class. A simple application may need only one subclass, while a complex application can have many. In our first application FirstAppActivity will be our activity subclass.
  • A layout defines a set of user interface(UI) objects(Views and Widgets) and their position on the device screen. A layout is made up of definitions written in XML. Each definition is used to create an object(a user interface) that appears on the device's screen, like a button or some text or an image.

In simple words we can say, that the backend will be handled by the class which extends the Activity class, and the frontend i.e. the User interface is defined in the layout XML file.


Steps to create an Android Application

The first step is to create a new Android Project. An Android Project contains all the files that make up an application. Open Android Studio, go to File → New → New Project...

You should see the new project wizard. Enter FirstApp as the application name. For the Company Domain, enter com.studytonight or you can use your own name for this, for example com.johnwick. As you enter the Company Domain, you will see the package name is automatically generated, just below the Company Domain field. Let the Project Location be as it is, you can change it if you want.

First Android Application

Notice that the package name(generated below Company Domain field) uses a reverse DNS convention in which the domain name of your organization is reversed and suffixed with further identifiers like application name. This convention keeps package names unique and distinguishes applications from each other on a device and on Google Play.


Click on Next. Next comes the step where Android Studio asks us about the Target Android Device, for which we are developing the application. Our FirstApp will only support phones, so just check Phone and Tablet. Select a Minimum SDK version of API 16: Android 4.1 (Jelly Bean). Minimum SDK version specifies the minimum version of Android required on a device to run our application.

First Android Application


In the Next step, you will be asked to choose a template for the first screen of you application, choose Empty Activity and click on Next.

First Android Application


In the final dialog of this wizard, you will be asked to name your activity subclass. Name it FirstAppActivity. Notice the Activity suffix on the class name. This is not required, but it is an excellent convention to follow.

First Android Application

The layout name will automatically update to activity_first_app to reflect the activity's name. The layout name reverses the order of the activity name, its all lowercase, and has underscores between words.

Click on Finish, Android Studio will take some time to prepare the project for you.


Navigating in Android Studio

Android Studio will open your project in a window as shown below:

navigating in Android Application

The different panes in Android Studio are called Tool Windows.

The lefthand view is the Project tool window. From here, you can view and manage the files associated with your project.

The middle view is the Editor. To get you started, Android Studio has opened activity_first_app.xml and FirstAppActivity.java files in the editor.

You can also switch to Design mode, to see how your layout will appear, by clicking on the Design Tab in the bottom-left corner of the Editor window, when the layout xml is open. In the Design mode you can directly drag and drop elements, and Android Studio will automatically write the code for the added widget in your layout xml file.

First Android Application

You can also open the Preview tool window, by going to View → Tool Windows → Preview.

The default activity layout defines two widgets: a RelativeLayout and a TextView.

When we say widgets, widgets are the building blocks you use to compose a user interface(UI). A widget can show text or graphics, interact with the user, or arrange other widgets on the screen. Buttons, text input controls, and checkboxes are all types of widgets. We will learn about Views and Widgets in coming lessons.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/white"
    tools:context="com.example.android.myfirstandroidapp.FirstAppActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@strings/hello_world"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:textSize="15sp"
        android:paddingTop="5dp" />
        
</RelativeLayout>

Our FirstAppActivity.java class will override onCreate() method, and is extending ActionBarActivity. ActionBarActivity is a subclass of Activity class of Android SDK, in which an Action Bar(Top navbar) is added by default.

// Some import statements

public class FirstAppActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_app);
    }
}

So this is all about your first application. Hope you have understood the basic concepts related to developing android application, and now know how to use Android studio.

Do not worry much about the code if you don't understand it. We will explain each and everything in detail in coming tutorials. As of now, just remember that any class which extends Activity class will create a view in the App, and the design of that View is set using the layout.xml file. In the FirstAppActivity class, the line setContentView(R.layout.activity_first_app); links the layout XML file with the FirstAppActivity class.