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.
So here is step by step guide.
1 – Start Android Studio and click on “Start a new Android Studio project“.
2 – Enter “Hello World” in “Application Name” then click “Next“.
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“.
4 – Select “Empty Activity” then click “Next“.
5 – On next window just keep the default value and click “Finish“.
Now Android Studio will build the project. After successful build you will see the editor with the following code written in file “MainActivity.java”.
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.
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.
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.
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”.
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.
That’s all in this post.
we are very glad to inform you that it is very useful for us.