Android Tutorial: How to Develop an Android App
Tags: android, java, kotlin
Published: 2020-03-03 18:39:26

**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](https://proandroiddev.com/theres-never-been-a-better-time-to-learn-android-development-d1724409bdfc) 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:**
* What is Android
* Android development tools 
* How to develop an Android app
* Resources for Android app development 

___

## What is Android?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wj2cnbs9epus4ltghiuf.png)

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

<br>

### Programming Language Skills

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e2xrnp5kjhpklhtegdw5.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q7n4sfua8d00e6zzo487.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3cir8c6z6ntpo4ctqbnt.png)

> *If you want to learn more about Kotlin, take a look at our article [here](https://www.educative.io/blog/how-to-learn-kotlin-a-guide-for-beginners-and-android-developers) 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.
 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vfx7cllpud1trmwmmblo.png)

<br>

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

<br>

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

<br>

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

Before you can start your Android dev journey, you need to get up to speed on Java. 
If you’re looking to get started with Java, check out Educative’s [free courses](https://www.educative.io/become-a-developer/java). 

### Install Android Studio IDE

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mybjnecxyz8knvapupc4.png)


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](https://developer.android.com/studio/install). 


**To install Android Studio on Windows, follow these steps.**

1. Visit [this link](https://developer.android.com/studio/install) 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](https://developer.android.com/studio/install) 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.



> *If you aren’t ready to download Android Studio IDE, you can also check out [Educative's course](https://www.educative.io/collection/6100126873419776/6133287401553920) with a unique pre-configured Andoird widget.* 

___


## Creating a Hello World Application 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8vf29tld0l3o0vp0kydu.png)

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


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qa111v12dfrf0kb58pz1.png)

<br>

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

<br>

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hjrvwkc5dcepwtuhs1t2.png)



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](https://mvnrepository.com/).

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

<br>

**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'
```


<br>

**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'
```


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nz1bbzkimwrfgy80t6qt.png)



<br>

**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](https://material.io/develop/android/components/dialog/). 
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. 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kdsdxj7ijd0g7c5nkcwi.png)

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](https://developer.android.com/guide/components/activities/activity-lifecycle).* 

___

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nvu3yqfl5px7amvct8ct.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/foqe4cx96rxwyc3wne20.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v9cdt45jkw1bh24el28s.png)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uu7fm3f12ew492zoe1qt.png)

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. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/80q0n37pfm9vdxcl2ory.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o847qy2g2vyo2w1tjwqs.png)


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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bqty59ej9qsh3ca04oo1.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fz1n2v3uh24g4l0969qu.png)

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 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eshnccx2rhtsc0dwvhfd.png)

### Official Resources from Google and Android

* [Official Android Guide](https://developer.android.com/guide): a step-by-step guide to building an application

* [Official Android Samples](https://developer.android.com/samples): learn by exploring other projects built with Android 

* [Official Android Documentation](https://developer.android.com/reference/): list of libraries and in-depth technical explanations 

* [Google Android Glossary](https://developers.google.com/android/for-all/vocab-words/): list of new terms and vocab, interactive glossary 

### Learn the Basics
* [XML Basics](http://www.xmlnews.org/docs/xml-basics.html): beginner’s guide to XML, used for design and layouts
Java from Scratch 

* [Java for Android (cheatsheet)](https://medium.com/rocknnull/effective-java-for-android-cheatsheet-bf4e3433889a): a specialized book for writing Java code for Android 

* [Kotlin Crash Course for Programmers](https://www.educative.io/courses/kotlin-crash-course-for-programmers): online course for Java developers to get up to speed with Kotlin for Android

* [Learn Java from Scratch](https://www.educative.io/courses/learn-java-from-scratch): a free online course that covers all the basics of Java

* [Android Guidelines](https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md): best practices and basic guidelines from GitHub

### For intermediate/advanced developers

* [Developing Android Apps](https://www.udacity.com/course/new-android-fundamentals--ud851): Google course designed to boost Android skills

* [Common Design Patterns for Android](https://www.raywenderlich.com/470-common-design-patterns-for-android-with-kotlin): learn new ways to solve problems in Kotlin 

* [The Google Play Store App Pre-Release Checklist](https://techbeacon.com/essential-google-play-store-app-pre-release-checklist): learn how to make smoother launches 

* [Android Development Best Practices](https://blog.mindorks.com/android-development-best-practices-83c94b027fd3): learn what aspects of Android you should use and avoid when it comes to best practices 

<br>

## Want to get started with Android development right now?

One of the best resources out there for developers of all levels is [Modern Android App Development with Java](https://www.educative.io/collection/6100126873419776/6133287401553920), 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!

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! 

*Happy learning!*