Beginning Android Development with Kotlin, Part Two: Using Android Studio

In this Android Studio tutorial, you’ll learn the fundamental concepts of developing with Android Studio using Kotlin by creating an app to read your fortune. By Kevin D Moore.

Leave a rating/review
Download materials
Save for later
Share
Update note: Kevin Moore updated this tutorial for Android Studio version 3.3.1 and Kotlin. Matt Luedke wrote the original.

Android Studio is an IntelliJ IDEA-based IDE, and it’s the official IDE for Android app development.

In this Android Studio tutorial, you’ll learn how to use the tools that every Android developer uses by creating a simple fortune telling app. You’ll learn to use some of Android Studio’s key features by:

  • Navigating through different files in your project using Project Explorer.
  • Working with the AndroidManifest.xml file.
  • Learning about the Gradle build system.
  • Importing files into your project.
  • Learning about the rich layout editor with dynamic layout previews.
  • Using Logcat and the Android Monitor to debug your app.

Note: This tutorial assumes that you’ve already installed Android Studio 3.2.1 or later and have set up an emulator or a device configured for testing. If you haven’t, please refer to our previous tutorial about installing Android Studio to get up and running in no time!

Getting Started With Android Studio

You’ll start by creating a brand new Android app that you’ll use to explore Android Studio and to learn about its capabilities and interface.

For bonus points, you’ll also walk away as a bona fide fortune teller — or something to that effect. At least you’ll have a fun app to play around with!

Fire up Android Studio and, in the Welcome to Android Studio window, select Start a new Android Studio project.

In the Choose your project window, select the Basic Activity type and press Next.

In the Configure your project window, set the Name to Fortune Ball, enter a Company domain of your choosing and select a convenient location to host your app in the Save location field. Also, make sure that the language is Kotlin and the minimum API is 21. When you’re done, click Finish.

Within a short amount of time (hopefully seconds!), you’ll land on a screen that looks similar to this:

First screen

Build and run your app and you should see a similar screen on your device or emulator. Note that the emulator acts like a device, so it will need time to boot and load.

First run Emulator

Voila, that’s an app! There’s not much to it, but it’s enough to let you dive into the next section.

Project and File Structure

For this portion of the tutorial, you’ll focus on the highlighted section of the screenshot below. This window shows the project files of your app. By default, Android Studio filters the files to show Android project files.

Project Tab

After you select the file drop-down menu as illustrated in the screenshot below, you’ll see several options to filter the files. The key filters here are Project and Android.

The Project filter will show you all of the app modules. You have to have at least one app module in every project.

Other types of modules include third-party library modules or other Android app modules such as Android wear apps, Android TV, etc… Each module has its own complete set of sources, including a gradle file, resources and source files such as Kotlin files.

Project Filter

Note: If you don’t see the Project view, you can click on the Project tab on the left side panel, as indicated in the screenshot above.

The default filter is Android, which groups files by specific types. You’ll see the following folders at the very top level:

  • Manifests
  • Java
  • Res
  • Gradle Scripts

Android Filter

Note that the source folder is named java even if you’re using Kotlin. You’ll take a deeper dive into each of these folders, starting with manifests, in the next section.

Overview of AndroidManifest.xml

Every Android app has an AndroidManifest.xml in its manifests folder. This XML file lets your system know what the app’s requirements are. It must be present for the Android system to build your app.

Go to your app’s manifests folder and expand it to select AndroidManifest.xml. Double-click on the file to open it.

Android Minfest

The manifest file must have manifest and application tags in it. Each must appear only once.

In addition to the element name, each tag also defines a set of attributes. For example, some of the many attributes in the application tag are: android:icon, android:label and android:theme.

Other common elements that can appear in the manifest include:

  • Uses-permission: Requests a special permission that the app must have in order for it to operate correctly. For example, if an app must request permission from the user in order to access the Internet, you must specify the android.permission.INTERNET permission.
  • Activity: Declares an activity that implements part of the app’s visual user interface and logic. Every activity that your app uses must appear in the manifest. The system won’t see undeclared activities and, sadly, they’ll never run.
  • Service: Declares a service that you’ll use to implement long-running background operations or a rich communications API that other apps can call. An example is a network call to fetch data for your app. Unlike activities, services have no user interface.
  • Receiver: Declares a broadcast receiver that enables apps to receive intents broadcast by the system or by other apps, even when other components of the app are not running. One example of a broadcast receiver is when the battery is low and you get a system notification within your app; you can then write logic to respond.

You can find a full list of tags allowed in the manifest file here on the Android Developer site.

Configuring the Manifest

Right now, your app is an excellent example of a framework, but it’s a terrible fortune teller. You’re here because you want to learn how to play around on Android. That’s convenient, because now you’ll need to make some changes to the manifest so that you can look into the future.

Under the activity tag in MainActivity, add the attribute android:screenOrientation="portrait" to restrict the screen to portrait mode only.

If this attribute is absent, the screen will transform to landscape or portrait mode depending on the device’s orientation. After adding this attribute, your manifest file should look like the screenshot below:

Screen Orientation

When you’re done, build and run the app. Rotate your phone or emulator and notice that the screen won’t switch into landscape mode, as you’ve restricted this capability in the manifest file.