Advertisement
  1. Code
  2. JavaScript

TEST POST:: Testing Editor Changes

Difficulty:BeginnerLength:LongLanguages:
Unlimited Plugins, WordPress themes, videos & courses!  From $16.50/m
 
 
Advertisement
  1. CODE
  2. ANDROID

The Simplest Android App: Hello World

 
by 
Difficulty:Beginner Length:Medium Languages:

Learning to code Android apps is a great decision. It's fun, and it's an in-demand skill for the tech industry. In this tutorial, you'll learn how to build the simplest Android app: Hello World. It's a great place to start making Android apps.

Before you get started with this tutorial, you should have:

  • a basic understanding of object-oriented programming and the Java programming language
  • a computer running Windows, Linux, or macOS

Android Studio is the official integrated development environment for native Android app development. It comes with all the tools necessary for developing apps such as a powerful code editor, debugger, testing support, and performance tools. It also comes with a Gradle-based build system, fast emulators, code templates, and much more.

Android Studio is available for Windows, macOS, and Linux platforms, and you can download it from  the Android Studio website.

Proceed to download the appropriate Android Studio distribution file and install it according to your operating system. 

  1. For a  .exe file (recommended), double-click to launch it.
  2. For a  .zip file, unpack the ZIP and copy the  android-studio folder into your  Program Files folder. Open the  android-studio/bin folder and launch the  .exe file.
  3. Follow the setup wizard guide to complete the installation. 
  1. Unpack the  .zip file you downloaded to  /usr/local for your user profile, or  /opt for shared users.
  2. To launch Android Studio, open a terminal, go to the  android-studio/bin directory, and run  studio.sh.
  3. Choose whether to import previous Android Studio settings or not, and then click  OK.
  4. Follow the setup wizard guide to complete the installation. 
  1. Launch the Android Studio DMG file.
  2. Drag and drop  Android Studio into the  Applications folder, and then launch Android Studio.
  3. Select whether to import previous Android Studio settings, and then click  OK.
  4. Follow the setup wizard guide to complete the installation. 
 
Advertisement

First, here's a peek at what we'll be building. The completed Android hello world application will look like this:

completed hello world app

To create this simplest Android app, just follow along with the steps in this tutorial.

Launch Android Studio, and you should see a welcome page, as shown below. 

Android homepage

On the welcome page above, click  Start a new Android Studio project. The next window presents the activities page, as shown below.

Android activities

Android Studio provides activity templates to help you get started. For the Hello World project, choose  Empty Activity and click  Next.

An activity is a crucial component of any Android app. It provides a screen with which users can interact to perform activities, such as making a phone call, taking a photo, or sending a message. Each Activity has a window in which to draw its user interface. The window typically fills the screen, but it may be smaller than the screen and float on top of other windows.

You can  learn more about Activities and many other topics in Android development in the  Android From Scratch series.

  • ANDROID
    Android From Scratch: Activities and Fragments
    Paul Trebilcox-Ruiz

We'll finish creating the project by configuring some details about its name, location, and the API version it uses.

configure android project
  • Change the name of the application.
  • Change the default  Project location to your preferred directory or just leave it as the default location.
  • On the  minimum API level, ensure that  API 15: Android 4.0.3 IceCreamSandwich is set as the Minimum SDK. This ensures that your application runs on almost all devices.

Click  Finish.

Each time you create a new application, Android Studio creates a folder for your projects and builds the project with its Gradle system. The Gradle process may take a few moments. Gradle is Android's build system, which is responsible for the compilation, testing, and deployment of code. It makes it possible for the app to run on the device.

Whenever you start a new project, Android Studio creates the necessary folder structure and files for that app. Let's look at the different files involved in an Android app project.

project overview

The  manifests folder contains the  AndroidManifest.xml file. The manifest file describes essential information about your application. 

This folder contains the Java source code files. As you can see from the editor window above, the  MainActivity.java file contains the Java source code for the app's main Activity.

This folder includes all non-code resources, such as:

  • layouts: Layouts are XML files that define the architecture for the UI in an Activity or a component of a UI. For example, in our application, the  activity_main.xml file corresponds to the main Activity.
  • values: Contains the color, style, and string XML files for the application.
  • drawable: This is a catch-all for graphics that can be drawn on the screen, e.g. images.
  • build.gradle: This is an auto-generated file which contains details of the app such as the SDK version, build tools version, application ID, and more.

Now that you have a general view of the project structure, let's describe the most critical component files that constitute the hello world application. 

The main activity is the first screen that will appear when you launch your app.

Each Activity represents a screen of the Android app's user interface. Each Activity has a Java (or Kotlin) implementation file and an XML layout file.

Below is the default Java code generated by the application for the main activity.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
package com.example.helloworldapplication;
 
import androidx.appcompat.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);
     }
}

You don't need to understand this code fully. A lot of it is boilerplate and will be the same for any app. The code defines a  MainActivity class and, along with the  onCreate method, defines what happens when the Activity is created. In this case, it simply initializes the view of the Activity with the layout from the  activity_main layout file.

XML files are used for layouts. The main Activity layout XML file is found in the project's  /app/src/main/res/layout directory. Layout files are named after what they represent. For example, the Hello World application has one layout, which is the   activity_main.xml named after the main Activity.

Here is the default  activity_main.xml layout. It contains one text view element, with the text  Hello World!

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
Activity.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".MainActivity" >
 
    < TextView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "Hello World!"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintLeft_toLeftOf = "parent"
        app:layout_constraintRight_toRightOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />
 
</ androidx.constraintlayout.widget.ConstraintLayout >

As you can see, we don't need to change much to complete our Hello World app, but we'll make a small change so that the text stands out better—we'll change the text colour and font size.

01
02
03
04
05
06
07
08
09
10
< TextView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "Hello World!"
        android:textSize = "50dp"
        android:textColor = "#800000"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintLeft_toLeftOf = "parent"
        app:layout_constraintRight_toRightOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />

The  strings.xml file provides text strings for your application. For example, a default strings file looks like this: 

1
2
3
4
5
STRINGS
 
< resources >
    < string name = "app_name" >Hello World Application</ string >
</ resources >

If you want to change your app name, you can do it here.

Connect your Android device to your computer with a USB cable. The right connection type should show up as  connected as a media device.

You'll also need developer options enabled on your device. If this is not already enabled, follow these steps (this will work on most Android devices):

  • Open up the  Settings menu on your Android phone and scroll to the bottom. 
  • Tap  About phone and scroll down again until you see the  Build number option.
  • Tap the Build number multiple times. Soon, you should see a pop-up that reads something similar to  You are five taps away from being a developer.
  • Keep tapping until the pop-up says you're a developer. 
  • Go back to the main  Settings  > System > Advanced. Developer options should be the second-last option. Turn the Developer options on.

In Android Studio, navigate to the top menu and select  Run 'app'. Android Studio will show a dialog where you can choose which device to run your Android app on. Choose your connected device and click the  OK button.

The Hello World application should now be running on your phone. From here, you can make modify your app to whatever you want and add more features.

hello world app

In this tutorial, I showed you how to install Android Studio and create your first app: Hello World, the simplest Android app. After following this tutorial to create your first Android app, you are on your way to a promising career in developing apps!

To learn more about Android programming, check out some of our  other Android tutorials here on Envato Tuts+.

  • Android From Scratch: Exploring Android Sample Apps

  • How to Learn Android App Development: Start With These Courses and Tutorials

  • How to Get Started Making Android Apps

  • Understanding the Android App Development Kit

  • The Best Free To-Do List Apps for Android and How to Make One Yourself

  • Build a 360-Degree Video Player App With an Android App Template

 

 
Advertisement
 
Advertisement
LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?
Envato Market has a range of items for sale to help get you started.
WordPress Plugins
From $5
PHP Scripts
From $5
JavaScript
From $3
Mobile App Templates
From $5
Unlimited Downloads
From $16.50/month
Get access to over one million creative assets on Envato Elements.
Over 9 Million Digital Assets
Everything you need for your next creative project.
Create Beautiful Logos, Designs
& Mockups in Seconds
Design like a professional without Photoshop.
Join the Community
Share ideas. Host meetups. Lead discussions. Collaborate.

 
Advertisement
 
Advertisement
Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.