Android Intents Tutorial with Kotlin

In this Intents tutorial you’ll learn what Intents are, the role they play in Android, and how to use them to communicate with other installed apps. By Jenn Bailey.

Leave a rating/review
Download materials
Save for later
Share

Update note: This tutorial has been updated to Kotlin, Android 28 (Pie) and Android Studio 3.4 by Jennifer Bailey. The original tutorial was written by Darryl Bayliss. Previous updates by Artem Kholodnyi and Steven Smith.

Update note: This tutorial has been updated to Kotlin, Android 28 (Pie) and Android Studio 3.4 by Jennifer Bailey. The original tutorial was written by Darryl Bayliss. Previous updates by Artem Kholodnyi and Steven Smith.

People don’t wander around the world aimlessly. Most of everything they do — from watching TV, to shopping, to coding the next killer app — has some sort of purpose, or intent, behind it.

Android works in much the same way. Before an app can perform an action, it needs to know what that actions purpose, or intent, is in order to carry out that action properly.

As it turns out, humans and Android aren’t so different after all. :]

In this intents tutorial, you are going to harness the power of Intents to create your very own meme generator. Along the way, you’ll learn the following:

  • What an Intent is and what its wider role is within Android.
  • How you can use an Intent to create and retrieve content from other apps for use in your own.
  • How to receive or respond to an Intent sent by another app.

If you’re new to Android Development, it’s highly recommended that you work through Beginning Android Development and Kotlin for Android to get a grip on the basic tools and concepts. You’ll also need Android Studio 3.4 or later.

Get your best meme face ready. This tutorial is about to increase your Android Developer Level to over 9000!!! :]

Getting Started

First, download the Memeify project using the Download Materials button at the top or bottom of this tutorial. If you already have Android Studio open, click File\Import Project and select the top-level project folder you just downloaded. If not, start up Android Studio and select Open an existing Android Studio project from the welcome screen, again choosing the top-level project folder for the starter project you just downloaded. Be sure to accept any prompts to update to the latest Gradle plugin or to download the correct build tools.

Inside, you will find the XML Layouts and associated Activities containing some boilerplate code for the app, along with a helper class to resize Bitmaps and some resources such as Drawables and Strings that you’ll use later on in this tutorial.

Take some time to familiarize yourself with the project before you proceed. TakePictureActivity contains an ImageView which you can tap to take a picture using your device’s camera. When you tap LETS MEMEIFY!, you’ll pass the file path of the bitmap in the ImageView to EnterTextActivity. This is where the real fun begins. You can enter your meme text to turn your photo into the next viral meme! :]

Creating Your First Intent

Build and run. You should see the following:

1. Starter Project Load App

It’s a bit sparse at the moment. If you follow the TextView‘s instructions and tap the ImageView, nothing happens!

You’ll make it more interesting by adding some code.

Open TakePictureActivity.kt and add the following inside the companion object at the bottom of the class:

private const val TAKE_PHOTO_REQUEST_CODE = 1

This will identify your intent when it returns — you’ll learn a bit more about this later in the tutorial.

Note: This tutorial assumes you are familiar with handling import warnings and won’t explicitly state the imports to add. As a quick refresher, if you don’t have on-the-fly imports set up in Android Studio, you can import by pressing option + return on a Mac or Alt + Enter on a PC while your cursor is over a class with an import warning.

Note: This tutorial assumes you are familiar with handling import warnings and won’t explicitly state the imports to add. As a quick refresher, if you don’t have on-the-fly imports set up in Android Studio, you can import by pressing option + return on a Mac or Alt + Enter on a PC while your cursor is over a class with an import warning.

Add the following just below onClick(), along with any necessary imports:

private fun takePictureWithCamera() {
  // 1
  val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

  // 2
  val imagePath = File(filesDir, "images")
  val newFile = File(imagePath, "default_image.jpg")
  if (newFile.exists()) {
    newFile.delete()
  } else {
    newFile.parentFile.mkdirs()
  }
  selectedPhotoPath = getUriForFile(this, BuildConfig.APPLICATION_ID +
      ".fileprovider", newFile)

  // 3
  captureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
      selectedPhotoPath)
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
  } else {
    val clip = ClipData.newUri(contentResolver, "A photo", selectedPhotoPath)
    captureIntent.clipData = clip
    captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
  }
}

There’s quite a bit going on in this method, so look at it step-by-step.

The first block of code declares an Intent object. That’s all well and good, but what exactly is an intent?

Intent

An intent is an abstract concept of work or functionality that can be performed by your app sometime in the future. In short, it’s something your app needs to do. The most basic intents are made up of the following:

  • Actions: This is what the intent needs to accomplish, such as dialing a telephone number, opening a URL, or editing some data. An action is simply a string constant describing what is being accomplished.
  • Data: This is the resource the intent operates on. It is expressed as a Uniform Resource Identifier or Uri object in Android — it’s a unique identifier for a particular resource. Depending on the action, an intent may require a specific type of data. You wouldn’t want your dial number intent trying to get a phone number from an image! :]

This ability to combine actions and data lets Android know exactly what the intent means to do and what it has to work with. It’s as simple as that!

Smile

Head back to takePictureWithCamera() and you’ll see the intent you created uses the ACTION_IMAGE_CAPTURE action. You’ve probably already guessed that this intent will take a photo for you, which is just the thing a meme generator needs!

The second block of code focuses on getting a temporary File to store the image in. The starter project handles this for you. Take a look at the code in the activity to see how this works.

Note: You may notice the selectedPhotoPath variable being appended with a .fileprovider string. File Providers are a special way of providing files to your app to ensure it is done in a safe and secure way. If you check the Android Manifest you can see Memeify makes use of one. You can read more about them here.

Note: You may notice the selectedPhotoPath variable being appended with a .fileprovider string. File Providers are a special way of providing files to your app to ensure it is done in a safe and secure way. If you check the Android Manifest you can see Memeify makes use of one. You can read more about them here.