Hello World Program in Android

Learn via video courses
Topics Covered

Overview

Android app development is a way to create software applications that can run on the Android Operating System.

Android apps can be developed with the help of programming languages like Java, Kotlin, and C++. The Android SDK (Software Development Kit) provides tools and resources for developers to create Android apps.

To acquainted with Android app development, the common way is to start by creating a Hello World program in Android. Moreover, you need to be familiar with the Android Studio environment and how the application runs.

Setting Up the Environment

Android Studio is the official integrated development environment (IDE) for Android app development. It is a free, open-source tool provided by Google, and is widely used by Android developers worldwide.

Before installing Android Studio, you need to make sure that you've JDK already installed in your system. If not, here's the link to set up JDK and Environment Variables:

Now that you have got JDK installed properly in your system, you can download Android Studio.

Visiting the page would look similar to the image here.

first-program-in-android-studio

Accept the License Agreement and now download the Android Studio patch.

first-program-in-android-studio-1

Upon the download completion, open the file and click on Next to start the installation process.

first-program-in-android-studio-2

You'll have an option for the Android Virtual Device to install along with Android Studio.

first-program-in-android-studio-3

Choose a location for Installation and click on Next.

first-program-in-android-studio-4

Once the installation finishes, click on Next again.

first-program-in-android-studio-5

first-program-in-android-studio-6

first-program-in-android-studio-7

Now, click on Finish again to finalize the entire process.

first-program-in-android-studio-8

first-program-in-android-studio-9

You have your Android Studio installed and you can create a New Project now.

Creating a New Project

Since we have wandered over the basics of the Android Studio environment, let's focus on creating your first Android app. It's going to be a simple screen with a message at the center of the screen.

Once you open Android Studio, you'll be welcomed with a medley of options including the one that says Start a new Android Studio project. Clicking on it will take you to another page, which has an assortment of all sorts of activity screens to choose from. Since we are starting from scratch, let's create our custom activity with an Empty Activity. Then click on Next.

creating-a-new-project-in-android-studio

creating-a-new-project-in-android-studio-1

The package name in this tab is a unique identifier for the Android application. It is also used to create a unique namespace for the application's code and resources.

The language choice can either be Java or Kotlin. We'll be choosing Java for now. Minimum SDK is the minimum version of the Android OS that your app will support. The value set for Minimum SDK determines which API level the app will be able to use and which devices will be able to run the app. The lower the value, the more devices the app will be able to support, but certain newer features and APIs may not be available.

Understanding the Code

understanding-the-code-in-android-studio

As the new project starts to build, you can observe the message that says Gradle project sync in progress.

understanding-the-code-in-android-studio-1

Here's the startup code for your Android app.

The above code is for the class MainActivity.java inside the HelloWorldApp. This is also the default code generated by the Empty Activity template in the Android Studio. The class MainActivity.java extends AppCompatActivity.java and also overrides the onCreate() method to create the startup logic of the Activity.

This method also sets the content view to the layout file activity_main.xml. This layout file is defined in the res/layout directory and is used to define the user interface of the app.

Adding the "Hello World" Message

And by default, if we wish to have an Android app with just the message "Hello World!", then it's already created by the Empty Activity itself.

Here's the view you can see in the activity_main.xml.

adding-the-hello-world-message-in-android-studio

However, that's all created for your convenience. To know it's in and out of the environment, let's change the message to be displayed.

Here's the entire code for activity_main.xml. We have modified the text to be displayed and also its size as well as color with a HEX code. Notice that the textSize is denoted in terms of sp (scale independent) in activity_main.xml:

Here's the onCreate logic for MainActivity.java.

adding-the-hello-world-message-in-android-studio-1

We can change the code in activity_main.xml by clicking either on Code or Split as shown in the image below.

adding-the-hello-world-message-in-android-studio-2

Running the App

Now, we are ready to run this application. However, we don't have any devices to run it on. We can attach a physical device to run and test the application or else we can use an AVD emulator to provide an Android Emulator on our screen itself. running-the-app-in-android-studio

running-the-app-in-android-studio-1

Clicking on AVD Manager will take you to a new tab where you'd have the option to Create Virtual Device. running-the-app-in-android-studio-2

Choose any device under the Phone section and click on Next. running-the-app-in-android-studio-3

Let's select the latest API version, for now, to make things simpler. running-the-app-in-android-studio-4

Choose any name for the AVD Emulator. running-the-app-in-android-studio-5

As you click on the Play button, the Emulator device will appear on the screen. running-the-app-in-android-studio-6

Since the device is still getting ready to use, so wait for a while till the home screen loads up. Click on the Play button again to run the application. And here's what it looks like: running-the-app-in-android-studio-7

running-the-app-in-android-studio-8

running-the-app-in-android-studio-9

running-the-app-in-android-studio-10

Conclusion

  • Android app development is a way to create software applications that can run on the Android Operating System.
  • The layout files are defined in the res/layout directory and is used to define the user interface of the app.
  • Minimum SDK is the minimum version of the Android OS that your app will support. The value set for Minimum SDK determines which API level the app will be able to use and which devices will be able to run the app. The lower the value, the more devices the app will be able to support.