Overlook on Android Studio: 5. Debugging in Android Studio

In this chapter you will learn how to debug application using logcat and debuging tool in the Android Studio. As you know Debugging is the most important technique in the development process. Debugging technique is the process of finding incorrect logics and make them correct. In another side which helps to find out how application is running as per decided flow.

Topics Covered


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3962) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Logcat is one of the important tool for developers in Android Studio. Which provides ability to developer to trace the code while testing application. Also it has ability to send the messages from emulator or physical devices to development environment while developing app.

So lets see how to use logcat.

Here I am going to send a message to logcat from onCreate method. To send a message I have to use Log class which is member of the android.util. Then call the “d” method by passing two parameters. See the code below.

The first parameter is the “Tag” and the another one is the “Message” parameter.

Now add log message in one more method. That is in onPause method.

So lets see the output by running project.

Debugging in Android Studio

See in the logcat window, where you can see the message you sent from your application.

When you take out the focus from your app. It will get paused and you can see the message sent from onPause method of the app.

Debugging in Android Studio


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3962) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

You may already known about what is debugging if you have good knowledge about programming. Debugging is the process of finding bugs from the developed computer program of software and remove or fixing them. So your program or software can run properly.

Here I will show you how to use breakpoint, watch variables and step through code while debugging.

So let see how we can debug with Android Studio with a basic example of for loop.

Just create new project and add a button control on design view of layout and call the function named “startCounter” onClick event of button control. See the image below.

Debugging in Android Studio

Now click on the left side of the small light icon which shows quick fix availability and select “Create ‘startCounter(View)’ in ‘MainActivity’” option from dropdown menu.

Debugging in Android Studio

After selecting create command, it will automatically create a function in MainActivity Class.

Now simply add following code snippet into the function.

So lets start with break-point. See in the image, To break your excution while running application, simply click on the left side gutter of the log message. Debugging in Android Studio

And here you can see, the break-point will be added in gutter at the same line where you clicked.

Now click on the debug icon from the toolbar or go to the Run menu and run “Debug app” command. Once app will run on the emulator, then click on “Start Counter” button.

Debugging in Android Studio

And here you can see the starting value of “i” of “for loop” in debugger window.

If you want to continue debugging application until “For Loop” get finished or next operation of program, you can resume program/app by clicking on “Resume program” button or by pressing F9 key. See the image below.

Debugging in Android Studio

if you click on resume, you can see the value of “i” is increasing by 1 on every click of resume as we have set value of i increasing by 1 in For Loop.

You can also add variable into watch list by right click on variable and select “Add to watches” command from context menu. See the image below.

Debugging in Android Studio

Now you can monitor variable values in Watches window while execution of code.

Debugging in Android Studio

If you resume the program you can notice into watches window how variable “i”s value will increase by 1.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *