Lesson Monday

In the last lesson we learned about the Android Studio logcat, and how to record information to it using various Log methods. This process is similar to using the console.log() method we learned in JavaScript, but with a few expanded options and functionalities.

Do you remember using the JavaScript console and debugger; in Intro to Programming and JavaScript? Android studio also offers a feature like this to assist in tracking down bugs and other issues. It can pause code, allow us to walk through code line-by-line, check what variables are, and more.

The Android Studio Debugger

Similar in name, the Android Studio version is also called the debugger. It allows us to monitor our application in real time, and can provide additional insight regarding a connected device or emulator. Let's walk through using the debugger in Android Studio, to familiarize ourselves with this process and make future bugs easier to track down and address.

First, let's remove the two Log messages we added in the last lesson:


public class RestaurantsActivity extends AppCompatActivity {
    @Bind(R.id.locationTextView) TextView mLocationTextView;
    @Bind(R.id.listView) ListView mListView;

    private String[] restaurants = new String[] {"Sweet Hereafter", "Cricket", "Hawthorne Fish House", "Viking Soul Food",
            "Red Square", "Horse Brass", "Dick's Kitchen", "Taco Bell", "Me Kha Noodle Bar",
            "La Bonita Taqueria", "Smokehouse Tavern", "Pembiche", "Kay's Bar", "Gnarly Grey", "Slappy Cakes", "Mi Mero Mole" };

    protected void onCreate(Bundle savedInstanceState) {

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, restaurants);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String restaurant = ((TextView)view).getText().toString();
                Toast.makeText(RestaurantsActivity.this, restaurant, Toast.LENGTH_LONG).show();

        Intent intent = getIntent();
        String location = intent.getStringExtra("location");

        mLocationTextView.setText("Here are all the restaurants near: " + location);


Next, in order to pause the code at a given location, we need to add something called a breakpoint. A breakpoint is a language-agnostic term meaning the location at which code is intentionally paused.

Placing Breakpoints

We can insert a breakpoint (or even multiple breakpoints) where we would like our code to pause. There are several ways to add a breakpoint.

  1. Click the line where you would like to add a breakpoint. Then, select Run > Toggle Line Breakpoint.


A red circle will appear directly to the left of this line of code. The area this red circle resides in is known as the gutter:


To remove a breakpoint in the same fashion, we can simply select the same line, then select Run > Toggle Line Breakpoint again to toggle it off.

  1. We can also simply click on the gutter to the immediate left of the line of code we'd like to add a breakpoint to. The same red circle should appear in the gutter where you clicked:


Hitting Breakpoints

Then, we can run our code by selecting the "Debug" icon at the top. This will launch our application in the emulator or connected device.


If an emulator is not already running, this will prompt you to select an emulator or device to run the application on. Once an emulator is up and running, travel to the activity the breakpoint was placed in. When the code containing the breakpoint is executed, the application will pause, and the debugger console will appear in the lower half of the Android Studio window:


Using the Android Debugger

Once the application is paused, we have several options. We can hover over variables to see their current values:


If we click the + symbol on the left side of the yellow pop-over that appears when we hover over a variable, we can see further details, too:


Navigating Through Code

Additionally, the Debugger pane will open in the lower half of the Android Studio window. The debugger contains multiple buttons for interacting with code. Here are the 10 you'll use the most:


1. Show Execution Point

This will place the cursor back to the spot you're currently debugging. (ie: if you insert a breakpoint somewhere, look around in a few other files, you can hit this to return to your original breakpoint).

2. Step Over

This advances to the next line of code without entering a method.

3. Step Into

This will advance to the first line of code inside a method call.

4. Force Step Into

This will forcibly advance to the first line of code inside a method call, if the option above does not work for any reason.

5. Step Out

This advances to the next line of code outside of the current method.

6. Resume Program

This will continue running the app normally.

7. Pause Program

This will be greyed-out at first, because the program is already paused. If you opt to resume the program, you may pause it again with this option.

8. Stop App

This halts the running applicaiton in the emulator or device entirely.

9. View Breakpoints

This will open a window that will summarize exactly which breakpoints have been inserted into what areas of your application. In addition, it will allow you to customize settings for each individual breakpoints. For instance, you can select Remove once hit to automatically remove the breakpoint after it pauses your code. Log message to console to include a message in the logcat when this breakpoint is hit, or add conditions, log messages, and filters.


10. Mute Breakpoints

This is an option you can toggle on and off that will temporarily ignore other breakpoints in the code while you interact with the code in other ways (primarily stepping into/out of/through/etc.)

As you can see, this is a pretty powerful tool. Begin to experiment with using the debugger in your upcoming projects, even before you experience bugs. That way, you should have a decent idea of how to use this powerful tool when you do need it.