Android Tutorial: How to Develop an Android App

Amanda Fawcett
Mar 02, 2020
editor-page-cover

Android development is all the hype these days as it continues to dominate the world of mobile development. Fun projects, great pay, and tons of job prospects are just some of the reasons developers are starting their journeys into the exciting world of the Android operating system.

Some experts say that there has never been a better time to learn Android skills, especially since the recent updates, like the addition of Kotlin and improvements to Google’s policies.

If you’ve been thinking about starting your Android journey or you’re just curious about what Android has to offer, you’re in the right place! Today we will walk you through all the basics of Android development and even show you how to build your own functioning application.

Here’s what we’ll cover today:



Become an Android developer the easy way.

Learn modern Android development with a hands-on, project-based course that walks through every stage of development.

Modern Android App Development with Java


What is Android?

Android is one of my world’s most popular operating systems for everything from 5G mobile devices to mobile apps to touchscreen smartphones and tablets. This open-source, Linux-based software is used by Google to power over 2.5 billion devices worldwide, accounting for over 80% of smartphone sales.

Android is based on the Linux kernel, which means that the basic operating structure is portable, multi-user, and able to handle complex multitasking. One of the biggest advantages of Android is the freedom of choice that comes with the technology. Not only is the hardware more diverse, but the software is very flexible and customizable.

The market for Android development is growing as more and more manufacturers, including Samsung, Lenovo, HTC, and LG, turn to Android to power their products. This means that there’s a huge demand for Android developers worldwide in diverse industries and companies!

On top of that, the new Google Play Store policies are the app development market much more lucrative. Learning Android skills will open doors and make you a desirable developer across the board.


Android development tools

Getting started as an Android developer is easier than you might think; you’ll need to master some basic skills and tools, such as:

  • Programming language skills (Java, Kotlin, and Groovy)
  • XML
  • Android build system
  • Android Studio IDE

Programming Language Skills

There are three programming languages and one markup language used in Android development.

Java is the official language for Android development and one of the most popular programming languages in the world. Java classes run on Android Runtime (ART), a specialized virtual machine. Take a look at this example from the Android MainActivity.java file.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mainTextView = findViewById(R.id.mainTextView);
        mainTextView.setText("Hello educative.io");
    }
}

Kotlin has been the second official language for Android development since 2017. Known for being much more concise and expressive, Kotlin helps alleviate some of Java’s drawbacks. Here’s an example of the above code in Kotlin so you can compare.

public class MainActivity : AppCompatActivity {

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val mainTextView = findViewById<TextView>(R.id.mainTextView)
        mainTextView.text = "Hello educative.io"
    }
}

If you want to learn more about Kotlin, take a look at our article here to catch up on the basics.

XML, a markup language, is commonly used in Android development to declare a layout for user interface (UI), dimensions, and strings. Take a look at this example of a layout that shows text in the middle of the user’s screen.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

Android Build System

Gradle powers the Android build automation system and expands upon the concepts of Apache Maven and Apache Ant by introducing a Groovy-based, domain-specific language. Groovy is an optionally typed, dynamic language with static-typing capabilities. It helps improve productivity and speed by integrating with your Java-based program. With Groovy’s ease and Gradle’s mature ecosystem, you can automate your software and deliver much faster builds.


Android Studio IDE

The Android Studio IDE is your new best friend for Android development. Based on IntelliJ IDEA, it’s the official development environment for Google’s operating system. It comes with great Android-specific tooling to cover all your needs. Using this IDE will accelerate your development time, and frequent updates mean you’ll never fall behind. It comes loaded with the following features, amongst many others:

  • Realtime profilers and analyzer
  • Flexible build system
  • Intelligent code editor
  • Visual layout editor
  • Fast emulator

Android SDK

The Android SDK is the official development kit for Android app development. It is composed of modular packages that can be separately downloaded from the Android SDK Manager, including SDK tool, Google API, Android support, Android Debug Bridge (ADB), and more. Just like the IDE, the Android SDK is always being updated. New releases will keep you up to date with the latest features.


How to develop an Android app

Java for Android

When it comes to creating Android apps, your knowledge of Java is paramount to your success. Java is the official language for developing Android applications, and it supports all of the Android tools. Knowledge of this language will make your dev experience much easier.

Java was chosen for Android development because it is well-known, well-supported by development tools, and already pervasive in the mobile phone industry. On top of that, Java runs in a VM, so it doesn’t need to be recompiled.


Install Android Studio IDE

To get started with Android development, you need to install the Android Studio IDE. This user-friendly, drag-and-drop interface is the official IDE development environment. It is purpose-built for high-quality Android apps. This IDE will speed up your development time and make your apps far more reliable and easier to update when new features are released.

For Linux or Chrome OS installation, visit the documentation here.

To install Android Studio on Windows, follow these steps.

  1. Visit this link to get the latest version of Android Studio.

  2. You can either download the IDE as a .exe file or a .zip file. For the .exe file, double click the file to launch it. For the .zip file, unpack the ZIP and copy the android-studio folder to your Program files.

  3. This will prompt you to open and launch the android-studio > bin folder.

  4. Once prompted, follow the Android Studio setup wizard, where you can select your SDK packages.

To install Android Studio on Mac, follow these steps.

  1. Visit this link to get the latest version of Android Studio.

  2. Once downloaded, launch the DMG file and drag it to your Applications folder.

  3. Launch Android Studio. From here, you can either start a new project or import previous settings.

  4. Follow the setup wizard prompts to select your SDK components.


Keep the learning going.

Learn Android developmemnt without scrubbing through videos or documentation. Educative’s text-based course is easy to skim and features a live Android emulator - making learning quick and efficient.

Modern Android App Development with Java


Creating a Hello World Application

Step 1: Structure of an Android App

Now that we have our IDE, how do we actually make an Android project? First, let’s look at the structure of a typical Android project.

  • app - root module folder

    • build.gradle - module config file
    • src/main/AndroidManifest.xml - module manifest file
    • src/main/java - module source folder for Java or Kotlin files
    • src/main/res - module resource folder
  • build.gradle - project config file

  • gradle, gradle.properties, gradlew, gradlew.bat - Gradle related files for to build Android project

  • settings.gradle - project settings file


Project Files

The settings.gradle file contains a list of your modules and project name. Keep in mind that an Android project can consist of one or several modules, which can each contain their own feature or logic. The gradle.properties file defines your settings and configures a build environment.

The gradle, gradlew, and gradlew.bat files are related to Gradle wrapper, so we don’t have to manually install Gradle.

build.gradle is a top-level build file. Here we can add configuration options shared by all modules. For example, you can give your files access to repositories for core Android functionalities.


Module files

Each module has a unique name where we put the application source code. The module build.gradle file contains the configurations related to this module only, such as:

  • compileSdkVersion - the version of Android SDK to compile the project
  • minSdkVersion - the minimal supported Android version
  • targetSdkVersion - the target version of Android SDK, used to tell the system to enable compatibility behaviors
  • applicationId - unique identifier of the application on the device and in Google Play Store
  • versionCode - an internal version number
  • versionName - the version name displayed to users
  • compileOptions - compile options to achieve some features of Java 1.8
  • dependencies - first-party and third-party library dependencies, discussed in the next lessons
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.travelblog"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    // ui
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-alpha10'
}

The AndroidManifest.xml is where we declare our main components. For example, a manifest file for a travel blog might contain the following things:

  • package - the package name of the application, in our case com.travelblog
  • theme - the global application theme, in our case MaterialComponents theme
  • label - the label which is used as a value for the application icon
  • activity - the activity, we currently only have one MainActivity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.travelblog">
    <application
        android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"
        android:label="Travel Blog">
        <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>

All resource-related files need to be placed inside predefined, sub-folders of the src/main/res folder. One subfolder, for example, is the layout folder for all your layout files. We will also have the src/main/java folder for our Java source code.


Step 2: Android Libraries

An important part of creating your application is the unique features and tools you add to it. That’s where libraries come into play. A library is a collection of pre-written resources that can be added to your app. The Android library ecosystem is large, and you can use dozens of libraries in a single project. You can access most of the Android libraries through maven.

Adding a library to your project is easy: declare the group id, artifact id, and version in the dependencies section of your app/build.gradle file.

dependencies {
    // ui
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-alpha10'
}

Here are some of the most popular libraries in use today:

  • appcompat - makes the apps developed with newer versions work with older versions
  • constraintlayout - allows creating large and complex layouts with a flat view hierarchy
  • material - brings material design components to Android
  • retrofit - a type-safe HTTP client library
  • moshi - a JSON parser library
  • glide - an image loading library
  • room - an official Android ORM database
  • dagger - a static, compile-time dependency injection framework

Appcompat

The appcompat library is great for solving compatibility issues between newer and older versions of your app. Its primary component is AppCompatActivity. This base class enables backward compatibility with older versions of Android apps. To add it to your app, use the following code:

implementation 'androidx.appcompat:appcompat:1.1.0'

Constraint layout

This library enables you to create complex layouts using a flat view hierarchy. It is common to use ConstraintLayout as the root of all the layout files. To add it to your app, use the following code:

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
widget

Visual representation of constrains


Material design

This library brings Material Design components to your app. Material design is a design language used to make your various components more user-friendly. You can take a look at the list of components here. To add it to your app, use the following code:

implementation 'com.google.android.material:material:1.1.0-alpha10'

Step 3: Android Activity

One of the core components of Android is activity, one screen of the application user interface. An application is comprised of multiple activities that can be launched on top of each other to form a back stack. A user can navigate through this back stack using the UI components, i.e. a back button.

For example, an app may have the following components:

  • LoginActivity - represents login flow
  • ListActivity - represents a list of recent article titles
  • DetailActivity - represents an article itself

Activities have their own lifecycles, so the Activity class offers six core callbacks: onCreate( ), onStart( ), onResume( ), onPause( ), onStop( ), onDestroy( ). When the user leaves an activity, the system will dismantle the activity by calling different methods. You can use these methods to check when an activity is being created or destroyed, becomes visible or hidden, etc.

widget

Image source: Applications Development

Creating an activity involves two main steps: make a Java class and extend it from the Activity superclass. You could then use the library AppCompatActivity to achieve backward compatibility. Android activities must then be declared in the AndroidManfiest.xml file.

If you want to learn more about Android activity lifecycles, take a look at the official Android guide.

Here’s an example for a travel blog app:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.travelblog">
    <application
        android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"
        android:label="Travel Blog">
        <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>

Step 4: Android Layout

Another key aspect of Android development is developing and working with Android layout. The layout defines the overarching structure of your UI (user interface). These are built using views and view groups.

Views, also called widgets, might be components such as TextView (render text), EditText (user can type text), and Button (clickable text).

ViewGroups, sometimes called layouts, are like invisible containers that determine where certain elements will be housed. This is where you might use the Google library ContraintLayout, which uses constraints to position your widgets. The Android SDK method is simpler for beginners but offers less flexibility.

The easiest way to build a layout is by using an XML file rather than Java code. We can then bind or inflate this layout to an activity. Let’s build a layout to see how it’s done.

First, inside your app/src/main/res/layout folder, create an activity_main.xml layout file. In this example, we will use a root layout through ConstraintLayout alongside some XML attributes:

  • layout_width="match_parent": this defines the width of the layout.
  • android:layout_height="match_parent": this defines the height of the layout
  • xmlns:android and xmlns:app: these define XML namespace, Android namespace for attributes from Android SDK, and app namespace for attributes from libraries
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

Secondly, we need to fill our empty layout by defining a child view. In this case, we will define static text that reads “Hello World”. To do so, we use a TextView and the text attribute. We will use wrap_content so the view will take up as much space as possible.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now that we’ve built our views, we move onto alignment. We want to move our text to the center of the screen since the default positions our views in the upper left corner. To do so, let’s add the following constraints:

  • layout_constraintTop_toTopOf: this declares a constraint to align the top of the view to the top of the ConstraintLayout
  • layout_constraintBottom_toBottomOf: this declares a constraint to align the bottom of the view to the bottom of the ConstraintLayout
  • layout_constraintLeft_toLeftOf: this declares a constraint to align the left of the view to the left of the ConstraintLayout
  • layout_constraintRight_toRightOf: this declares a constraint to align the right of the view to the right of the ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

Preview of alignment

Now that everything is aligned where we want it, we move onto layout binding. This serves to associate activity_main.xmllayout with the MainActivity. We do this using the setContentView method when an activity is created inside the onCreate method.

The method setContentView accepts the layout resource ID. This is referenced by the auto-generated Android R class, where all the resource IDs are stored. For binding purposes, we can use the R.layout.activity_main to obtain the ID of activity_main.xml so we can tell MainActivity to render layout from this file.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The final step is view binding, which enables us to interact with views on runtime. To do so, we bind the view from XML to Java object.

First, we define a new ID for the TextView using the id attribute with @+id/mainTextView value.

<TextView
        android:id="@+id/mainTextView"
        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" />

We can now bing the TextView from XML to Java object using the findViewById method. The setText method will change our text to make it interactive.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mainTextView = findViewById(R.id.mainTextView);
        mainTextView.setText("Hello educative.io");
    }
}
widget

Preview of Android app structure

Now you know the basic build and layout process for an Android application! It’s time for you to get started on your own! In the next section, we’ll walk you through some important resources to get you started.


Resources for Android Development

One of the best resources out there for developers of all levels is Modern Android App Development with Java, a hands-on, project-based course that walks through every stage of development. As you learn, you’ll build a fully functional Travel Blog Application.

On top of that, the course comes with Educative’s unique pre-configured Android environment, so you don’t have to download anything to get started. It’s one of the only courses out there with this powerful widget!

widget

The course begins with a basic intro and moves through each stage of development, including…

  • Intro to Android
  • Login screen
  • Details screen
  • List screen
  • Search and sort
  • Offline functionalities
  • Additional resources
  • and more

It’s never been easier to get started with Android development to propel your career into the future!


Continue reading about app development

WRITTEN BY

Amanda Fawcett