Android Hello World Application

0
256
Android Hello World Application

With this module, we are starting the actual Android application programming with the help of one of the already installed Android frameworks. But to start with coding section, you must setup an Android development environment beforehand. If you haven’t then you can go through our Android Environment Setup module. Another prerequisite will be to have a basic knowledge of the Android Studio.

Also Read: Android Eclipse IDE

This first Android application will just display “Hello World!”. By using this first basic application, you will get to know the basics of the Android application.

How to create an Android application?
Follow the step-by-step guide of ours to understand how an Android application is created.

Step 1: We are using Android Studio to create the Android application. Once you launch this software, you will get a window which will appear on the following screen.

Step 2: Here, click on Start a new Android Studio project. After this, you will be redirected to another window titled as Create New Project. This window will ask you to enter a few details which include the Application name, Company domain, Package name, and Project location.

Step 3: After entering the required details, hit the Next button. On this window, you will be called to select a few form factors. This new window will appear like the given screen. Well, we are using API23: Android 6.0 (Marshmallow) which we have declared in the Minimum SDK of Phone and Tablet.

Step 4: This level will ask you to add activities to mobile. On this window, you will get a number of default layouts for your Android application.

Step 5: Finally, you have arrived at the application development tool where you can write the code for your first application.

Closer look at Android Application
Before we go straight to the application coding, you must understand that there are various files and directories used for certain purposes. Let’s take a closer look at all of these files and directories of the Android project.

S.No. Files and Directories Description
1 java * Contains .java source files used in Android projects
* Contains MainActivity.java file which automatically runs, once your application is launched. This Java file includes an Activity class in it
2 res/drawable * Contains drawable objects especially used for high-density screen
3 res/layout * Files used to help you to define an application’s user interface
4 res/values * Contains numerous XML files
* These files keep a collection of strings and color definitions
5 manifests/AndroidManifest.xml * Describes basic features of an application
* Contains definition of each and every component of an application
6 Gradle Scripts/build.gradle * An auto-generated file
* Contains compileSdkVersion, buildToolsVersion, applicationId, minSdkVersion, targetSdkVersion, versionCode and versionName

Here, you will get to know a bit more about the major application files.

Main Activity File – MainActivity.java
As you can already see MainActivity.java is a Java file. This is the real application file and helps in running your application. This file first gets converted into Dalvik executable file after which it helps your application to run. Check the given default code for Hello World! app.

package com.example.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, onCreate() method is one of those methods which is used when you want to load an activity. Other than that, R.layout.activity_main is used to refer to the activity_main.xml file from the res/layout directory.

Manifest File – manifest.xml
All the components used in an application should be defined in manifest.xml. This file is located at the root of the application project directory. manifest.xml is an important file as it acts as an interface between your operating system and the developed Android application. So, in any case, if you forget to declare your application’s component in this file then the operating system won’t be able to recognize them. The given code will help you to understand how a manifest.xml file usually looks like.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.example.tutorialspoint7.myapplication”>
   <application
      android:allowBackup=”true”
      android:icon=”@mipmap/ic_launcher”
      android:label=”@string/app_name”
      android:supportsRtl=”true”
      android:theme=”@style/AppTheme”>
      <activity android:name=”.MainActivity”>
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>
      </activity>
   </application>
</manifest>

Here, all the important application’s components are enclosed between <application> and </application> tags. Other than that, android:icon is an attribute which is used to point the application icon present in res/drawable-hdpi directory. In this drawable directory, you will find an image under the name ic_launcher.png which is also used in the application. And the <activity> and </activity> tags enclose the specification of an activity. The action performed by the intent filter is stored in android.intent.action.MAIN. This file points out an activity as the entry point for your application. While android.intent.action.LAUNCHER is the category for the intent filter. The inclusion of this file shows that your application is available to be launched with the help of device’s launcher icon.

To refer to strings.xml file, we have used @string. While @string/app_name is already defined in the strings.xml file under the name of app_name. The definition of the @string/app_name is “HelloWorld”. Similarly, other strings are also declared in the strings.xml file.

To specify various components, you need to use the following tags in your manifest file. Take a look at the following list.

* <activity> and </activity> : to denote activities
* <service> : to denote services
* <receiver> and </receiver> : to denote broadcast receivers
* <provider> : to denote content providers

Strings File – strings.xml
The strings file is denoted by strings.xml which is located in the res/values directory. All the text which are being used by your application is already declared in this file. From the names of the buttons to the labels, everything is being defined in the strings.xml file. In totality, this file is having the responsibility of handling all the textual content of the application. Well, the strings.xml file usually contains the code which looks like the following.

<resources>
   <string name=”app_name”>HelloWorld</string>
   <string name=”hello_world”>Hello world!</string>
   <string name=”menu_settings”>Settings</string>
   <string name=”title_activity_main”>MainActivity</string>
</resources>

Layout File – activity_main.xml
To represent the layout file, the Android Studio is using activity_main.xml. You can locate this file in the res/layout directory. This file will be used when you want to build a user interface for your application. The default layout for the “Hello World!” application will appear as the given code.

<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” >
   <TextView
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
            android:layout_centerHorizontal=”true”
            android:layout_centerVertical=”true”
            android:padding=”@dimen/padding_medium”
            android:text=”@string/hello_world”
     tools:context=”.MainActivity” />  
</RelativeLayout>

The above code is a basic structure for the RelativeLayout. The Android control, TextView own various attributes which include android:layout_width, android:layout_height, android:layout_centerHorizontal, android:layout_centerVertical, and a few others. All of these attributes help in building the user interface of your application. In the above lines of code, @string is used to denote strings.xml which resides in the res/values directory. And so, the @string/hello_world is used to denote the hello string which already defined in strings.xml file. This string will be responsible for displaying the “Hello World!”.

Running The “Hello World!” Application
Now, let’s run your first Android application. We have already created AVD to test the application. So, to run your recently created application, you need to open the activity file of the project in the Android Studio. After which, go to the toolbar of the software and click on the Run button. The process will further help the Android Studio to install your application on the AVD. Then, the application will run to display the following output only if there are no syntax errors in the coding part. You will be able to see this output on the Emulator window.