• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/21/19

Launch Your Application on an Emulator or Device

Log in or subscribe for free to enjoy all this course has to offer!

You've typed some code, great. But in a literal sense, what good is it? This chapter shows you how to launch your application. You have two options: either use an emulator if you don't have an Android device, or use your phone (or tablet) if you have one.

Using the emulator lets you quickly generate different configurations with different screen sizes, limited RAM, or when an older version of Android has been installed. Beyond that, nothing beats a test on an actual device to ensure the best possible user experience.

Launch on an Emulator

To launch the application, simply click on the green triangle located on the IDE bar:

The following window should appear:

Choose a target
Choose a deployment target

Create a Virtual Device

To launch the application on an emulator, you have to create a virtual device. A virtual device corresponds to a real-world architecture (the hardware part) and a version of Android (the software part). To do this, click the Create New Virtual Device button.

In the left window pane, choose the hardware type. In our case we will use Phone, but in the future, you can choose Tablet if you would like to test the application on a larger virtual device.

The middle pane lists all available emulated devices. Each device has specific hardware characteristics (screen size, resolution, available memory, etc.). If you wanted to emulate a very specific device (with a large screen or little memory), you could create it by clicking on the New Hardware Profile button. For today, select the Nexus 5, which will do just fine. Then click the Next button.

Finally, the system image corresponds to the version of Android installed on the virtual device. I recommend staying in the Recommended tab. 😉 You may need to install a system image to proceed.  In that case, click the Download link next to the image that you want to install.

Android O, or API version 26, is the latest image available
Android O, or API version 26, is the latest image available

When you want to install a new image, a window will appear. You have to click on each license in the left pane, then accept it by toggling the appropriate radio button at the bottom of the window (not easy to spot). Then click Next. If you don't accept the license, the Next button will remain disabled (and yes, you should read the license agreement).

Mandatory license agreements? Yes, please!
Mandatory license agreements? Yes, please!

The Component Installer screen should appear. It will include the estimated time to download the necessary files. Since the files are large, you have plenty of time to go make a coffee, or a tea, or a expertly crafted sandwich. After installation completes, click the Finish button.

Back on the System Image screen, click the Next button to arrive on the Android Virtual Device (AVD) screen. This screen allows you to check the configuration of your virtual device or even change some parameters. For now, I suggest you keep the default settings. Click Finish.

Launching

You should now see your new virtual device appear in the window. If the window is not displayed, click the green play button we revealed at the beginning of this chapter.

Wow, there's our app in the emulator!
Wow, there's our app in the emulator!

When launching the application on an emulator, by default the application will focus on the input field. You should see the cursor positioned and flashing at "Please type your name." You can use your computer keyboard to enter your name (if you want to display the virtual keyboard, click on the input field with your mouse).

You can move focus to the next interface element (in this case, the button) by pressing the tab key. Once highlighted, press the enter key to simulate a button click. The button animates to indicate the click, but nothing else happens (we haven't coded that behavior yet). Navigating by keyboard helps you test forms and other step-by-step interface experiences.

Launch on a Real Device

Nothing beats a test on a real device! Okay, maybe a hi-five from Weird Al Yankovic, but he's too busy to hi-five all of you. So, if you're lucky enough to have an Android device (phone or tablet), it's time to plug in your USB cable and connect it to your computer.

Enabling Developer Mode

To let Android Studio communicate with your device, enable developer mode. To do so, follow these instructions.

Then you must allow your operating system to detect your device. If you are on macOS, you're good to go. If you are on Windows or Linux, follow the Set up a Device for Development instructions.

Launching

If the application is already running on the emulator, stop it by clicking on the Stop button (red square on the Android Studio toolbar).

Click here to stop your application
Click here to stop your application

Then click the Launch button again. You should see the following window, which lets you select whether you want to launch the application on the emulator or your device. Select your device and click the OK button.

The Instant Run feature

Historically, testing any changes to your source code forced Android Studio to recompile your entire application and install it back on the emulator or device. This process was slow (even after repeated build cycles).

Actually no, there are clearly worse things in this world like poverty and hunger so yeah. It was like, #3
Actually no, there are many things worse than Android's ancient deploy cycle: poverty, hunger, the gender wage gap. But it's definitely in the top five. 

Fortunately, Android Studio v2.3 made it possible to pass your changes to your running application without recompiling the whole thing. This allows us to test code changes quickly. If you launch the application for the first time on your device, you might see the window below:

Click Install and Continue. A new window appears (Component Installer) to install the missing files. Press the Finish button, and the application should launch.

From now on, you can click Apply Changes in the toolbar to apply the changes without having to recompile everything.

If you want to know more about Instant Run, click here.

R Class

As we mentioned in the previous chapter, when compiling your application, Android Studio generates a Java class named R. This class identifies all project resources with a unique, static integer.

If you want to better understand its use and see what it looks like, select the Project Files view on the left and open the file in the tree shown below. You will see identifiers which you defined previously in your layout file, for example activity_main_name_input.

You should never edit the file, but can study it for... fun?
You should never edit the file, but can study it for... fun?

Let's recap!

Now you know:

  • How to launch your application on an emulator or a real device 

  • The Java class "R" retains unique identifiers for all Android project resources 

Sweet! Remember that your users will own a variety of phones: so don't hesitate to use your emulator to test on an older version of Android, on a device with a smaller screen, etc.

Example of certificate of achievement
Example of certificate of achievement