Android SDK- Hello World example as the first app

Let’s start real programming with Android Framework. Before writing the first app with the Android SDK, make sure you have the Android development environment installed correctly. 

Android SDK, We also assume that you know a little about working with Android studio. So let’s write a simple Android app that says “Hello World! Prints.

Build an Android app

The first step is to create a simple Android application using Android studio. When you click on the Android studio icon, a screen will appear as shown below.

C: \ Users \ Mr \ Desktop \ studio9.jpg

You can start your application by creating a new project in Android studio. The next page will ask you for the program name, package information, and project creation location.

C: \ Users \ Mr \ Desktop \ studio10.jpg

After entering the name of the program, you must specify on which device the program will run, specify the Minimum SDK here. In this tutorial, we will use API23, ie Android 6.0 (Mashmallow) –

C: \ Users \ Mr \ Desktop \ studio11.jpg

The next step of the installation is to select Activity for Mobile, which specifies the default layout of the application.

C: \ Users \ Mr \ Desktop \ studio12.jpg

Finally, the development tool will open to write program code.

C: \ Users \ Mr \ Desktop \ studio13.jpg

Android application structure

Before running the program, you should be aware of several directories and files in the Android project –

C: \ Users \ Mr \ Desktop \ hello_word1.jpg

Number Folders, files and descriptions
1 JavaContains java source files for the project. By default, it contains the MainActivity.java source file, which has an Activity class that runs when the program is launched using the program icon.
۲ res / drawable-hdpiA directory for drawable objects designed for high-density screens.
3 res / layoutDirectory for files that define the user interface of the program.
4 res / valuesA directory for various other XML files that contains a set of resources such as the definition of strings and colors.
5 AndroidManifest.xmlIt is a Manifest file that describes the basic features of the program and defines each of its components.
6 Build.gradleThe file is automatically generated and includes compileSdkVersion, buildToolsVersion, ApplicationId, minSdkVersion, targetSdkVersion, versionCode and versionName.

The following is a summary of important program files.

Main Activity file

The Main Activity code is a Java MainActivity.java file. This is the program file that eventually becomes an executable Dalvik and executes the program. Below is the default code generated by Hello World! You see –

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, R.layout.activity_main points to the activ_main.xml file in the res / layout folder. The onCreate () method is one of the methods specified when loading an Activity.

Manifest file

Each part you create as part of the program, you must declare all its components in manifest.xml, which is located in the root directory of the program. This file acts as an interface between the Android operating system and the application, so if you do not specify your component in this file, the operating system will not consider it. For example, you can see the default content of the manifest file below –

<? 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 the <application>… </application> tag encloses the components of the application. The android: icon attribute points to the icon in res / drawable-hdpi. The program uses an image called ic_launcher.png located in the drawable folder.

The <activity> tag is used to specify activity. android: name Specifies the full name of the Activity subclass. The android: label attribute specifies a string to use as an Activity tag. You can specify multiple activities using <activity> tags.

android.intent.action.MAIN in intent-filter indicates that the activity is the entry point of the application. android.intent.categ.LAUNCHER indicates that the application can be launched from the icons section.

@string refers to the strings.xml file described below. Hence, string / app_name @ refers to the app_name defined in the strings.xml file, which is “HelloWorld”. In a similar way, other strings are stored in the program.

Below is a list of tags that you will use in the Manifest file to specify the various components of the Android app –

  • <activity> Activity tag
  • <service> service tag
  • <receiver> tag for broadcast receivers
  • <provider> tags for content providers

Strings file

The strings.xml file is located in the res / Values ​​folder and contains all the text that the program uses. For example, button names, tags, default text, and similar types of strings are included in this file. This file is responsible for your textual content. For example, a default string file would look like this:

<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 is a layout file in the res / layout directory, which is referenced by the program when creating the user interface. You can change this file as many times as you want to change the design of your program. This file is for “Hello World!” , Has the following content that is related to the default design –

<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>

This is a simple example of RelativeLayout. TextView is an Android control that is used to build the GUI and has various features such as: android: layout_width, android: layout_height, etc. that are used to adjust the width and height. @string points to the strings.xml file in the res / Values ​​folder. Hence, @string / hello_world refers to the hello string defined in the strings.xml file, which is “Hello World!” Is.

Run the program

Let’s Hello World! To run. Suppose you created your AVD while setting up your Android environment. To run the program from Android studio, open one of the project activity files and from the toolbar, click on the Run Eclipse Run Icon icon. Android studio installs the program in AVD and launches it, and if everything is set correctly, the Emulator window is displayed –

C: \ Users \ Mr \ Desktop \ android_hello_world.jpg

Congratulations! You have developed your first Android application and now follow the rest of the step-by-step tutorials to become a great Android developer.

Leave a Reply

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