create first andriod application countontech.com
Android Development

Write Your First Android Application : “Hello World” of Android

In this post we are going to make our first Android application “Hello World”, the very basic application showing the text “Hello World!”. Here is the screen-shot of the application that we are going to create.

Screenshot from 2017-08-18 16-52-00

So here is step by step guide.

1 – Start Android Studio and click on “Start a new Android Studio project“.

Screenshot from 2017-08-18 11-47-01

2 – Enter “Hello World” in “Application Name” then click “Next“.

Screenshot from 2017-08-18 11-54-10

3 – Select “Minimum SDK” to “API 19: Android 4.4 (KitKat)” in “Phone and Tablet” and untick “Wear“, “TV” and “Android Auto” then click “Next“.

Screenshot from 2017-08-18 12-03-47

4 – Select “Empty Activity” then click “Next“.

Screenshot from 2017-08-18 12-05-56.png

5 – On next window just keep the default value and click “Finish“.

Screenshot from 2017-08-18 12-07-37

Now Android Studio will build the project. After successful build you will see the editor with the following code written in file “MainActivity.java”.

package com.example.nishant.helloworld;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Here Android Studio created a “Hello World” application automatically, but we will create our own “Hello World” for better understanding.

In the above code, the line setContentView(R.layout.activity_main); is directing the “activity_main.xml” to set as the main page of the application, here “activity_main.xml” is a layout. So to change the main page of the application we need to change “activity_main.xml” file.

In this project we will edit two files, the first file is “strings.xml” and the second file is “activity_main.xml”. File “strings.xml” contains all the string we need to print/show in application and file “activity_main.xml” contains the code that defines how the main page looks.

Before editing any file in Android Studio we need to know the file structure to get that file in editor. In the left side of the Android Studio you will see the file structure like shown in this image.Screenshot from 2017-08-18 14-13-52.png

Here you will see the root folder “app” and it’s sub-folders “manifests”, “java” and “res”.

You will find the “MainActivity.java” in “app > java > PACKEGE_NAME.helloworld”, you will need to edit “MainActivity.java” file if you want to use some Java code (in this project we don’t need to edit this file).

To get “strings.xml” file we have to look in “app > res > values”. In this project we will edit this file. Here “res” is the short form for “resource”.

And the last file we need is “activity_main.xml” and we will find this file in “app > res > layout”.

6 – First we replace all the code in “strings.xml” with following code.

<resources>
    <string name="app_name">Hello World</string>
    <string name="hello_text">Hello World!</string>
</resources>

Here the first string element is with name “app_name” which is containing the name of the application that is “Hello World” and the second string element is with name “hello_text” which is containing “Hello World!” which we will use in “activity_main.xml” file to show in our application.

7 – Now replace everything in “activity_main.xml” with following code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_text"/>

</LinearLayout>

Have you notice “android:text” property of “TextView” in above code. Here the value “@string/hello_text” is redirecting to the string element with name “hello_text” in “strings.xml” file. So the property “android:text” is fetching the text from that string element. Changing the string there will reflect here.

We have written our first Android application, now we need to run it, to do so we have two options. The first option is to use an AVD (Android Virtual Device) but it is very slow and the second and fast option is to use an Android device, so we will use second option.

8 – Before running it in Android device we need to enable “Developer Mode” in Android Device. Process to enable “Developer Mode” may be vary device to device, if you don’t know how to enable “Developer Mode” in your mobile then search on Google with your device name.

9 – After enabling “Developer Mode” simply connect the device to computer with a data cable and click “run app” button or press “shift + F10” then select your device name in the list and click “OK”.

Screenshot from 2017-08-18 16-48-12.png

10 – Let the Android Studio build and install the application on the device. After installing the application in device simply run it.

Woow! you have just created your first Android application. Here is the screen shot for the application.

Screenshot from 2017-08-18 16-52-00

That’s all in this post.

If you like this post don’t forget to share this post with your friends. Thanks

Nishant Bhakta

Hello this is Nishant Bhakta, The man behind this blog. I like writing blog, writing android code and watching movies. It's all i am getting about me for right now, when I get something more, it will be reflect here. Thanks
(Visited 64 times, 1 visits today)

4 thoughts on “Write Your First Android Application : “Hello World” of Android”

Leave a Reply

Your email address will not be published. Required fields are marked *