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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Intents Tutorial with Kotlin
30 mins
Exploring the Extras
The third block of code in your method adds an Extra to your newly created intent.
What’s an extra, you say?
Extras are a form of key — value pairs that give your intent additional information to complete its action. Just like humans are more likely to perform better at an activity if they are prepared for it, the same can be said for intents in Android. A good intent is always prepared with the extras it needs!
The types of extras an intent can acknowledge and use change depending on the action. This is similar to the type of data you provide to the action.
A good example is creating an intent with an action of ACTION_WEB_SEARCH
. This action accepts an extra key value called QUERY
, which is the query string you wish to search for. The key for an extra is usually a string constant because its name shouldn’t change. Starting an intent with the above action and associated extra will show the Google Search page with the results for your query.
Look back at the captureIntent.putExtra()
line. EXTRA_OUTPUT
specifies where you should save the photo from the camera. In this case, the Uri
location of the empty file you created earlier.
Putting Your Intent in Motion
You now have a working intent ready to go, along with a full mental model of what a typical intent looks like:
There’s not much left to do here except let the intent fulfill what it was destined to do with the final line of takePictureWithCamera()
. Add the following to the bottom of the method:
startActivityForResult(captureIntent, TAKE_PHOTO_REQUEST_CODE)
This line asks Android to start an activity that can perform the action captureIntent
to capture an image to a file. Once the activity has fulfilled the intent’s action, you also want to retrieve the resulting image. TAKE_PHOTO_REQUEST_CODE
, the constant you specified earlier, will be used to identify the intent when it returns.
Next, in the onClick()
function, replace the empty closure in the when
statement for the R.id.picture_imageview
branch condition with a call to the takePictureWithCamera()
function. The resulting line of code should look like the following:
R.id.pictureImageview -> takePictureWithCamera()
This calls takePictureWithCamera()
when you tap the ImageView
.
Time to check the fruits of your labor! Build and run the app. Tap the ImageView
to invoke the camera:
You can take pictures at this point, you just can’t do anything with them! You’ll handle this in the next section.
Note: If you are running the app in the Emulator, you may need to edit the camera settings on your AVD. To do this, click Tools\Android\AVD Manager and then click the green pencil to the right of the virtual device you want to use. Then, click Show Advanced Settings in the bottom-left of the window. In the Camera section, ensure all enabled camera dropdowns are set to Emulated or Webcam0. Also, allow the app to use the camera permission if prompted.
Note: If you are running the app in the Emulator, you may need to edit the camera settings on your AVD. To do this, click Tools\Android\AVD Manager and then click the green pencil to the right of the virtual device you want to use. Then, click Show Advanced Settings in the bottom-left of the window. In the Camera section, ensure all enabled camera dropdowns are set to Emulated or Webcam0. Also, allow the app to use the camera permission if prompted.
Implicit Intents
If you’re running the app on a physical device with a number of camera-centric apps, you might have noticed something unexpected:
You get prompted to choose which app should handle the intent.
When you create an intent, you can be as explicit or as implicit as you like with what the intent should use to complete its action. ACTION_IMAGE_CAPTURE
is a perfect example of an Implicit Intent.
Implicit intents let Android developers give users the power of choice. If they have a particular app they like to use to perform a certain task, would it be so wrong to use some of its features for your own benefit? At the very least, it definitely saves you from reinventing the wheel in your own app.
An implicit Intent informs Android that it needs an app to handle the intent’s action when it starts. The Android system then compares the given intent against all apps installed on the device to see which ones can handle that action, and therefore process that intent. If more than one can handle the intent, the user is prompted to choose one:
If only one app responds, the intent automatically takes the user to that app to perform the action. If there are no apps to perform that action, then Android will return nothing, leaving you with a null value that will cause your app to crash! :[
You can prevent this by checking the result to ensure that at least one app responded to the action before attempting to start it. Or, in this case, you can also state the app can only be installed on devices that have a camera by declaring the necessary hardware requirements by adding the following line to AndroidManifest.xml:
<uses-feature android:name="android.hardware.camera" />
The starter project opts for the device restriction method.
So you have an implicit intent set up to take a photo, but you don’t yet have a way to access that photo in your app. Your meme generator isn’t going to get far without photos!
Add the following new method just below takePictureWithCamera()
in TakePictureActivity
:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TAKE_PHOTO_REQUEST_CODE
&& resultCode == RESULT_OK) {
//setImageViewWithImage()
}
}
The above method only executes when an activity started by startActivityForResult()
in takePictureWithCamera()
has finished and returns to your app.
The if
statement above matches the returned requestCode
against the constant you passed in (TAKE_PHOTO_REQUEST_CODE
) to ensure this is your intent. You also check that the resultCode
is RESULT_OK
, this is simply an Android constant that indicates successful execution.
If everything does go well, then you can assume your image is ready for use, so you call setImageViewWithImage()
.
Time to define that method!
First, inside TakePictureActivity
at the top, add the following boolean
variable:
private var pictureTaken: Boolean = false
This tracks whether you have taken a photo, which is useful in the event you take more than one photo. You’ll use this variable shortly.
Next, add the following right after onActivityResult()
:
private fun setImageViewWithImage() {
val photoPath: Uri = selectedPhotoPath ?: return
pictureImageview.post {
val pictureBitmap = BitmapResizer.shrinkBitmap(
this@TakePictureActivity,
photoPath,
pictureImageview.width,
pictureImageview.height
)
pictureImageview.setImageBitmap(pictureBitmap)
}
lookingGoodTextView.visibility = View.VISIBLE
pictureTaken = true
}
BitmapResizer
is a helper class bundled with the starter project to make sure the Bitmap
you retrieve from the camera is scaled to the correct size for your device’s screen. Although the device can scale the image for you, resizing it in this way is more memory efficient.
With setImageViewWithImage()
now ready, uncomment this line that calls it, within onActivityResult()
:
// setImageViewWithImage()
Build and run. Select your favorite camera app, if prompted, and take another photo.
This time, the photo should scale to the appropriate size given your display and show up in the ImageView
:
You’ll also see a TextView
underneath that compliments you on your excellent photography skills. It’s always nice to be polite. :]